6

Let me explain my question it is bugging me from long time. Any help will be appreciated. Please help.

I am working on hybrid kind of app in iOS. I am using UIWebView for loading webpages from http://myserver.com (its example its not actual URL) I have hosted multiple webpages on this server.

I am using "JSContext" to communicate between javascript (webpages loaded from my server) in UIWebView and native iOS functions.

I have followed procedure given at UIWebView JavaScript losing reference to iOS JSContext namespace (object) the context was getting lost but I have added workaround provided in solution of this question. Event then for some frame load inside UIWebView my iOS context is getting vanished. After following following steps context gets vanished.

  1. I load page1 from myserver in UIWebView. Almost all works well all functions OS native iOS are called from javascript.
  2. Now when second page2 is loaded as result of interaction in page1, in same UIWebView javascript context gets vanished.

There is one more magical thing. if I have directly load page2 in UIWebView instead of indirect loading (page1->page2). All works well all native methods are called from UIwebView as expected. Please help. If you want more info please ask.

3
  • Is it duplicate for stackoverflow.com/questions/21714365/… ? Commented Apr 25, 2014 at 19:07
  • This is the same thing happening on stackoverflow.com/questions/21714365/…. When a new page is loaded, the new page gets a new JSContext. You mentioned a frame load. Does the frame have the JSContext set correctly? Commented Apr 28, 2014 at 2:11
  • When some part of page loads without calling webViewDidFinishedloading. I can not set JSContext for such request. Commented Apr 28, 2014 at 8:14

1 Answer 1

2
+150

First of all, stealing and using the UIWebView's JavaScriptContext in this way is likely a path to app rejection.

But if this is just a science project...

You might try another technique for obtaining the JavaScriptContext. I present a solution in this GitHub project:

https://github.com/TomSwift/UIWebView-TS_JavaScriptContext

Basically I'm using some knowledge of how WebKit works to implement a category on NSObject to intercept some non-handeled WebKit delegate methods which are called whenever a new JavaScriptContext is created for a frame.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your answer but - (void)webView:(UIWebView *)webView didCreateJavaScriptContext:(JSContext *)ctx { ctx[@"myinterface"] = self; or ctx[@"window.myinterface"] = self; } And my html calling window.myinterface.mynativefunc('hello'); not getting called at all.Please help.
example app worked but still having issue as specified by jnix.
When some part of page loads without calling webViewDidFinishedloading. I can not set JSContext for such request.
@TomSwift do you know solution ? I am still facing same problem .please help.
Sorry; without some more information it's pretty difficult to guess what's going on. If it works in the example app then you need to figure out what's different in your code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.