设置WKNavigationDelegate代理
_webView.UIDelegate = self;
WKNavigationDelegate处理方法
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSString *reqUrl = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
reqUrl = [reqUrl stringByReplacingOccurrencesOfString:@"tell:" withString:@"tel:"];
__weak typeof(self) weakSelf=self;
if([reqUrl hasPrefix:@"tel"]){
NSURL *url=[NSURL URLWithString:reqUrl];
[UIApplication.sharedApplication openURL:url];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
NSLog(@"%@",reqUrl);
if([UIApplication.sharedApplication canOpenURL:navigationAction.request.URL]){
if([reqUrl hasPrefix:@"mailto"]){
NSURL *url=[NSURL URLWithString:reqUrl];
[UIApplication.sharedApplication openURL:url];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
}
if([navigationAction.request.URL.scheme hasPrefix:@"openApp://"]){
[UIApplication.sharedApplication openURL:navigationAction.request.URL options:@{} completionHandler:^(BOOL success) {
if(!success){
NSString* strMessage = @"没有安装该应用";
[weakSelf alertMessage:strMessage];
}
}];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
其中WKNavigationActionPolicyCancel你处理了不用WKWebView操心了,WKNavigationActionPolicyAllow表示你不想处理让WKWebView去处理。
网友评论