I created a cocoa web browser application. I was testing out my browser and I couldn’t open the links with target_blank
. So I found a question on stack overflow. I used that answer and tested it out and I could open the links. But, the links would open in the same web page. I want it to open in a new tab. So I found this web page that tells me how to add nswindow tabs to my application I added tabs to my cocoa browser.
Here are the codes I added
I used the code below to create tabs in my window controller:
@IBAction override func newWindowForTab(_ sender: Any?) {
let newWindowController = self.storyboard!.instantiateInitialController() as! WindowController
let newWindow = newWindowController.window!
newWindow.windowController = self
self.window!.addTabbedWindow(newWindow, ordered: .above)
}
This is the code in my ViewController that opens the links with target blank:
//It opens in the same web page
func webView(webView: WKWebView!, createWebViewWithConfiguration configuration: WKWebViewConfiguration!, forNavigationAction navigationAction: WKNavigationAction!, windowFeatures: WKWindowFeatures!) -> WKWebView! {
if navigationAction.targetFrame == nil {
webView.loadRequest(navigationAction.request)
}
return nil
}
I thought of changing the code above into this:
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
newWindowForTab(webView.load(navigationAction.request))
return nil
}
But then I get this error saying:
Cannot convert return expression of type 'Void' to return type 'WKWebView?'
Can you please help me with opening links in new tabs instead of the same page?