[英]Will using the JSContext from a UIWebview get my app rejcted?
We have a requirement in our app wherein we have to communicate across the web/native boundary - between JavaScript running in a UIWebView
and ObjectiveC in the app. 我们的应用程序中有一个要求,我们必须跨Web /本地边界进行通信 - 在
UIWebView
运行的JavaScript和应用程序中的ObjectiveC之间。
The web component communicates internally via pagebus(a subscription and publishing mechanism) and the requirement is that the native component should also be capable enough to subscribe for those events and listen to them. Web组件通过pagebus(订阅和发布机制)进行内部通信,并且要求本机组件也应该足够能够订阅这些事件并监听它们。
We all know that the only official way to call into a UIWebView
from ObjectiveC is via stringByEvaluatingJavaScriptFromString
. 我们都知道从ObjectiveC调用
UIWebView
的唯一官方方法是通过stringByEvaluatingJavaScriptFromString
。 And the typical way to call out from JavaScript is some manner of setting window.location to trigger a shouldStartLoadWithRequest:
callback on the UIWebView
delegate. 从JavaScript调用的典型方法是设置window.location以触发
UIWebView
委托上的shouldStartLoadWithRequest:
callback。 But this will be not useful to handle page bus events. 但这对处理页面总线事件没有用。
Apple gives us a public JavaScriptCore framework (part of WebKit) in iOS7, and JavaScriptCore provides simple mechanisms to proxy objects and methods between ObjectiveC and the JavaScript “context”. Apple在iOS7中为我们提供了一个公共JavaScriptCore框架(WebKit的一部分),而JavaScriptCore提供了简单的机制来代理ObjectiveC和JavaScript“上下文”之间的对象和方法。
Unfortunately, to make use of this framework in achieving our objective, we need the JSContext object of the webview, but there is no mechanism to achieve it. 不幸的是,为了利用这个框架来实现我们的目标,我们需要webview的JSContext对象,但是没有实现它的机制。 There is a stack overflow link where we found there are two approaches to achieve this (both undocumented).
有一个堆栈溢出链接,我们发现有两种方法可以实现这一点(两者都没有记录)。
Approach 1: Get the JSContext via KVC 方法1:通过KVC获取JSContext
JSContext *ctx = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
Approach 2: Get the JSContext via WebKit's -didCreateJavaScriptContext
delegate callback. 方法2:通过WebKit的
-didCreateJavaScriptContext
委托回调获取JSContext。
Please find the reference link below Why use JavaScriptCore in iOS7 if it can't access a UIWebView's runtime? 请在下面找到参考链接为什么在iOS7中使用JavaScriptCore,如果它无法访问UIWebView的运行时? The question is can we use either of these methods in our app, so that our app won't get rejected in App Store Or is there any other mechanism to achieve our objective of listening to pagebus events?
问题是我们可以在我们的应用程序中使用这些方法中的任何一种,以便我们的应用程序不会在App Store中被拒绝或者是否有任何其他机制来实现我们监听pagebus事件的目标?
I don't think apple will rejected your app. 我不认为苹果会拒绝你的应用程序。 I use kvc to get jscontext from uiwebview in my app and it pass the app review.
我在我的应用程序中使用kvc从uiwebview获取jscontext并通过了应用程序审核。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.