I always use an approach in which the application translates to a special URL:
window.location = "myapp://somemessage/someargument";
And when the application catches this in the following function:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSURL *url = [request URL]; if ( [[url scheme] isEqualToString:@"myapp"] ) { [self handleJSEvent:url]; return NO; } return YES; }
In addition, depending on what you need, you can use some kind of event queue that you retrieve using JSON.stringify(events) in response to a message sent to the application using the method described above. For communication from an application with js, JSON is also very suitable.
If there is a standard or standard way to do this, I explicitly skipped it.
mvds
source share