2018年4月28日
1.中文连接在编码后就无法正常显示问题修改
https://baike.baidu.com/item/%E4%BA%92%E8%81%94%E7%BD%91/199186?fr=aladdin
修改方法:只对参数进行编码
+ (nullable instancetype)hu_URLWithString:(NSString *)urlString{
NSString *realEncodeUrl;
NSArray *arr = [urlString componentsSeparatedByString:@"?"];
if (arr.count > 1) {
NSString *paramPart = arr[1];
//ios 9以上系统
if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_9_0){
paramPart = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
}else{
paramPart = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
realEncodeUrl = [NSString stringWithFormat:@"%@?%@",arr[0],paramPart];
}else{
realEncodeUrl = urlString;
}
if ([realEncodeUrl hasPrefix:@"http"]) {
return [self URLWithString:realEncodeUrl];
}else{
return nil;
}
}
2017年8月9日
1.NSString *encodedValue = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
2017年3月10日
1.本地url链接组装
//法1:(推荐用公共接口管理方便)
NSString *filePath = [HsURLUtil appPathWithFileName:@“test.mp4"];
NSURL *url = [NSURL fileURLWithPath:str];
//法2:(其实本质是一样的)
NSString *str = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:str];
2.公共接口实现部分
//根据传入文件名,返回关键目录根目录加上文件的绝对路径 document
+ (NSString *)dataFilePathWithFileName:(NSString *)fileName WithDirType:(NSSearchPathDirectory)dirType
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(dirType, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *absolutefileName = [fileName lastPathComponent];
return [documentsDirectory stringByAppendingPathComponent:absolutefileName];
}
//根据传入的相对路径,返回基于全局的关键目录的绝对路径
+ (NSString *)dataFilePathWithFilePath:(NSString *)fileName WithDirType:(NSSearchPathDirectory)dirType;
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(dirType, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:fileName];
}
eg:使用
NSString *filePath = [HsURLUtil dataFilePathWithFileName:@"trade_function_config.xml.encode" WithDirType:NSDocumentDirectory];
//根据传入文件名,返回安装目录加上文件名的绝对路径 app
+ (NSString *)appPathWithFileName:(NSString *)fileName
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
filePath = [filePath stringByDeletingLastPathComponent];
NSString *absolutefileName = [fileName lastPathComponent];
// TRACELOG([filePath stringByAppendingPathComponent:absolutefileName]);
return [filePath stringByAppendingPathComponent:absolutefileName];
}
//根据传入的相对路径,返回基于安装目录的绝对路径
+ (NSString *)appPathWithFilePath:(NSString *)filePath
{
NSString *appPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
appPath = [appPath stringByDeletingLastPathComponent];
//NSString *absolutefileName = [fileName lastPathComponent];
// TRACELOG([filePath stringByAppendingPathComponent:absolutefileName]);
return [appPath stringByAppendingPathComponent:filePath];
}
eg:使用
NSString *filePath = [HsURLUtil appPathWithFileName:@"JF-info.plist.encode"];
如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。