ios - 为什么 WKWebView 不打开 ios 9 中的电话链接?

标签 ios swift ios9 ios10 wkwebview

我有一个 WKWebView 加载网页,网页上有一些电话链接。

目前我有这段代码来处理对这些链接的点击。

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if navigationAction.request.url?.scheme == "tel" {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(navigationAction.request.url!, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(navigationAction.request.url!)
        }
        decisionHandler(.cancel)
        return
    }
    decisionHandler(.allow)
}

这在任何安装了 ios 10 的设备上都可以正常工作,系统会提示我一个警告框,要求取消或调用电话。但在 ios 9 设备上,电话应用程序屏幕闪烁(无提示),之后什么也没有发生。

最佳答案

继续之前的关键概念:

By default, a web view automatically converts telephone numbers that appear in web content to Phone links. When a Phone link is tapped, the Phone app launches and dials the number. The tel URL scheme is used to launch the Phone app on iOS devices and initiate dialing of the specified phone number.

从 html 端来看,显示电话号码的网页必须是这样的:

<body>
     <!-- Then use phone links to explicitly create a link. -->
     <p>A phone number: <a href="tel:1-408-555-5555">1-408-555-5555</a></p>
     <!-- Otherwise, numbers that look like phone numbers are not links. -->
     <p>Not a phone number: 408-555-5555</p>
</body>

在这个简短的序言之后,WKWebView 有两种行为,一种用于 iOS 9,另一种用于 iOS 10 及更高版本。

在 iOS 10 上,Apple 在 WKWebViewConfiguration 上引入了一个名为 dataDetectorTypes 的属性,默认情况下此属性的值为 WKDataDetectorTypeNone,这意味着不检测没有什么。因此,如果您想要检测电话号码和链接(仅作为示例),WKWebView 的配置将如下所示(假设您有一个类型为 WKWebView 的变量 _webView) :



    WKWebViewConfiguration *configuration = [WKWebViewConfiguration new];
    if ([configuration respondsToSelector:@selector(dataDetectorTypes)]) {
        configuration.dataDetectorTypes = UIDataDetectorTypeLink | UIDataDetectorTypePhoneNumber;
    }

    _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];

使用此配置,您的 webView 将能够处理幕后的所有事情。

现在在 9 > iOS < 10(iOS 9 到 10 之间)你的委托(delegate)决策处理程序看起来像这样:



    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {

     if ([navigationAction.request.URL.scheme isEqualToString:@"tel"]){

            [UIApplication.sharedApplication openURL:navigationAction.request.URL];

            decisionHandler(WKNavigationActionPolicyCancel);

        }else {
            decisionHandler(WKNavigationActionPolicyAllow);
        }

    }

这将打开手机应用程序。请记住:

iOS 10.3 and later displays an alert and requires user confirmation before dialing. (When this scenario occurs in versions of iOS prior to 10.3, iOS initiates dialing without further prompting the user and does not display an alert, although a native app can be configured to display its own alert.

请不要遵循任何关于 URL scheme whitelist 的说明,这是苹果在 iOS 9 上引入的另一个故事,但这是您需要声明 url scheme 处理程序的时候,目前 tel scheme 由 iOS 处理。

希望对您有所帮助。

关于ios - 为什么 WKWebView 不打开 ios 9 中的电话链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44835703/

相关文章:

iphone - 多节 UITableView 和 NSArray

iphone - 如何在 UILabel 中将三次幂/立方体字符的上标显示为字符串?

ios - Firebase 数据库突然返回 nil

iphone - Xcode 7 : UITextview doesn't aligned properly for 3 different sizes iPhone screen

iOS UIpageviewcontroller 详见 uisplitviewcontroller。忽略后平移手势

ios - 如何通过 2 个 View Controller 传递 textfield.text 数据

objective-c - 在 Swift 类中调用 Objective-C 类

ios - XCode UI Tests 如何测试屏幕边缘平移手势?

ios - 自定义 UICollectionViewFlowLayout 边框

ios - 当我使用 StoreKit 的 requestReview 功能时,有什么方法可以知道用户是否点击了提交或不现在或取消按钮?