简体   繁体   中英

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.

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.

We all know that the only official way to call into a UIWebView from ObjectiveC is via 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. 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”.

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. 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

JSContext *ctx = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

Approach 2: Get the JSContext via WebKit's -didCreateJavaScriptContext delegate callback.

Please find the reference link below Why use JavaScriptCore in iOS7 if it can't access a UIWebView's runtime? 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM