Cocoa: Open the links with Target_blank in a new tabs? WKWebview

陌路散爱 提交于 2020-06-29 06:41:16

问题


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?

来源:https://stackoverflow.com/questions/62472239/cocoa-open-the-links-with-target-blank-in-a-new-tabs-wkwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!