有两种方法,
第一种:
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
//打印携带电话的URL
NSLog(@"URL: %@", webView.URL);
NSString *strURL = [webView.URL.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSArray * array = [strURL componentsSeparatedByString:@"//"];
if ([[strURL substringWithRange:NSMakeRange(0, 3)] isEqualToString:@"tel"]) {
NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"%@", [NSString stringWithFormat:@"telprompt://%@",array[1]]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
}
有时候上面的这种方法可能会出现一些不确定的问题,导致有可能吊不起打电话的功能,
第二种:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURL *URL = navigationAction.request.URL;
NSString *scheme = [URL scheme];
if ([scheme isEqualToString:@"tel"]) {
NSString *resourceSpecifier = [URL resourceSpecifier];
NSString *callPhone = [NSString stringWithFormat:@"telprompt:%@", resourceSpecifier];
/// 防止iOS 10及其之后,拨打电话系统弹出框延迟出现
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
});
}
decisionHandler(WKNavigationActionPolicyAllow);
}