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");
}
}