知道何时在 UIWebView 中加载了 AJAX [英] Knowing when AJAX has loaded in UIWebView

查看:24
本文介绍了知道何时在 UIWebView 中加载了 AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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?

推荐答案

你需要做两件事:

  1. 在您的 JavaScript 代码中,使用 XMLHTTPRequest 信号表明 AJAX 请求已通过自定义 URL 完成:

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

  • 在您的本机代码中,您需要实现一个 UIWebView 委托来侦听此自定义 URL:

  • 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.

    这篇关于知道何时在 UIWebView 中加载了 AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆