[英]WkWebView in MacOS remains blank on local or remote content
I'm trying to build a MacOS app that can load websites and extract data from them using webviews, but I can't get webviews to load at all, either with remote content or with local content. 我正在尝试构建一个可以加载网站并使用webview从中提取数据的MacOS应用程序,但我无法使用远程内容或本地内容来加载webview。
I'm trying to use a very small webview embedded into the main view. 我正在尝试使用嵌入主视图的非常小的webview。 (Ultimately, I don't actually want this to be displayed, or I want it to be as small as possible---I just want the rendered content to extract data.) Like this:
(最终,我实际上并不想要显示它,或者我希望它尽可能小 - 我只是想让渲染的内容提取数据。)像这样:
However, neither loading from a remote url nor loading from a local test html string works. 但是,既不从远程URL加载也不从本地测试html字符串加载。 When I click either the load-from-remote-url button or the load-from-local-test-string button, the print statement I put in there to make sure the function gets called is printed, but there's no change at all to the webview: it just remains a blank white box.
当我单击load-from-remote-url按钮或load-from-local-test-string按钮时,我在那里放置了print语句以确保函数被调用,但是根本没有变化webview:它只是一个空白的白框。 It also doesn't throw any kind of error.
它也不会抛出任何错误。
Here's the failing code: 这是失败的代码:
webViewTools.swift: webViewTools.swift:
import Foundation
import WebKit
extension WKWebView {
func load(_ urlString: String) {
guard let url = URL(string: urlString) else {
print("can't make url")
return
}
let request = URLRequest(url: url)
load(request)
}
}
let testHtml = "<html><body><h1>Hello World</h1></body></html>"
extension WKWebView {
func test(){
loadHTMLString(testHtml, baseURL: nil)
}
}
ViewController.swift: ViewController.swift:
import Cocoa
import WebKit
class ViewController: NSViewController {
@IBOutlet var webView: WKWebView!
@IBAction func loadButtonPressed(_ sender: Any) {
webView.load("https://www.apple.com/")
print("trying to load from remote")
}
@IBAction func linkButtonPressed(_ sender: Any) {
webView.test()
print("trying to load local string")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
Note: the sandbox is not enabled, and I've added "Allow Arbitrary Loads" to the "App Transport Security Settings" entry in info.plist, so this prior SO does not resolve the problem. 注意:沙箱未启用,我已将“允许任意负载”添加到info.plist中的“App Transport Security Settings”条目,因此此先前的SO无法解决问题。
How do I convince the WKWebView to actually, you know, display something? 我怎么说服WKWebView实际上,你知道,显示一些东西? (MacOS 10.14.5, Swift 5, XCode 10.2.1)
(MacOS 10.14.5,Swift 5,XCode 10.2.1)
Aaaah, I discovered the problem: apparently even if you don't turn the sandbox on, WKWebView totally fails without it. Aaaah,我发现了这个问题:显然即使你没有打开沙盒,WKWebView也会在没有它的情况下完全失败。 Not only that, but it fails silently.
不仅如此,它还是默默地失败了。 When I turned the sandbox on, and then enabled outgoing connections, everything started working right away.
当我打开沙箱,然后启用传出连接时,一切都立即开始工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.