I have an IOS application which has a web browser in it.
When this web browser is displayed, it navigates to a page on Internet, say http://myexample.com/myiosapp.
Is it possible to place a javascript code on this web site, which, when downloaded to phone, communicates with the native application? Can it call an Objective-C method, or write something to storage space of this application?
Ex.
Code hosted on myexample.com/myios app:
function saveSomeString() {
xcode.Save("somestring");
}
should call an Objective-C function named Save, which saves incoming string parameter to local storage.
Yes, you can, but it is a bit hacky.
On the Objective-C side:
Basically, in the UIWebView, you will use method shouldStartLoadWithRequest (from the UIWebViewDelegate) which is called before a URL request starts:
On Javascript side:
Your javascript code will navigate to a ‘fake’ URL that you will filter on the method above. Something like:
Back on the Objective-C side:
Inside the method I mentioned earlier, you look for the keyword js2objc (or whichever you choose) and then extract the info and take any appropriate action: