Listen to all requests from UIWebView

点点圈 提交于 2019-12-04 10:42:51

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


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

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];

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.

Try the below code

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"url %@", [[request URL] absoluteString]);
return YES;
}

hope it will help you.

This works:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"url %@", [[request URL] absoluteString]);
return YES;
}

Make sure you put webView.delegate = self; in the [super viewDidLoad], else it won't show anything.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!