在WKWebView中启动电话/电子邮件/地图链接 [英] Launching phone/email/map links in WKWebView
问题描述
KINWebBrowser 是iOS应用程式的开放原始码网路浏览器模组。我最近升级了KINWebBrowser,以使用 WKWebView 开始逐步淘汰UIWebView。这产生了显着的改进,但是:
问题:WKWebView不允许用户启动包含电话号码,电子邮件地址,地图等URL的链接。
如何配置WKWebView,以便从显示的页面以链接形式启动这些备用网址的标准iOS行为?
所有代码都可在此处
我能够得到它通过将此函数添加到您的KINWebBrowserViewController.m
<$ p,工作Google地图链接(看起来与target =_ blank相关)和tel: $ p>
- (void)webView:(WKWebView *)webView decisionPolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler
{
if webView!= self.wkWebView){
decisionHandler(WKNavigationActionPolicyAllow);
return;
}
UIApplication * app = [UIApplication sharedApplication];
NSURL * url = navigationAction.request.URL;
if(!navigationAction.targetFrame){
if([app canOpenURL:url]){
[app openURL:url];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
}
if([url.scheme isEqualToString:@tel])
{
if([app canOpenURL:url])
{
[app openURL:url];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
}
decisionHandler(WKNavigationActionPolicyAllow);
}
KINWebBrowser is an open source web browser module for iOS apps. I recently upgraded KINWebBrowser to use WKWebView to begin phasing out UIWebView. This yields significant improvements, but:
Problem: WKWebView does not enable users to launch links containing URLs for phone numbers, email address, maps, etc.
How can I configure a WKWebView to launch the standard iOS behaviors for these alternate URLs when launched as links from the displayed page?
All of the code is available here
More info on WKWebKit
See the issue on the KINWebBrowser GitHub here
I was able to get it to work for the Google Maps link (which appears to be related to the target="_blank") and for the tel: scheme by adding this function to your KINWebBrowserViewController.m
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
if(webView != self.wkWebView) {
decisionHandler(WKNavigationActionPolicyAllow);
return;
}
UIApplication *app = [UIApplication sharedApplication];
NSURL *url = navigationAction.request.URL;
if (!navigationAction.targetFrame) {
if ([app canOpenURL:url]) {
[app openURL:url];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
}
if ([url.scheme isEqualToString:@"tel"])
{
if ([app canOpenURL:url])
{
[app openURL:url];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
}
decisionHandler(WKNavigationActionPolicyAllow);
}
这篇关于在WKWebView中启动电话/电子邮件/地图链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!