3

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?

Community
  • 1
  • 1
David Doyle
  • 1,716
  • 10
  • 23
  • “However, attempting to immediately call a second load URL of exactly the same thing is ignored.” What if you do `my-app-scheme://printHello`, then `my-app-scheme://danceAJig`, then `my-app-scheme://printHello`? – Peter Hosey May 11 '13 at 02:41
  • Seemingly, any multiple of different calls gets ignored within a certain time frame - my particular app is using a "pause" message followed immediately by a "logMessage" message to detail why the pause was called. The logMessage is thrown away. A short (usually around 3 - 4 seconds) time later, a third "unpause" message is returned and intercepted correctly. – David Doyle May 13 '13 at 09:17
  • That probably blows my theory, then, which was that history was refusing to launch the “current” (last launched) URL again. +1 and I hope somebody has an answer for you. – Peter Hosey May 13 '13 at 13:38

1 Answers1

2

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.

BJ Homer
  • 48,806
  • 11
  • 116
  • 129