iOS_WKWebView加载本地网页
2017-11-03 15:27
731 查看
@property (strong, nonatomic) WKWebView *webView; @property (nonatomic, copy) NSString *url;
WKWebView加载工程内
Html页面
// 我这里是将html资源文件放置在工程内一个bundle的文件夹内 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]]];
WKWebView加载沙盒内
Html页面
// 将要加载的html路径 NSString *urlStr1 = @"~/Cache/fund/index.html"; // 将要加载的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]; // NO代表此路径没有下一级,等同于[NSURL fileURLWithPath:filePath]; // 如果设置为YES,则路径会自动添加一个“/” 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]; // urlComponents.URL 返回拼接后的(NSURL *) // urlComponents.string 返回拼接后的(NSString *) return urlComponents.string; }
相关文章推荐
- iOS 加载HTML的相关问题(与JS的互相调用和WKWebView加载本地网页)
- iOS中wkwebview加载本地html的要点
- iOS WKWebView添加网页加载进度条(转)
- WKWebView实现网页静态资源优先从本地加载
- IOS 使用 WKWebView 加载本地的JS和CSS文件出现乱码问题
- iOS 8 WkWebView 网页的配置和前进,后退,js 交互和进度条的加载
- IOS 使用 WKWebView 加载本地的JS和CSS文件出现乱码问题
- IOS开发(7)WKWebView加载本地HTML、CSS、JS文件JS(解决html内访问其他资源路径问题)
- iOS WKWebView添加网页加载进度条
- iOS 8 WkWebView 网页的配置和前进,后退,js 交互和进度条的加载
- iOS WKWebView 远端h5优先加载本地资源
- iOS WKWebView 加载本地html文件(swift)
- iOS wkwebview懒加载中遇到的问题
- UIWebview和WKWebview加载本地html
- iOSStroyBoard拖拽webView,加载网页前黑边问题
- Androidwebview加载本地html网页
- iOS WebView 加载本地资源(图片,文件等)
- iOS之WKWebView修改网页页面的值
- iOS开发---WKWebView加载不受信任的https
- 使用WKWebView的一个问题:iOS中使用本地html的一个大坑