UIWebViewDelegate不监控XMLHtt prequest? [英] UIWebViewDelegate not monitoring XMLHttpRequest?

查看:329
本文介绍了UIWebViewDelegate不监控XMLHtt prequest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是真的,该UIWebViewDelegate不监视使用XMLHtt prequest提出的要求?如果是这样,有没有办法来监控这类要求?

例如。 UIWebViewDelegate没有在抓住这个 - (BOOL)web视图:(KMWebView *)web视图shouldStartLoadWithRequest:(NSMutableURLRequest *)要求navigationType:(UIWebViewNavigationType)navigationType ;

  VAR XHR =新XMLHtt prequest();
xhr.open(GET,http://www.google.com,真正的);

xhr.onreadystatechange =功能()
{
    如果(xhr.readyState == 4)
    {
        警报(xhr.responseText);
    }
}

xhr.send();
 

解决方案

有趣的问题。

有两个部分,使这项工作:JavaScript处理程序和UIWebView的委托方法。在JavaScript中,我们可以通过修改原型的方法是创建一个AJAX请求时触发事件。随着我们的UIWebView的委托,我们能够捕捉到这些事件。

JavaScript处理

我们需要在一个AJAX请求时得到通知。我找到了解决办法这里

在我们的例子中,使code的工作,我把下面的JavaScript在一个叫做资源 ajax_handler.js 这是捆绑在我的应用程序。

  VAR s_ajaxListener =新的对象();
s_ajaxListener.tempOpen = XMLHtt prequest.prototype.open;
s_ajaxListener.tempSend = XMLHtt prequest.prototype.send;
s_ajaxListener.callback =功能(){
    了window.location ='mpAjaxHandler://+ this.url;
};

XMLHtt prequest.prototype.open =功能(A,B){
  (!a)如VAR一个='';
  (!b)如变种B ='';
  s_ajaxListener.tempOpen.apply(这一点,参数);
  s_ajaxListener.method =一个;
  s_ajaxListener.url = B;
  如果(a.toLowerCase()=='得到'){
    s_ajaxListener.data = b.split('?');
    s_ajaxListener.data = s_ajaxListener.data [1];
  }
}

XMLHtt prequest.prototype.send =功能(A,B){
  (!a)如VAR一个='';
  (!b)如变种B ='';
  s_ajaxListener.tempSend.apply(这一点,参数);
  如果(s_ajaxListener.method.toLowerCase()=='后')s_ajaxListener.data =一个;
  s_ajaxListener.callback();
}
 

什么这实际上会做的是改变浏览器的位置,一些由URL方案(在这种情况下, mpAjaxHandle )有关发出请求信息。别担心,我们的代表通过抓这和位置不会改变。

UIWebView的代表

首先,我们需要阅读我们的JavaScript文件。我建议做存储在一个静态变量。我在使用+初始化的习惯。

 静态的NSString * JSHandler;

+(无效)初始化{
    JSHandler = [[NSString的stringWithContentsOfURL:[一个NSBundle mainBundle] URLForResource:@ajax_handlerwithExtension:@JS]编码:NSUTF8StringEncoding错误:无]保留]。
}
 

接下来,我们要注入这段JavaScript之前,页面加载完成,所以我们可以接收所有事件。

   - (无效)webViewDidStartLoad:(UIWebView的*)web视图{
    [web视图stringByEvaluatingJavaScriptFromString:JSHandler]。
}
 

最后,我们想捕捉的事件。

由于URL方案是编造出来的,我们不希望实际遵循它。回到,然后一切都很好。

 的#define CocoaJSHandler @mpAjaxHandler

 - (BOOL)web视图:(UIWebView的*)web视图shouldStartLoadWithRequest:(的NSURLRequest *)要求navigationType:(UIWebViewNavigationType)navigationType {
    如果([[[请求URL]模式]的isEqual:CocoaJSHandler]){
        的NSString * requestedURLString = [[[请求URL] absoluteString] substringFromIndex:[CocoaJSHandler长度] + 3]。

        的NSLog(@Ajax请求:%@,requestedURLString);
        返回NO;
    }

    返回YES;
}
 

我创建了解决方案的样本项目,但无处可承载它。你可以留言给我,如果你可以承载它,我就会相应地编辑这篇文章。

Is it true that the UIWebViewDelegate does not monitor requests made by using a XMLHttpRequest? If so, is there a way to monitor these kind of requests?

e.g. UIWebViewDelegate does not catch this in -(BOOL) webView:(KMWebView *)webView shouldStartLoadWithRequest:(NSMutableURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://www.google.com", true);

xhr.onreadystatechange=function() 
{
    if (xhr.readyState==4) 
    {
        alert(xhr.responseText);
    }
}

xhr.send();

解决方案

Interesting question.

There are two parts to make this work: a JavaScript handler and UIWebView delegate methods. In JavaScript, we can modify prototype methods to trigger events when an AJAX request is created. With our UIWebView delegate, we can capture these events.

JavaScript Handler

We need to be notified when an AJAX request is made. I found the solution here.

In our case, to make the code work, I put the following JavaScript in a resource called ajax_handler.js which is bundled with my app.

var s_ajaxListener = new Object();
s_ajaxListener.tempOpen = XMLHttpRequest.prototype.open;
s_ajaxListener.tempSend = XMLHttpRequest.prototype.send;
s_ajaxListener.callback = function () {
    window.location='mpAjaxHandler://' + this.url;
};

XMLHttpRequest.prototype.open = function(a,b) {
  if (!a) var a='';
  if (!b) var b='';
  s_ajaxListener.tempOpen.apply(this, arguments);
  s_ajaxListener.method = a;  
  s_ajaxListener.url = b;
  if (a.toLowerCase() == 'get') {
    s_ajaxListener.data = b.split('?');
    s_ajaxListener.data = s_ajaxListener.data[1];
  }
}

XMLHttpRequest.prototype.send = function(a,b) {
  if (!a) var a='';
  if (!b) var b='';
  s_ajaxListener.tempSend.apply(this, arguments);
  if(s_ajaxListener.method.toLowerCase() == 'post')s_ajaxListener.data = a;
  s_ajaxListener.callback();
}

What this will actually do is change the location of the browser to some made up URL scheme (in this case, mpAjaxHandle) with info about the request made. Don't worry, our delegate with catch this and the location won't change.

UIWebView Delegate

First, we need to read our JavaScript file. I suggest doing storing it in a static variable. I'm in the habit of using +initialize.

static NSString *JSHandler;

+ (void)initialize {
    JSHandler = [[NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"ajax_handler" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil] retain];
}

Next, we want to inject this JavaScript before a page is done loading so we can receive all events.

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [webView stringByEvaluatingJavaScriptFromString:JSHandler];
}

Finally, we want to capture the event.

Since the URL Scheme is made up, we don't want to actually follow it. We return NO and all is well.

#define CocoaJSHandler          @"mpAjaxHandler"

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if ([[[request URL] scheme] isEqual:CocoaJSHandler]) {
        NSString *requestedURLString = [[[request URL] absoluteString] substringFromIndex:[CocoaJSHandler length] + 3];

        NSLog(@"ajax request: %@", requestedURLString);
        return NO;
    }

    return YES;
}

I created a sample project with the solution but have nowhere to host it. You can message me if you can host it and I'll edit this post accordingly.

这篇关于UIWebViewDelegate不监控XMLHtt prequest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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