6

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.
        }
    }


}
orange
  • 5,297
  • 12
  • 50
  • 71
  • 6
    Duplicate? [Open WebView URL in Browser](http://stackoverflow.com/q/31033782/2415822) – JAL Jan 21 '16 at 20:35

1 Answers1

1

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.

So Over It
  • 3,668
  • 3
  • 35
  • 46
  • Use, this is what I'm asking. Using this code causes links to open as soon as you run the application, it doesn't wait until a user clicks on the link. I run the application and opens the links automatically. – orange Jan 17 '16 at 16:52
  • I just tried this again with my provided code and it seems to work fine. Created a new Cocoa Application with storyboard. Added a single WebView to the storyboard in Interface Builder. Pasted the above code overtop of the ViewController code. Linked up the WebView to the ViewController. Ran the app. It only pages in browser when links the links were clicked in the WebView. Can you provide your actual code somewhere - and I'll look through it to see why you are getting a different result. – So Over It Jan 18 '16 at 16:54