2016-03-26 67 views
17

尋找我的iOS應用程序的一些幫助。在我的應用程序中,我想要在WKWebView中打開來自我的域內(EX:communionchapelefca.org)的點擊鏈接,然後在Safari中打開所有其他域(EX:google.com)的鏈接。我更喜歡以編程的方式來做這件事,因爲這是我的代碼已經安裝的方式。WKWebView在Safari中打開某個域名的鏈接

我已經找到了幾個解決方案上的計算器(herehereherehere),但他們似乎都對象 - 基於我期待使用雨燕的解決方案是。提前致謝。

ViewController.swift:

import UIKit 
import WebKit 

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let myWebView:WKWebView = WKWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)) 

     myWebView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.communionchapelefca.org/app-home")!)) 

     self.view.addSubview(myWebView) 

回答

34

您可以實現WKNavigationDelegate,添加decidePolicyForNavigationAction方法,並檢查那裏的導航類型和請求的網址。我用google.com以下,但你可以將其更改爲您的域:

的Xcode 8.3•斯威夫特3.1

import UIKit 
import WebKit 
class ViewController: UIViewController, WKNavigationDelegate { 
    let webView = WKWebView() 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     guard let url = URL(string: "https://www.google.com") else { return } 
     webView.frame = view.bounds 
     webView.navigationDelegate = self 
     webView.load(URLRequest(url: url)) 
     webView.autoresizingMask = [.flexibleWidth,.flexibleHeight] 
     view.addSubview(webView) 
    } 
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { 
     if navigationAction.navigationType == .linkActivated { 
      if let url = navigationAction.request.url, 
       let host = url.host, !host.hasPrefix("www.google.com"), 
       UIApplication.shared.canOpenURL(url) { 
       UIApplication.shared.open(url) 
       print(url) 
       print("Redirected to browser. No need to open it locally") 
       decisionHandler(.cancel) 
      } else { 
       print("Open it locally") 
       decisionHandler(.allow) 
      } 
     } else { 
      print("not a user click") 
      decisionHandler(.allow) 
     } 
    } 
} 
+1

謝謝!你的代碼解決了我的問題! –

+1

只是作了修正;再次感謝 –

+0

@GregWilliams歡迎您 –

1

做一個函數來決定在哪裏加載的網址:

func loadURLString(str: String) { 
    guard let url = NSURL(string: str) else { 
     return 
    } 

    if url.host == "www.communionchapelefca.org" { 
     // Open in myWebView 
     myWebView.loadRequest(NSURLRequest(URL: url)) 
    } else { 
     // Open in Safari 
     UIApplication.sharedApplication().openURL(url) 
    } 
} 

用法:

loadURLString("http://www.communionchapelefca.org/app-home") // Open in myWebView 
loadURLString("http://www.apple.com") // Open in Safari 
+0

對不起我花了這麼長着你,但你的代碼沒有工作時找回我點擊我的web視圖中的外部鏈接,它仍然在webview中打開它。 –

6

對於雨燕3.0

import UIKit 
import WebKit 

class ViewController: UIViewController, WKNavigationDelegate { 
    let wv = WKWebView(frame: UIScreen.main.bounds) 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     guard let url = NSURL(string: "https://www.google.com") else { return } 
     wv.navigationDelegate = self 
     wv.load(NSURLRequest(url: url as URL) as URLRequest) 
     view.addSubview(wv) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { 
     if navigationAction.navigationType == .LinkActivated { 
      if let newURL = navigationAction.request.url, 
       let host = newURL.host , !host.hasPrefix("www.google.com") && 
       UIApplication.shared.canOpenURL(newURL) && 
       UIApplication.shared.openURL(newURL) { 
        print(newURL) 
        print("Redirected to browser. No need to open it locally") 
        decisionHandler(.cancel) 
      } else { 
       print("Open it locally") 
       decisionHandler(.allow) 
      } 
     } else { 
      print("not a user click") 
      decisionHandler(.allow) 
     } 
    } 
} 
3

下面是示例代碼對物品的迅速反應。

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WKNavigationAction *)navigationAction decisionHandler:(nonnull void (^)(WKNavigationActionPolicy))decisionHandler 
{ 
    if (navigationAction.navigationType == WKNavigationTypeLinkActivated) { 
     if (navigationAction.request.URL) { 
      NSLog(@"%@", navigationAction.request.URL.host); 
      if (![navigationAction.request.URL.resourceSpecifier containsString:@"ex path"]) { 
       if ([[UIApplication sharedApplication] canOpenURL:navigationAction.request.URL]) { 
        [[UIApplication sharedApplication] openURL:navigationAction.request.URL]; 
        decisionHandler(WKNavigationActionPolicyCancel); 
       } 
      } else { 
       decisionHandler(WKNavigationActionPolicyAllow); 
      } 
     } 
    } else { 
     decisionHandler(WKNavigationActionPolicyAllow); 
    } 
} 
1

我的SWIFT 3溶液:

public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { 

     let url = navigationAction.request.url 

     if url?.description.lowercased().range(of: "http://") != nil || url?.description.lowercased().range(of: "https://") != nil { 
      decisionHandler(.cancel) 
      UIApplication.shared.openURL(url!) 
     } else { 
      decisionHandler(.allow) 
     } 

    } 

也不要忘記設置TE代表

public override func loadView() { 
     let webConfiguration = WKWebViewConfiguration() 
     webView = WKWebView(frame: .zero, configuration: webConfiguration) 
     webView.uiDelegate = self 
     webView.navigationDelegate = self 
     view = webView 
    } 
相關問題