收听UIWebView的所有请求

Dam*_*Diz 4 iphone uiwebview uiwebviewdelegate

我可以通过以下方式拦截来自UIWebView的初始加载请求:

(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType.
Run Code Online (Sandbox Code Playgroud)

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

Leo*_*ica 9

更新:另一个选项是用于NSURLProtocol听取应用程序发出的请求,包括Web视图中的所有请求.


我会建议一个有创意的解决方案 这不是你想要的,但是由于这在SDK上实际上是不可能的,所以这是唯一可行的解​​决方案.

@interface PrintingCache : NSURLCache
{
}
@end

@implementation PrintingCache

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

    return [super cachedResponseForRequest:request];
}
@end
Run Code Online (Sandbox Code Playgroud)

现在,在您的app委托中,执行以下操作:

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];
Run Code Online (Sandbox Code Playgroud)

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