Open WKWebView Links in Safari

I'm creating an app that uses WKWebView. Whenever a user clicks on a link in that webview, I want the link to open up in safari instead of inside the webview. I'm really new at coding, so please make it as easy as possible. I just need the code I need to add. Here's the code bellow:


import UIKit
import WebKit
class FirstViewController: UIViewController {
   
    @IBOutlet var containerView: UIView! = nil
   
    var webView: WKWebView?
   
    override func loadView() {
        super.loadView()
       
        self.webView = WKWebView()
        self.view = self.webView!
       
    }
   
    override func viewDidLoad() {
        super.viewDidLoad()
       
        var url = NSURL(string:"http://www.example.com/")
        var req = NSURLRequest(url:url as! URL)
        self.webView!.load(req as URLRequest)
    }
   
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
   
}


I'm using Xcode 8 and Swift 3

Answered by DTS Engineer in 199512022

What you need to do here is set the navigation delegate on the web view (via the

navigationDelegate
property) and then implement the
webView(_:decidePolicyFor:decisionHandler:)
callback along the lines of what’s shown below.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if let url = navigationAction.request.url, … handle in Safari … {
        decisionHandler(.cancel)
        UIApplication.shared.openURL(url)
    } else {
        decisionHandler(.allow)
    }
}

where the elided code decides whether

url
should go to Safari or be opened in the web view.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

What you need to do here is set the navigation delegate on the web view (via the

navigationDelegate
property) and then implement the
webView(_:decidePolicyFor:decisionHandler:)
callback along the lines of what’s shown below.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if let url = navigationAction.request.url, … handle in Safari … {
        decisionHandler(.cancel)
        UIApplication.shared.openURL(url)
    } else {
        decisionHandler(.allow)
    }
}

where the elided code decides whether

url
should go to Safari or be opened in the web view.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

@eskimo - we are running into kind of the inverse problem - we are trying to load an amazon.com page in our WKWebView but some of the time our users report that it instead switches them over to the Amazon app. Assuming that the Amazon app has registered to handle "regular" web URLs like "https://www.amazon.com/some_path" (and not a custom scheme like "amazon://") is there any way for us to intercept the request and force it to load in our WKWebView rather than redirecting out?

Open WKWebView Links in Safari
 
 
Q