【问题标题】:Listen to all requests from UIWebView监听来自 UIWebView 的所有请求
【发布时间】:2013-01-08 06:20:51
【问题描述】:

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

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

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

【问题讨论】:

  • 你让它工作了吗?
  • 不,我没有。据我所知,无法拦截 webview 发出的单个 http 请求

标签: iphone uiwebview uiwebviewdelegate


【解决方案1】:

试试下面的代码

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

希望对你有所帮助。

【讨论】:

    【解决方案2】:

    这行得通:

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

    确保将webView.delegate = self; 放在[super viewDidLoad] 中,否则它不会显示任何内容。

    【讨论】:

    • 您刚刚复制了Exploring 的另一个答案吗?因为它是错误的。
    【解决方案3】:

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


    我会建议一个创造性的解决方案。这并不是您真正想要的,但由于目前 SDK 无法实现,因此这是唯一可能的解决方案。

    @interface PrintingCache : NSURLCache
    {
    }
    @end
    
    @implementation PrintingCache
    
    - (NSCachedURLResponse*)cachedResponseForRequest:(NSURLRequest*)request
    {
        NSLog(@"url %@", request.URL.absoluteString);
    
        return [super cachedResponseForRequest:request];
    }
    @end
    

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

    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 请求都将通过此缓存,包括您的 Web 视图。但是,您可能会从其他不想要的来源获得更多 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 2017-08-11
      • 2017-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多