在浏览器中打开 WebView URL
Open WebView URL in Browser
我制作了一个非常简单的 Swift 应用程序,用于加载带有链接的网页。每当我单击链接时,它们都打不开。我如何获得加载的 .html 网页上的链接在浏览器中打开 window for OS X?
这是我的实现:
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.
}
}
}
首先,将您的 WebView
的策略委托和您的初始 URL 设置为 class 变量:
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))
}
然后,覆盖委托方法以拦截导航。
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
}
}
您还可以决定在 WebView 中打开哪些链接以及在浏览器中打开哪些链接,就像在您的 html 页面中编写目标属性一样简单
<a href="http://www.google.com/" target="_blank">external page</a>
并在上面提到的 decidePolicyForNewWindowAction 中使用目标检查。我已将完整答案放在 this question 主题中。希望你能自己翻译成swift。
我制作了一个非常简单的 Swift 应用程序,用于加载带有链接的网页。每当我单击链接时,它们都打不开。我如何获得加载的 .html 网页上的链接在浏览器中打开 window for OS X?
这是我的实现:
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.
}
}
}
首先,将您的 WebView
的策略委托和您的初始 URL 设置为 class 变量:
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))
}
然后,覆盖委托方法以拦截导航。
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
}
}
您还可以决定在 WebView 中打开哪些链接以及在浏览器中打开哪些链接,就像在您的 html 页面中编写目标属性一样简单
<a href="http://www.google.com/" target="_blank">external page</a>
并在上面提到的 decidePolicyForNewWindowAction 中使用目标检查。我已将完整答案放在 this question 主题中。希望你能自己翻译成swift。