UIWebViewDelegate有以下几个代理方法。相对于WKWebView的delegate来说很少。相信小伙伴们通过字面意思都可以知道下面方法是做什么的。UIWebViewDelegate是用来监控页面加载进度和结果的。
UIWebViewNavigationType{
UIWebViewNavigationTypeLinkClicked,点击链接
UIWebViewNavigationTypeFormSubmitted,提交表单
UIWebViewNavigationTypeBackForward,点击前进或返回按钮
UIWebViewNavigationTypeReload,点击重新加载按钮
UIWebViewNavigationTypeFormResubmitted,重复提交表单
UIWebViewNavigationTypeOther,发生其它行为
}
__TVOS_PROHIBITED @protocol UIWebViewDelegate <NSObject>
@optional
//询问是否允许加载页面请求。UIWebViewNavigationType是个枚举。 可以通过判断不同的枚举值拿到事件,进行操作。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
//页面开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView;
//页面加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView;
//页面加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error;
@end
接下来看看WKNavigationDelegate,它将UIWebViewDelegate细化,可以监控加载进度,服务器跳转、身份认证等。
@protocol WKNavigationDelegate <NSObject>
@optional
//请求之前,决定是否要跳转:用户点击网页上的链接,需要打开新页面时,将先调用这个方法。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
//接收到相应数据后,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;
//页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation;
// 主机地址被重定向时调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation;
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation;
// 页面加载完毕时调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation;
//跳转失败时调用
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
// 如果需要证书验证,与使用AFN进行HTTPS证书验证是一样的
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *__nullable credential))completionHandler;
//9.0才能使用,web内容处理中断时会触发
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView NS_AVAILABLE(10_11, 9_0);
@end
按照上面代理方法的顺序,每个内部都打印数字.第一个打印1,第二个打印2,第三个打印3等等。
WKWebView *web = [[WKWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:web];
web.navigationDelegate = self;
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
界面加载出来,其打印结果:
Snip20160902_6.png这两个回调要这么写。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
// WKNavigationActionPolicyCancel,
// WKNavigationActionPolicyAllow,
// navigationAction.sourceFrame;
// navigationAction.targetFrame;
// navigationAction.request;
if (navigationAction.navigationType == WKNavigationTypeLinkActivated){
decisionHandler(WKNavigationActionPolicyCancel);
}else{
decisionHandler(WKNavigationActionPolicyAllow);
}
NSLog(@"1");
//接收到相应后,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
if (!navigationResponse.isForMainFrame){
decisionHandler(WKNavigationResponsePolicyCancel);
}else{
decisionHandler(WKNavigationResponsePolicyAllow);
}
NSLog(@"2");
}
}
网友评论