在我的iOS应用程序中,我有2 WKWebView
控件来模拟我们的Web应用程序中2选项卡/窗口的父/子关系,以便我们可以重用所有这些JavaScript。问题在于这些2 WKWebView
之间没有通用的通信通道。从源WKWebView(window.opener)访问javascripts函数
Web架构:
- 第一个/主窗口中加载我们内部的JavaScript函数与第二/子窗口中打开链接。
- 第二个/子窗口加载第三方网页内容。
- 第三方内容使用javascript窗体源窗口(使用window.opener/window.parent API桥)与我们的系统进行通信。
现在,在本机应用程序,这是我在做什么重用现有的JavaScript和模拟网络杜彦武 -
一个。下面从网页中JS调用1 WKWebView -
window.open("http://localhost/child.html", "myWindow", "width=200,height=100");
湾该WKUIDelegate拦截此调用(a)和打开第二WKWebView -
- (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
// Here, window.open js calls are handled by loading the request in another/new web view
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
WebViewController *webViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"WebViewController"];
webViewController.loadURLString = navigationAction.request.URL.absoluteString;
// Show controller without interfering the current flow of API call, thus dispatch after a fraction of second
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.navigationController pushViewController:webViewController animated:YES];
});
return nil;
}
℃。在网页/电脑浏览器中,网页应用程序的子窗口可以使用window.opener
和window.parent
如何在WKWebView
中实现相同的功能?换句话说,第二个WKWebView
中的js函数可以从父/源WKWebView
调用js函数?
好,这看起来像一个有希望的答案。兴奋。今天就来试试吧。 – Ashok