I frequently use window.postMessage and “message” event listeners to communicate across domains between a parent webpage and the webpages in its child IFRAME elements. I would like to know if this is possible with a UIWebView and webpages in its child IFRAME elements.
I frequently use window.postMessage and message event listeners to communicate across domains between a
Share
Yes this is indeed possible. I ran a test on this a couple of days ago in my own app and was successful.
On the webpage I wanted to communicate with, I simply added a “message” event listener to the window
Listening for UIWebView messages on the webpage
UIWebView just needs to grab the embedded iFrame object’s window and call postMessage on it:
For the reverse, the html implementation in the UIWebView needs to listen for messages also like in the code above. This time e.origin will be the domain of the webpage.
Webpage just needs to call the following with * as targetDomain as UIWebView will not have one.