@property (strong, nonatomic) WKWebView *webView;
@property (nonatomic, copy) NSString *url;
NSString *path = [[[NSBundle mainBundle] pathForResource:@"H5Local" ofType:@"bundle"] stringByAppendingPathComponent:@"fund/index.html"];
self.url = [self componentFileUrlWithOriginFilePath:path Dictionary:@{@"key":@"value"}];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
NSString *urlStr1 = @"~/Cache/fund/index.html";
NSString *urlStr2 = @"~/Cache/fund";
self.url = [self componentFileUrlWithOriginFilePath:urlStr1 Dictionary:@{@"key":@"value"}];
[self.webView loadFileURL:[NSURL URLWithString:self.url] allowingReadAccessToURL:[NSURL fileURLWithPath:urlStr2]];
WKWebView
对加载的本地网页进行后拼接参数- 无论是加载沙盒内网页还是工程内网页,如果需要在网页后拼接参数,不能简单的使用字符串拼接的方式进行拼接,需要借助
NSURLComponents
类进行数据的拼接。 - 使用方法如下:
/**
本地网页数据拼接
@param filePath 网页路径
@param dictionary 拼接的参数
@return 拼接后网页路径字符串
*/
- (NSString *)componentFileUrlWithOriginFilePath:(NSString *)filePath Dictionary:(NSDictionary *)dictionary{
NSURL *url = [NSURL fileURLWithPath:filePath isDirectory:NO];
NSURLComponents *urlComponents = [[NSURLComponents alloc]initWithURL:url resolvingAgainstBaseURL:NO];
NSMutableArray *mutArray = [NSMutableArray array];
for (NSString *key in dictionary.allKeys) {
NSURLQueryItem *item = [NSURLQueryItem queryItemWithName:key value:dictionary[key]];
[mutArray addObject:item];
}
[urlComponents setQueryItems:mutArray];
return urlComponents.string;
}