Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to call native all iOS functions from UIWebView using JavaScriptCore and JSContext

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.

like image 445
jnix Avatar asked Apr 14 '14 16:04

jnix


1 Answers

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.

like image 71
TomSwift Avatar answered Oct 17 '22 20:10

TomSwift