Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable links to open in default browser on WebView [duplicate]

I'm learning OS X/Swift development and have loaded a webpage with contains links to other websites, how I open these links in the default browser. At the moment when clicking on the links nothing happens at all. This is my ViewController.swift contents:

import Cocoa
import WebKit
import Foundation

class ViewController: NSViewController, WebFrameLoadDelegate, WKNavigationDelegate {

    @IBOutlet weak var webView: WebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let URL = "https://test.mywebsite.com"

        self.webView.frameLoadDelegate = self
        self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: URL)!))



    }

    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}
like image 775
orange Avatar asked Jan 09 '16 20:01

orange


People also ask

How do I open a link in WebView?

Within the shouldOverrideUrlLoading() method simply get the URL from the request and pass into the Intent. See the full example.

How to enable JavaScript on WebView?

JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.

How to use JavaScript in WebView Android?

WebView allows you to bind JavaScript code to Android code through an interface. To do this, we must use the addJavaScriptInterface() method, which is passed the class that provides the interface for JS, and the name that will be used to display the instance in JS (for example, “AndroidFunction“).

How do I open WebView in Chrome?

# Open a WebView in DevToolsThe chrome://inspect page displays a list of debug-enabled WebViews on your device. To start debugging, click inspect below the WebView you want to debug. Use DevTools as you would for a remote browser tab.


1 Answers

I am not entirely clear what you are asking.

I think you are asking how to make links clicked inside a WebView open in the desktop browser (e.g. Safari)?

If this is what you are trying to achieve, you can use the WebPolicyDelegate to determine where a URL should open.

Eg.

import Cocoa
import WebKit

class ViewController: NSViewController, WebFrameLoadDelegate, WebPolicyDelegate {

    @IBOutlet weak var webView: WebView!

    let defaultURL = "http://www.apple.com/"  // note we need the trailing '/' to match with our 'absoluteString' later

    override func viewDidLoad() {
        super.viewDidLoad()

        self.webView.frameLoadDelegate = self
        self.webView.policyDelegate = self
        self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: defaultURL)!))

    }

    func webView(webView: WebView!, decidePolicyForNavigationAction actionInformation: [NSObject : AnyObject]!, request: NSURLRequest!, frame: WebFrame!, decisionListener listener: WebPolicyDecisionListener!) {

        if let currentURL = request.URL {
            if currentURL.absoluteString == defaultURL {
                print("our base/default URL is being called - showing in WebView")
                listener.use()   // tell the listener to show the request
            } else {
                print("some other URL - ie. a link has been clicked - ignore in WebView")
                listener.ignore()   // tell the listener to ignore the request
                print("redirecting url: \(currentURL.absoluteString) to standard browser")
                NSWorkspace.sharedWorkspace().openURL(currentURL)
            }
        }
    }

}

If this is not what you are asking, could you please edit your question to make it clearer.

like image 72
So Over It Avatar answered Sep 19 '22 23:09

So Over It