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?
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.
来源:https://stackoverflow.com/questions/14502283/listen-to-all-requests-from-uiwebview