0

I have a UIWebView and one page has an AJAX component whose content changes when the user clicks a button. How can I know when the content has loaded inside the UIWebView?

rid
  • 61,078
  • 31
  • 152
  • 193
Samuli Lehtonen
  • 3,840
  • 5
  • 39
  • 49

1 Answers1

3

You'll need to do two things:

  1. In your JavaScript code, have the XMLHTTPRequest signal that the AJAX request is complete with a custom URL:

    var req = new XMLHttpRequest();  
    req.open('GET', 'http://www.mozilla.org/', true);  
    req.onreadystatechange = function (aEvt) {  
      if (req.readyState == 4) {  
         if(req.status == 200) { 
           window.location = "myspecialurl:foo"
         }
      }  
    };  
    
  2. In your native code, you'll need to implement a UIWebView delegate that listens for this custom URL:

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
    {
      if([[[request URL] absoluteString] isEqualToString:@"myspecialurl:foo"]) {
         // Your code for when the AJAX request completes.
         return NO;
      }
      return YES;
    }
    

No assurances this code parses/compiles but should give you the idea how to do it.

rid
  • 61,078
  • 31
  • 152
  • 193
Ishan
  • 1,346
  • 9
  • 11