Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a WKWebview target="_blank" link in Safari

I am trying to get my Hybrid IOS app that uses Swift and WKWebviews to open a link that has target="_blank" or if the URL contains http://, https://, or mailto: in Mobile Safari.

From this answer I get this code.

func webView(webView: WKWebView!, createWebViewWithConfiguration     configuration: WKWebViewConfiguration!, forNavigationAction navigationAction:     WKNavigationAction!, windowFeatures: WKWindowFeatures!) -> WKWebView! {
    if navigationAction.targetFrame == nil {
        webView.loadRequest(navigationAction.request)
    }
    return nil
}

First, that doesn't do anything for me. Second, I want it to open in a new window. And I found this code that is supposed to do something like that...

if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
     UIApplication.sharedApplication().openURL(requestUrl)
}

How do I put these two together and get them to work? What do I need to add to the ViewController declaration to make it work?

like image 685
Jed Grant Avatar asked Jun 02 '15 18:06

Jed Grant


People also ask

Does WKWebView use Safari?

WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.

Does WKWebView share cookies with Safari?

You can share cookies between multiple WKWebView 's inside your app by utilising WKProcessPool . There is a way of passing cookie data from Safari to your app by combining SFSafariViewController (iOS 8 and below you will need to switch to Safari) with a custom URL scheme.

How do I debug Safari WKWebView?

Debugging with WKWebViewOpen Safari and press ⌘+, to open Preference, under the Advanced tab, check “Show Develop menu in menu bar”. Not until we have the project build can we use the Safari debugger. The debugger is under Develop → Your simulator or device.

Is UIWebView deprecated?

Apple is phasing out UIWebView, which is used by developers for integrating web content into an app in a quick and secure manner. Apple is replacing UIWebView (and WebView) with WKWebView, an updated version, as UIWebView has been deprecated.


2 Answers

Swift 4.2

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
    if navigationAction.navigationType == WKNavigationType.linkActivated {
        print("here link Activated!!!")
        if let url = navigationAction.request.url {
            let shared = UIApplication.shared
            if shared.canOpenURL(url) {
                shared.open(url, options: [:], completionHandler: nil)
            }
        }
        decisionHandler(.cancel)
    }
    else {
        decisionHandler(.allow)
    }
}
like image 131
Daniel Avatar answered Sep 16 '22 18:09

Daniel


In (from here)

 override func loadView() {
    super.loadView()
    self.webView.navigationDelegate = self 
    self.webView.UIDelegate = self  //must have this
 }

Then add the function (from here, with additions)...

func webView(webView: WKWebView,
    createWebViewWithConfiguration configuration: WKWebViewConfiguration,
    forNavigationAction navigationAction: WKNavigationAction,
    windowFeatures: WKWindowFeatures) -> WKWebView? {
        if navigationAction.targetFrame == nil {
            var url = navigationAction.request.URL
            if url.description.lowercaseString.rangeOfString("http://") != nil || url.description.lowercaseString.rangeOfString("https://") != nil || url.description.lowercaseString.rangeOfString("mailto:") != nil  {
                UIApplication.sharedApplication().openURL(url)
            }
        }
        return nil
}
like image 35
Jed Grant Avatar answered Sep 16 '22 18:09

Jed Grant