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?