UIWebView Javascript window for window

I am working on an iOS application in which I am trying to use UIWebView to display various websites. I recently finished the logic to insert Javascript in a UIWebView to catch instances of window.open , window.close and window.opener.focus . In short, for this I am adding JS, which overrides the aforementioned JS functions, which includes creating an iframe with a special scheme that I can catch in the webView:shouldStartLoadWithRequest:navigationType application. Now everything works fine, including window.open , creating a new UIWebView , rather than loading in the same window.

Now, however, a problem has arisen where there is no acceptable solution for JS communication between windows. If the child window tries to call window.opener or window.parent , it always returns a null value and therefore it cannot return to the original web view.

In an attempt to see that iOS browsers can effectively communicate between windows, I found that of the nine browsers that I have on my iPhone, only Safari was able to efficiently execute this message. This makes me think that there is something with the UIWebView that prevents possible full communication between the JS window.

Has anyone been successful in getting a UIWebView for full integration with all JS logic, namely between window-window dialog? Or do you have evidence that JS communication between windows is not possible? Any direction or advice is welcome. Thanks!

+4
source share
2 answers

Possible solution found.

Add JavaScriptCore.framework in the Linked Framework and in your webViewDidFinishLoad:

 JSContext *parentCtx = [self.parentWebView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; JSContext *childCtx = [self.childWebView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; childCtx[@"window"][@"opener"] = parentCtx[@"window"]; 

Now when you call window.opener.test () from childWebView, it runs a test function in parentWebView!

I am not sure about the private API. Only works with iOS 7

+1
source

Quick version

  import JavaScriptCore let jsContextA = webA.valueForKeyPath("documentView.webView.mainFrame.javaScriptContext") let jsContextB = webB.valueForKeyPath("documentView.webView.mainFrame.javaScriptContext") //Original objc code : jsContextB[@"window"][@"opener"] = jsContextA[@"window"]; jsContextB!.setObject("opener", forKeyedSubscript: "window") jsContextB!.setObject(jsContextA!.objectForKeyedSubscript("window"), forKeyedSubscript: "opener") 
0
source

All Articles