2014-10-22 65 views
10

KINWebBrowser是一款適用於iOS應用的開源Web瀏覽器模塊。我最近升級KINWebBrowser使用WKWebView開始逐步取消UIWebView。這就產生了顯著改善,但:在WKWebView中啓動電話/電子郵件/地圖鏈接

問題:WKWebView並不能使用戶推出包含電話號碼,電子郵件地址,地圖等的URL鏈接

如何配置一個WKWebView啓動標準iOS行爲這些替代網址是否以展示頁面的鏈接形式啓動?

所有在WKWebKit

code is available here

更多資訊,請參閱issue on the KINWebBrowser GitHub here

+1

你不能這樣做。如果這個功能對你來說很重要,那麼這就是現在堅持使用UIWebView的原因 - 以及向Apple提交增強請求的原因。 UIWebView可以做WKWebView無法做到的事情。 – matt 2014-10-22 19:07:04

回答

18

我能得到它的谷歌地圖鏈接工作(這似乎與目標= 「_blank」)和電話:方案通過將此功能添加到您的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); 
} 
+2

這段代碼有點冗長,並不保證會調用'decisionHandler'。 – 2014-10-22 19:16:02

+0

好點!謝謝。這是一個深夜,來自兩個不同來源的最後一分鐘補丁一起投擲,以快速解決問題。雖然它確實有效,但並不是最乾淨的,肯定存在decisionHandler沒有被調用的情況。我編輯了代碼。謝謝! – 2014-10-23 13:00:58

+2

此代碼絕對是正確的路徑,但它引入了一個安全漏洞,可能會導致不希望發起呼叫和FaceTime呼叫。看看我的解釋在這裏:https://github.com/dfmuir/KINWebBrowser/issues/10 – dfmuir 2014-10-25 06:15:42

1

這有助於我的Xcode 8 WKWebview

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { 
    if navigationAction.targetFrame == nil { 
     let url = navigationAction.request.url 
     if url?.description.range(of: "http://") != nil || url?.description.range(of: "https://") != nil || url?.description.range(of: "mailto:") != nil || url?.description.range(of: "tel:") != nil { 
      UIApplication.shared.openURL(url!) 
     } 
    } 
    return nil 
} 

編輯:

在鏈接必須屬性target="_blank"

0

以上回答workes我,但我需要它改寫爲SWIFT 2.3

if navigationAction.targetFrame == nil { 
    let url = navigationAction.request.mainDocumentURL 
    if url?.description.rangeOfString("mailto:")?.startIndex != nil || 
     url?.description.rangeOfString("tel:")?.startIndex != nil 
    { 
     if #available(iOS 10, *) { 
      UIApplication.sharedApplication().openURL(url!,options: [:], completionHandler: nil) 
     } else { 
      UIApplication.sharedApplication().openURL(url!) // deprecated 
     } 
    } 
} 
5

工程上的Xcode 8.1,雨燕2.3。

對於target =「_ blank」,電話號碼(tel :)和電子郵件(mailto :)鏈接。

func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) { 
    if webView != self.webview { 
     decisionHandler(.Allow) 
     return 
    } 

    let app = UIApplication.sharedApplication() 
    if let url = navigationAction.request.URL { 
     // Handle target="_blank" 
     if navigationAction.targetFrame == nil { 
      if app.canOpenURL(url) { 
       app.openURL(url) 
       decisionHandler(.Cancel) 
       return 
      } 
     } 

     // Handle phone and email links 
     if url.scheme == "tel" || url.scheme == "mailto" { 
      if app.canOpenURL(url) { 
       app.openURL(url) 
       decisionHandler(.Cancel) 
       return 
      } 
     } 

     decisionHandler(.Allow) 
    } 
} 
+0

不要忘記添加委託:WKUIDelegate和var webView = WKWebView()屬性 – 2016-11-22 13:03:49

4

您需要實施的其他回調得到這個權利(雨燕3.1):

// Gets called if webView cant handle URL 
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { 
    guard let failingUrlStr = (error as NSError).userInfo["NSErrorFailingURLStringKey"] as? String else { return } 
    let failingUrl = URL(string: failingUrlStr)! 

    switch failingUrl { 
    // Needed to open Facebook 
    case _ where failingUrlStr.startsWith("fb:"): 
    if #available(iOS 10.0, *) { 
     UIApplication.shared.open(failingUrl, options: [:], completionHandler: nil) 
     return 
    } // Else: Do nothing, iOS 9 and earlier will handle this 

    // Needed to open Mail-app 
    case _ where failingUrlStr.startsWith("mailto:"): 
    if UIApplication.shared.canOpenURL(failingUrl) { 
     UIApplication.shared.openURL(failingUrl) 
     return 
    } 

    // Needed to open Appstore-App 
    case _ where failingUrlStr.startsWith("itmss://itunes.apple.com/"): 
    if UIApplication.shared.canOpenURL(failingUrl) { 
     UIApplication.shared.openURL(failingUrl) 
     return 
    } 

    default: break 
    } 
} 

現在的Facebook,郵件,蘋果商店,...得到直接從您的應用程序稱爲無需打開Safari

相關問題