如何在WebView上启用默认浏览器中打开的链接[复制]

时间:2023-01-25 07:38:02

This question already has an answer here:

这个问题在这里已有答案:

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:

我正在学习OS X / Swift开发,并且已经加载了包含指向其他网站的链接的网页,我如何在默认浏览器中打开这些链接。在点击链接时,根本没有任何事情发生。这是我的ViewController.swift内容:

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


}

1 个解决方案

#1


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)?

我想你问的是如何在桌面浏览器(例如Safari)中打开WebView中点击链接?

If this is what you are trying to achieve, you can use the WebPolicyDelegate to determine where a URL should open.

如果这是您要实现的目标,则可以使用WebPolicyDelegate确定URL应该打开的位置。

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.

如果这不是您要求的,请您编辑您的问题以使其更清晰。

#1


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)?

我想你问的是如何在桌面浏览器(例如Safari)中打开WebView中点击链接?

If this is what you are trying to achieve, you can use the WebPolicyDelegate to determine where a URL should open.

如果这是您要实现的目标,则可以使用WebPolicyDelegate确定URL应该打开的位置。

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.

如果这不是您要求的,请您编辑您的问题以使其更清晰。