2

I made a very simple Swift application that loads a webpage with links on it. Whenever I click the links, they do not open. How would I got about having the links on the loaded .html webpage open in a browser window for OS X?

Here is my implementation:

import Cocoa
import WebKit

class ViewController: NSViewController {

    @IBOutlet weak var webView: WebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let urlString = "URL"
        self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: urlString)!))
    }

    override var representedObject: AnyObject? {
        didSet {
            // Update the view, if already loaded.
        }
    }
}
JAL
  • 41,701
  • 23
  • 172
  • 300
  • It is impossible to know what is going on without more information. Please post your UIWebView code and any relevant error messages. – JAL Jun 24 '15 at 18:09
  • Sorry about that. I added it to the main post. –  Jun 24 '15 at 18:49
  • I just proposed up another edit to fix the tags, since [tag:uiwebview] should be used for iOS questions only. Sorry about the mix up. – JAL Jun 24 '15 at 20:46

2 Answers2

4

First, set your WebView's policy delegate and your initial URL as a class variable:

let url = NSURL(string: "http://www.google.com/")!

// ...

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    self.webView.policyDelegate = self

    self.webView.mainFrame.loadRequest(NSURLRequest(URL: self.url))
}

Then, override the delegate methods to intercept navigation.

override func webView(webView: WebView!, decidePolicyForNewWindowAction actionInformation: [NSObject : AnyObject]!, request: NSURLRequest!, newFrameName frameName: String!, decisionListener listener: WebPolicyDecisionListener!) {
    println(__LINE__) // the method is needed, the println is for debugging
}


override func webView(webView: WebView!, decidePolicyForNavigationAction actionInformation: [NSObject : AnyObject]!, request: NSURLRequest!, frame: WebFrame!, decisionListener listener: WebPolicyDecisionListener!) {
    if request.URL!.absoluteString == self.url.absoluteString { // load the initial page
        listener.use() // load the page in the app
    } else { // all other links
        NSWorkspace.sharedWorkspace().openURL(request.URL!) // take the user out of the app and into their default browser
    }
}
JAL
  • 41,701
  • 23
  • 172
  • 300
  • Thank you! That appeared to work. Can I use the same method for menu items as well for having them open in a browser? –  Jun 24 '15 at 19:55
  • Do you mean for options under `File`, `Edit`, ...? Yes, just use `NSWorkspace.sharedWorkspace().openURL(someURL)` – JAL Jun 24 '15 at 20:03
0

Also you can decide what links to open in WebView and what - in browser as easy as to write target attribute in your html page like

<a href="http://www.google.com/" target="_blank">external page</a>

And use target check in the decidePolicyForNewWindowAction, menthioned above. I've placed the full answer in this question thread. Hope you can translate it to swift yourself.

DeadlineX
  • 129
  • 1
  • 4