- 加载远程URL
UIWebView加载远程URL
_uiwebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview: _uiwebView];
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[_uiwebView loadRequest:request];
WKWebView加载远程URL
_configuration = [[WKWebViewConfiguration alloc] init];
_webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:self.configuration];
[self.view addSubview:_webView];
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[_webView loadRequest:request];
可以看出,加载远程URL时,两种方法都能加载出来,
- 加载本地URL
如上图,在工程里面有两个本地html,
2.1 加载黄色的本地test01.html
2.1 - 1 UIWebView加载本地test01.html的代码为
_uiwebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview: _uiwebView];
NSString *path = [[NSBundle mainBundle] pathForResource:@"test01" ofType:@"html"];
NSURL *url = [NSURL URLWithString:path];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[_uiwebView loadRequest:request];
2.1 - 2 WKWebView加载本地test01.html的代码时需要注意
正确的加载方式是使用- (nullable WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL这个方法, 而不是使用- (nullable WKNavigation *)loadRequest:(NSURLRequest *)request
_configuration = [[WKWebViewConfiguration alloc] init];
_webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:self.configuration];
[self.view addSubview:_webView];
NSString *path = [[NSBundle mainBundle] pathForResource:@"test01" ofType:@"html"];
NSURL *fileURL = [NSURL fileURLWithPath:path];
[_webView loadFileURL:fileURL allowingReadAccessToURL:fileURL];
//加载本地HTML的错误写法
// NSURL *url = [NSURL URLWithString:path];
// NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
// [_webView loadRequest:request];
2.2 加载蓝色的本地test02.html,加载的方式跟2.1 是一样的。
屏幕快照 2018-09-19 下午4.19.58.png需要注意的是由于蓝色文件夹跟黄色文件夹不同, 取目录时需要从蓝色文件夹的最外层目录开始取,
取目录的方式
NSString *path = [[NSBundle mainBundle] pathForResource:@"test02" ofType:@"html" inDirectory:@"test02"];
如果有多层,则从最外层一直往里面取,比如
屏幕快照 2018-09-19 下午4.25.18.pngNSString *path = [[NSBundle mainBundle] pathForResource:@"test02" ofType:@"html" inDirectory:@"doc01/doc02/test02"];
- 最后,比较坑的一点是,加载本地HTML, 如果工程名或者路径含有中文时,
3.1 UIWebView加载本地HTML含有中文时需要UTF8转码
_uiwebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview: _uiwebView];
NSString *path = [[NSBundle mainBundle] pathForResource:@"TESTJS" ofType:@"html"];
//需要进行UTF8转码,否则显示不出网页
path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:path];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[_uiwebView loadRequest:request];
3.2 WKWebView加载本地HTML含有中文时不需要UTF8转码
_wkwebView = [[WKWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview: _wkwebView];
NSString *path = [[NSBundle mainBundle] pathForResource:@"TESTJS" ofType:@"html"];
//不需要进行UTF8转码
// path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL fileURLWithPath:path];
[_wkwebView loadFileURL:url allowingReadAccessToURL:url];
分析原因, 因为获取本地html时, 如果进行了转码, 则路径变了,找不到本地的html文件,
如果是使用UIWebView加载远程的方式来加载本地,则转码后,内部会进行自动转换识别,
由于WKWebView不能使用loadRequest的加载本地html,因此不需要转码
网友评论