0

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:

Screenshot of Error

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?

Won ixer
  • 21
  • 3
  • 4
    You can't expect to have an answer within an hour, that's not how it works. To answer your question, WKWebView does not create tabs for you. It's gonna be up to you to implement them, with some NSTabViewController I suppose. – Greg de J Jun 19 '20 at 15:48
  • I did add the tabs, how do I open the links with target blank in a new tab – Won ixer Jun 19 '20 at 16:05
  • Create a new view controller, assign it to the new tab, put a webview in it, and load the url you captured in `createWebViewWithConfiguration` in there. – Greg de J Jun 20 '20 at 01:33
  • Can you provide me with some code? – Won ixer Jun 20 '20 at 15:43
  • NVM, I'll ask a new question – Won ixer Jun 20 '20 at 15:45

0 Answers0