收听UIWebView的所有请求 [英] Listen to all requests from UIWebView

查看:124
本文介绍了收听UIWebView的所有请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以拦截来自UIWebView的初始加载请求:

I can intercept the initial load request from a UIWebView with:

(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType.

如何记录我正在加载的页面中的所有请求?

How do I log all the requests from the page I'm loading?

推荐答案

更新:另一种选择是使用 NSURLProtocol 来听取请求应用程序,包括Web视图中的所有应用程序。

Update: Another option is to use NSURLProtocol to hear requests the application makes, including all the ones from the web view.

我将建议一个创造性的解决方案。这不是你真正想要的,但是由于现在不可能使用SDK,这是唯一可行的解​​决方案。

I will suggest a creative solution. This is not really what you want, but since that is not really possible at this point with the SDK, this is the only possible solution.

@interface PrintingCache : NSURLCache
{
}
@end

@implementation PrintingCache

- (NSCachedURLResponse*)cachedResponseForRequest:(NSURLRequest*)request
{
    NSLog(@"url %@", request.URL.absoluteString);

    return [super cachedResponseForRequest:request];
}
@end

现在在你的app delegate中,执行以下操作:

Now in your app delegate, do the following:

NSString *path = ...// the path to the cache file
NSUInteger discCapacity = 10*1024*1024;
NSUInteger memoryCapacity = 512*1024;

FilteredWebCache *cache = [[Printing alloc] initWithMemoryCapacity: memoryCapacity diskCapacity: discCapacity diskPath:path];
[NSURLCache setSharedURLCache:cache];

这会创建一个共享缓存,您的所有应用程序的URL请求都将通过此缓存,包括您的网页视图。但是,您可能会从其他来源获得更多您不想要的URL。

This creates a shared cache, and all of your application's URL requests will go through this cache, including your web view. However, you may get more URLs from other sources that you do not want.

这篇关于收听UIWebView的所有请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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