在Safari中打开UIWebView链接

时间:2016-11-24 22:20:17

标签: swift xcode webview safari uiwebview

我有一个使用Swift 3的iOS应用程序。我在这个应用程序中有一个uiwebview。每当用户点击webview中的链接时,它就会在webview中打开。我需要它在iPhone上的Safari应用程序内打开链接。这是代码:

class VideosViewController: UIViewController {

    @IBOutlet var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        var URL = NSURL(string: "http://example.com")

        webView.loadRequest(NSURLRequest(url: URL! as URL) as URLRequest)


    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

你能给我一些我可以输入的代码吗?我是编码的新手,所以非常感谢。

更新:由于答案我得到了一些代码,但链接仍未在Safari中打开。这是迄今为止的代码......

class VideosViewController : UIViewController, UIWebViewDelegate {

@IBOutlet weak var webView : UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    guard let url = URL(string: "http://example.com") else { return }

    let request = URLRequest(url: url)
    webView.loadRequest(request)

}

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {

    if navigationType == .linkClicked {

        guard let url = request.url else { return true }
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
        return false
    }
    return true
}

}

1 个答案:

答案 0 :(得分:1)

使用UIWebView:

确保您的视图控制器符合UIWebViewDelegate,然后在控制器中实现此方法:

class VideosViewController : UIViewController, UIWebViewDelegate {

    @IBOutlet weak var webView : UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        guard let url = URL(string: "http://example.com") else { return }

        webView.delegate = self

        let request = URLRequest(url: url)
        webView.loadRequest(request)

    }

    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {

        if navigationType == .linkClicked {

            guard let url = request.url else { return true }
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
            return false
        }
        return true
    }
}

使用WKWebView:

使用WKWebView代替UIWebView,并确保您的控制器符合WKNavigationDelegate协议。因此,您的实现将如下所示:

class VideosViewController : UIViewController, WKNavigationDelegate {

    var webView : WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        guard let url = URL(string: "http://example.com") else { return }

        webView = WKWebView(frame: self.view.frame)
        webView.translatesAutoresizingMaskIntoConstraints = false
        webView.isUserInteractionEnabled = true
        webView.navigationDelegate = self

        self.view.addSubview(self.webView)

        let request = URLRequest(url: url)
        webView.load(request)

    }


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

        // Check if a link was clicked
        if navigationAction.navigationType == .linkActivated {

            // Verify the url
            guard let url = navigationAction.request.url else { return }
            let shared = UIApplication.shared

            // Check if opening in Safari is allowd
            if shared.canOpenURL(url) {

                // Ask the user if they would like to open link in Safari
                let alert = UIAlertController(title: "Open link in Safari?", message: nil, preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (alert: UIAlertAction) -> Void in
                    // User wants to open in Safari
                    shared.open(url, options: [:], completionHandler: nil)
                }))
                alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))

                present(alert, animated: true, completion: nil)

            }
            decisionHandler(.cancel)
        }
        decisionHandler(.allow)
    }
}

这样,当用户点击网页视图中的链接时,系统会提示他们提示他们是否要在Safari中打开它,如果允许,会启动Safari链接将被打开。