iOS内嵌WKWebView拨打电话、发短信、发邮件没反应

时间:2022-11-10 07:30:08

iOS内嵌WKWebView拨打电话没反应

 <a href="tel:13112345678">拨打号码</a> 

内嵌H5的发送短信,打电话,发送邮件代码的处理
实现webview的代理方法

swift3.0 代码:

    // 处理拨打电话、发短信、发邮件以及Url跳转等等 
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
        if navigationAction.request.url?.scheme == "tel" {
//            DispatchQueue.main.async {
                UIApplication.shared.openURL(navigationAction.request.url!);
                decisionHandler(WKNavigationActionPolicy.cancel)
//            }
        }
        else if navigationAction.request.url?.scheme == "sms"{
            //短信的处理
            UIApplication.shared.openURL(navigationAction.request.url!);
            decisionHandler(WKNavigationActionPolicy.cancel)
        }
        else if navigationAction.request.url?.scheme == "mailto"{
            //邮件的处理
            UIApplication.shared.openURL(navigationAction.request.url!);
            decisionHandler(WKNavigationActionPolicy.cancel)
        }
        else{
            decisionHandler(WKNavigationActionPolicy.allow)
        }
    }

OC代码:

// 处理拨打电话以及Url跳转等等
- (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]];
        });
    }
    else {
    decisionHandler(WKNavigationActionPolicyAllow);
    }
}

发送短信、邮件和打电话方式一样