当AJAX在UIWebView中加载知道

人气:508 发布:2022-09-11 标签: ajax ios cocoa-touch uiwebview

问题描述

我有一个的UIWebView ,一个页面有一个AJAX组件,其含量的变化,当用户点击一个按钮。我怎么能知道,当内容已经内加载了的UIWebView

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?

推荐答案

您需要做两件事情:

在你的JavaScript code,有 XMLHTT prequest 的信号,即AJAX请求完成了自定义网址:

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"
     }
  }  
};  

在您的本地code,你需要实现一个的UIWebView 委托监听这个自定义网址:

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;
}

没有保证,这code解析/编译但应该给你的想法如何做到这一点。

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

160