美文网首页程序员
iOS WKWebView 处理自定义URLScheme如tel

iOS WKWebView 处理自定义URLScheme如tel

作者: 何景根 | 来源:发表于2021-01-15 11:32 被阅读0次

    设置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去处理。

    相关文章

      网友评论

        本文标题:iOS WKWebView 处理自定义URLScheme如tel

        本文链接:https://www.haomeiwen.com/subject/qqgiaktx.html