Reputation: 1716
One of my projects has a web view which is used to host a HTML5 web game (included in the app bundle). I've run into a curious issue. The game communicates with the hosting app via open URL calls (as detailed in the following question: Is is possible to communicate between UINavigationController and web page loaded inside UIWebView?). I over-ride the following method in the web view's delegate:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *requestURL = [request URL];
NSString *host = [requestURL host];
if([host isEqualToString: @"printHello"])
{
NSLog(@"Hello!");
return NO;
}
return YES;
}
This works fine for the first call (such as document.location(my-app-scheme://printHello)), then Hello! is indeed logged into the application's running console. However, attempting to immediately call a second load URL of exactly the same thing is ignored.
I've worked around this by making sure any communication between the webview and the app happens only in single calls, but this is proving to be quite restrictive - is there a better strategy to fix this?
Upvotes: 3
Views: 410
Reputation: 49054
When I've done this kind of communication via URLs, I had similar problems with using document.location("someURL")
. Instead, I've started doing this:
var iframe = document.createElement("iframe");
iframe.setAttribute("src", "my-app-scheme://doSomething/");
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
This creates a new iframe which will load the url you specified, and then immediately removes it. This is long enough to cause the message to be passed, but since each URL is opening in a new 'document', it never gets confused about the changing URL it's supposed to be loading.
Give it a try, anyway.
Upvotes: 2