下面先介绍拼接两种靠谱方式
第一种
fileURL = [NSURL URLWithString:@"?lan=zh" relativeToURL:fileURL];
//这时候log fileURL发现返回url的是一个 ”?lan=zh -- OriginFileURL“ 导致加载不出来
fileURL.filePathURL
//如果要获取本地资源路径用这个去加载 会返回一个file协议地址 如果是url是以file开头
第二种
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:finalString resolvingAgainstBaseURL:NO];
[urlComponents setQueryItems:@[[NSURLQueryItem queryItemWithName:@"lan" value:@"zh"]]];
urlComponents.URL
//得到的最终url
第三种
//前两种属于直接拼接可能存在转义问题
+ (NSString *)appendUrlParmasWithBaseUrl:(NSString * )urlString andParams:(NSDictionary * )params{
NSURL *parsedURL = [NSURL URLWithString:urlString];
NSString* queryPrefix = parsedURL.query ? @"&" : @"?";
NSMutableArray* pairs = [NSMutableArray array];
for (NSString* key in [params keyEnumerator]) {
if (![[params objectForKey:key] isKindOfClass:[NSString class]]) {
continue;
}
NSString *value = (NSString *)[params objectForKey:key];
NSCharacterSet *allowedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"];
NSString *urlEncodingKey = [key stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
NSString *urlEncodingValue = [value stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
[pairs addObject:[NSString stringWithFormat:@"%@=%@", urlEncodingKey, urlEncodingValue]];
}
NSString* query = [pairs componentsJoinedByString:@"&"];
return [NSString stringWithFormat:@"%@%@%@", urlString, queryPrefix, query];
}
本地html静态资源加载确行方案:
iOS9之前拷贝到temp()目录loadrequest
iOS9之后[wkWeb loadFileURL:fileurl allowingReadAccessToURL:url]
参数随便写的,方法使用就不赘述了