博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]UIWebView 监控 XMLHttpRequest
阅读量:5157 次
发布时间:2019-06-13

本文共 2976 字,大约阅读时间需要 9 分钟。

需要 Javascript 与 ObjC 合作从而达到这个目标。

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 .

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.

 

转载于:https://www.cnblogs.com/Proteas/archive/2013/01/07/2848932.html

你可能感兴趣的文章
javascript笔记---貌似大叔
查看>>
去重查询表mysql 中数据
查看>>
工厂模式
查看>>
AngularJS学习之旅—AngularJS 模块(十五)
查看>>
计算机网络基础知识
查看>>
大数据算法:对5亿数据进行排序
查看>>
BZOJ4372: 烁烁的游戏【动态点分治】
查看>>
C#里如何遍历枚举所有的项
查看>>
FPGA的上电复位
查看>>
工作那些事(三十一)怎样带好一个项目团队
查看>>
如何在键盘出现时滚动表格,以适应输入框的显示
查看>>
超级强大的鼠标手势工具
查看>>
常用Dockerfile举例
查看>>
Python的安装部署
查看>>
jquery的ajax用法
查看>>
设计模式-策略模式(Strategy)
查看>>
关于CALayer导致的crash问题
查看>>
sqoop导出数据|Hive|HDFS和脚本编写
查看>>
关于vue中watch和computed
查看>>
django orm 数据查询详解
查看>>