Listen to all requests from UIWebView

I can intercept the bootstrap request from UIWebView with:

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

How can I register all requests from the page I'm downloading?

+4
source share
3 answers

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


I offer a creative solution. This is not what you want, but since it is currently not possible to do this using 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 the application deletion, 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 URL requests will go through that cache, including your webview. However, you can get more URLs from other sources that you do not need.

+8
source

Try the code below

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

Hope this helps you.

+2
source

It works:

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

Make sure you set webView.delegate = self; in [super viewDidLoad] , otherwise it will not display anything.

-2
source

All Articles