知道AJAX何时加载到UIWebView中

我有一个UIWebView ,一个页面有一个AJAX组件,当用户单击按钮时,其内容会发生变化。 如何在UIWebView加载内容?

你需要做两件事:

  1. 在您的JavaScript代码中,使用自定义URL获取AJAX请求完成的XMLHTTPRequest信号:

     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. 在您的本机代码中,您需要实现一个侦听此自定义URL的UIWebView委托:

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

不保证此代码会解析/编译,但应该让您了解如何执行此操作。