赞
踩
WKWebView加载网络文件
在ViewController里面新建一个wkwebview属性, 我们初始化它并且加入到视图中来,加载https://www.baidu.com这个URL。
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"WKWebview";
self.webview = [[WKWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.webview];
self.webview.navigationDelegate = self;
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[self.webview loadRequest:request];
}
WKWebView加载本地文件
如果是加载本地html文件,可以使用loadFileURL这个方法,写一个本地的html文件,命名比如custom.html,放到主工程中,使用以下代码加载这个custom.html文件。
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"WKWebview";
self.webview = [[WKWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.webview];
self.webview.navigationDelegate = self;
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"custom" ofType:@"html"];
NSURL *fileUrl = [NSURL fileURLWithPath:htmlPath];
[self.webview loadFileURL:fileUrl allowingReadAccessToURL:fileUrl];
}
WKWebView代理方法
可以看到我们设置了navigationDelegate为self,所以ViewController需要实现对应的协议方法。navigationDelegate是WKWebView提供的代理方法,包含了WKWebView在加载期间的各种回调。
//在网络加载前调用,用于前置拦截一些网络请求,WKNavigationActionPolicyAllow代表允许请求,WKNavigationActionPolicyCancel代表取消请求
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
NSLog(@"decidePolicyForNavigationAction");
decisionHandler(WKNavigationActionPolicyAllow);
}
//网络请求开始的代理方法
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
NSLog(@"didStartProvisionalNavigation");
}
//在网络加载后调用,在收到网络响应后拦截,WKNavigationActionPolicyAllow代表允许网页加载,WKNavigationActionPolicyCancel代表取消网页加载
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{
NSLog(@"decidePolicyForNavigationResponse");
decisionHandler(WKNavigationResponsePolicyAllow);
}
//网络请求开始的代理方法
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{
NSLog(@"didCommitNavigation");
}
//网络请求结束的代理方法,网络请求成功或者失败都会调用,需要在navigation里面获取response响应判断statusCode
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
NSLog(@"didFinishNavigation");
}
//网络请求结束的代理方法,这种情况对应的是网络传输的错误
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
NSLog(@"didFailNavigation");
}
//网络请求结束的代理方法,这种情况对应的是请求没有到达服务端,比如请求的url不存在,在DNS解析的时候找不到IP地址
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
NSLog(@"didFailProvisionalNavigation");
}
//收到301或者302重定向请求的时候调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation
{
NSLog(@"didReceiveServerRedirectForProvisionalNavigation");
}
//WKWebView进程被终止的时候调用
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView
{
NSLog(@"webViewWebContentProcessDidTerminate");
}
如果我们使用加载网络文件的方式渲染WKWebView,在加载成功的状态下,可以得到回调的Log信息。
decidePolicyForNavigationAction
didStartProvisionalNavigation
decidePolicyForNavigationResponse
didCommitNavigation
didFinishNavigation
由于网页加载成功,fail相关的方法不会回调,网页没有重定向的响应,
didReceiveServerRedirectForProvisionalNavigation也没有回调。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。