问题描述
我正在从另一个打开 WKWebView,并希望两者能够通过 JavaScript 相互通信.
I'm opening a WKWebView from another, and would like the two to be able to communicate with each other via JavaScript.
以下代码打开两个 Web 视图 - 我可以使用 Safari 调试器检查它们.但是第二个 Web 视图的 opener
属性是 null
.
The following code opens two Web views - I can inspect them with the Safari debugger. But the opener
property of the second Web view is null
.
class MyVC: UIViewController {
override func viewDidLoad() {
...
// "webView" is the first web view. Open a second one...
webView = WKWebView()
webView.uiDelegate = self
webView.evaluateJavaScript("window.open('about:blank');")
}
}
extension MyVC : WKUIDelegate {
func webView(... createWebViewWith ...) -> WKWebView? {
let newWebView = WKWebView(frame: webView.bounds,
configuration: configuration)
newWebView.load(navigationAction.request)
let vc = UIViewController()
vc.view.addSubview(newWebView)
navigationController?.pushViewController(vc, animated: true)
return nil
}
}
有没有办法设置 opener 属性?
Is there a way to set the opener property?
推荐答案
window.opener
为 null,因为我从 createWebViewWith
方法返回 nil.我将其更改为返回新的 Web 视图,并将新窗口的 opener 属性设置为父"Web 视图.
The window.opener
was null because I was returning nil from the createWebViewWith
method. I changed it to return the new web view, and that sets the new window's opener property to "parent" web view.
func webView(... createWebViewWith ...) -> WKWebView? {
let newWebView = WKWebView(frame: webView.bounds,
...
return newWebView
}
这篇关于可以通过 opener 属性链接两个 WKWebViews 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!