Knowing when AJAX has loaded in UIWebView

隐身守侯 提交于 2019-12-01 14:57:39
Ishan

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!