UIWebView Javascript窗口到窗口通信 [英] UIWebView Javascript Window to Window Communication

查看:79
本文介绍了UIWebView Javascript窗口到窗口通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个iOS应用,我正在尝试使用 UIWebView 来显示各种网站。最近我完成了将Javascript注入 UIWebView 的逻辑,以捕获 window.open 的实例, window.close window.opener.focus 。简而言之,为此,我注入了JS,它覆盖了前面提到的JS函数,其中包括使用特定的方案创建 iframe ,我可以在应用程序的<$ c $中捕获c> webView:shouldStartLoadWithRequest:navigationType 方法。这一切现在都可以,包括 window.open 创建一个新的 UIWebView 而不是在同一个窗口中加载。

I'm working on an iOS app in which I'm trying to use UIWebView to display a variety of websites. Recently I finished logic to inject Javascript into the UIWebView to catch instances of window.open, window.close, and window.opener.focus. In short, to do so, I inject JS that overrides the aforementioned JS functions, which includes creating an iframe with a specific scheme that I can catch in the app's webView:shouldStartLoadWithRequest:navigationType method. This is all working OK for now, including window.open creating a new UIWebView rather than loading in the same window.

现在,问题已出现在Windows之间没有可行的JS通信解决方案。如果子窗口试图调用 window.opener window.parent ,它总是返回一个空值,并且因此,它无法与原始网络视图进行通信。

Now though, the issue has come up where there's no feasible solution for JS communication between windows. If the child window tries to call to window.opener or window.parent, it's always returning a null value, and thus, it can't communicate back to the original web view.

为了了解哪些iOS浏览器能够有效地执行窗口到窗口的通信,我我发现在iPhone上的9个浏览器中,只有Safari能够成功地有效地执行此通信。这让我相信 UIWebView 会阻止完整的JS窗口到窗口的通信。

In an effort to see what iOS browsers are able to effectively perform window-to-window communication, I found that of the 9 browsers I have on my iPhone, only Safari was able to effectively perform this communication successfully. This leads me to believe that there's something with UIWebView that prevents full JS window-to-window communication from being possible.

有没有人能够让UIWebView与所有JS逻辑完全集成,即窗口到窗口的通信?或者证明JS窗口到窗口的通信是不可能的?任何方向或建议表示赞赏。谢谢!

Has anyone had any success with getting UIWebView to fully integrate with all JS logic, namely window-to-window communication? Or have proof that JS window-to-window communication isn't possible? Any direction or advice is appreciated. Thanks!

推荐答案

找到可能的解决方案。

添加JavaScriptCore.framework到Linked Frameworks和你的webViewDidFinishLoad:

Add JavaScriptCore.framework to Linked Frameworks and in your webViewDidFinishLoad:

JSContext *parentCtx = [self.parentWebView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
JSContext *childCtx = [self.childWebView  valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
childCtx[@"window"][@"opener"] = parentCtx[@"window"];

现在从childWebView调用window.opener.test()时,它将在parentWebView中触发测试函数!

Now when you call window.opener.test() from childWebView, it will fire test function in parentWebView!

我不确定私有API。
仅适用于iOS 7

I'm not sure about private API. Works on iOS 7 only

这篇关于UIWebView Javascript窗口到窗口通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆