【发布时间】:2017-10-07 02:41:10
【问题描述】:
在我的 iOS 应用程序中,我有 2 个WKWebView 控件来模拟我们的网络应用程序中 2 个选项卡/窗口的父/子关系,以便我们可以重用所有这些 javascript。问题是这 2 个WKWebViews 之间没有通用的通信通道。
网页架构:
- 第一个/主窗口加载我们的内部 javascript 函数,并在第二个/子窗口中打开链接。
- 第二个/子窗口加载第三方网页内容。
- 第三方内容使用javascript表单源窗口与我们的系统通信(使用window.opener / window.parent API桥)。
现在,在本机应用程序中,我正在这样做以重用现有的 javascript 并模拟 Web 架构 -
一个。下面是 1st WKWebView 中网页的 js 调用 -
window.open("http://localhost/child.html", "myWindow", "width=200,height=100");
b. WKUIDelegate 拦截此调用 (a) 并打开 2nd 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;
}
c。在网页/电脑浏览器中,网页应用的子窗口可以使用window.opener和window.parent访问源js函数
如何在WKWebViews 中实现同样的功能?换句话说,第二个WKWebView中的js函数可以从父/源WKWebView调用js函数?
【问题讨论】:
标签: javascript ios objective-c wkwebview window.opener