JavaScriptCore and iOS in iOS7

Why use JavaScriptCore in iOS7 if it can't access a UIWebView's runtime?


my app have a demand,Need to interact with the JavaScriptCore ,JavaScriptCore need client to request data and The data is returned to the JavaScriptCore,JavaScriptCore get the data before rendering the webview;

"

self.context=[self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

self.context.name = @"text";

self.appClient.delegate = self;

self.context[@"appClient"] = self.appClient;

"

Metaphor, has three data request, the request completely before rendering interface, the callback unravelled, how to do

Quick return information!!!thanks

The callback compelling is performed only once

Accepted Answer

UIWebView does not give you access to its underlying JavaScriptCore context. This code:

[self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

is not supported because it accesses private properties of the UIWebView object (if you look at the

<UIKit/UIWebView.h>
header, you’ll find no mention of a
documentView
property). Given that, the rest of your question falls into ‘private APIs don’t always work as expected’ category.

What’s your high-level goal here? If you’re trying to communicate between the JavaScript code running in your web view and the native code running the rest of your app, there are well-known techniques for doing that.

Also, why are you using UIWebView rather than WKWebView. In addition to it being a more modern and generally recommended way to host web content in a native app, WKWebView has much better facilities for JavaScript/native integration.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Excuse me

My high-level goal is UiWebview in rendering and js interaction!Why use JavaScriptCore in iOS7 if it can't access a UIWebView's runtime?

Excuse me, I met a maddening problem with my app needs to interact and H5. When we - after injection of JS code (void) viewDidLoad, if a page redirection happens, this time JS web page has changed, and - (void) viewDidLoad method will only be executed once, it is no longer before us JS injection over those, and then re-call the local method naturally becomes ineffective.

If we - (void) webViewDidFinishLoad: (UIWebView) webView inject the JS, it seems, can solve the problem call failed after a redirect, because every time you load it completes webView callback - (void) webViewDidFinishLoad: webView (UIWebView) , that is to say after each redirection, as long as the page loads, JS code that will be re-injected. If JS method call OC timing is after the page loads, such as clicking the button on the web interface or manually by the user triggers an event call OC code, this situation must be completed after the web page load will occur, but this time we have re-injected JS, so no problem at all. However, if the JS method call OC timing just happened in the page loading process it? For example, web interface loading process automatically perform some actions need to call the OC code, but this time - (void) webViewDidFinishLoad: (UIWebView *) webView yet callback, so we JS code is not refilled, there is still cause problems fail

Why use JavaScriptCore in iOS7 if it can't access a UIWebView's runtime?

I’m not sure if you’re asking that as a literal question, or for me to comment on the linked-to thread. If it’s a literal question, the answer is simple: there are lots of reasons to use JavaScriptCore that are completely unrelated to the web. For example, CFNetwork needs to run JavaScript to interpret PAC files.

If you’re asking me to comment on the linked-to thread, I already have: UIWebView does not give you access to its underlying JavaScriptCore context.

My high-level goal is UiWebview in rendering and js interaction?

Have you looked at WKWebView? If web/native integration is important to you, WKWebView is the way forward.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I know some wkwebview framework, if I do so I will make a large amount of work, and that is how do wkwebview framework after iOS8 only that the previous version?

I know some wkwebview framework, if I do so I will make a large amount of work

Right, but that work puts you on a path that’s both supported and recommended. OTOH, sticking with UIWebView gives you a choice:

  • you can try to get the KVC approach working — The drawbacks here are that it’s unsupported and is considered private API, and thus could see you rejected by App Review.

  • you can use a supported UIWebView technique — These are super ugly, typically involving either UIWebView’s URL opening delegate callback or an NSURLProtocol subclass.

Hence my recommendation that you go with WKWebView.

and that is how do wkwebview framework after iOS8 only that the previous version?

Right, but I’m sure you’re aware that the percentage of users still running pre-iOS 8 is tiny, right? Our current information indicates that it’s less than 3%.

after injection of JS code

(void) viewDidLoad
, if a page redirection happens, this time JS web page has changed, and
- (void) viewDidLoad
method will only be executed once, it is no longer before us JS injection over those, and then re-call the local method naturally becomes ineffective.

Yep. This is something else that WKWebView makes much easier (check out WKUserScript).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Should probably also point out that Xcode 8 has dropped support for deploying OS7 applications. (OS8 is the minimum deployment target now).

JavaScriptCore and iOS in iOS7
 
 
Q