ios - 在对 canInitWithRequest 的 YES 响应后,不要求加载 NSURLProtocol

标签 ios cocoa-touch uiwebview nsurl nsurlprotocol

我正在注册一个 NSURLProtocol 的实现来对某些 URL 请求进行一些自定义处理(我通过在它们上设置属性来标记这些请求)。这些 URL 请求来自 UIWebView 的加载方法。

  1. 创建 UI WebView (启动后首次加载)
  2. 加载内容
  3. 销毁 WebView
  4. 创建新的 WebView (后续加载)
  5. 加载内容

我发现第 2 步和第 5 步之间的行为有很大不同。我的 NSURLProtocol 实现管理缓存数据并设计用于处理请求。如果我没有在请求中检测到我的属性,我会再次检查特定的 URL(用于调试)。在任何一种情况下,canInitWithRequest 都将返回 YES:

启动后首次加载:

2014-10-15 11:37:11.403 MYApp[5813:60b] MYURLProtocol canInitWithRequest: Matched property in request: <0x15ebbb40> https://example.com/ 
2014-10-15 11:37:11.404 MYApp[5813:60b] MYURLProtocol canInitWithRequest: Matched property in request: <0x15dc8da0> https://example.com/ 
2014-10-15 11:37:11.409 MYApp[5813:60b] MYURLProtocol canInitWithRequest: Matched property in request: <0x15ee5ef0> https://example.com/ 
2014-10-15 11:37:11.409 MYApp[5813:60b] MYURLProtocol canInitWithRequest: Matched property in request: <0x15ee6240> https://example.com/ 
2014-10-15 11:37:11.410 MYApp[5813:60b] MYURLProtocol initWithRequest:cachedResponse:client: Request: https://example.com/
2014-10-15 11:37:11.411 MYApp[5813:60b] MYURLProtocol canInitWithRequest: Matched property in request: <0x15ee69d0> https://example.com/ 
2014-10-15 11:37:11.415 MYApp[5813:9c07] MYURLProtocol startLoading Loading <0x15ee6240> https://example.com/ 
... A bunch of loading of assets occurs (cached responses) ...
2014-10-15 11:37:12.497 MYApp[5813:60b] MyWebViewController webViewDidFinishLoad: Finished loading

其他人指出,针对同一个 Assets 多次调用协议(protocol),这不是问题,但有趣的是每次调用一个新对象时,它是第 4 个(的4) 传递给 startLoading 的对象。不过,这里没有真正的问题。

后续加载:

2014-10-15 11:11:27.466 MYApp[5782:60b] MYURLProtocol canInitWithRequest: Matched property in request: <0x1727c310> https://example.com/ 
2014-10-15 11:11:27.467 MYApp[5782:60b] MYURLProtocol canInitWithRequest: Matched property in request: <0x145b1d90> https://example.com/ 
2014-10-15 11:11:27.488 MYApp[5782:560f] MYURLProtocol canInitWithRequest: Matched URL in request: <0x17266060> https://example.com/
2014-10-15 11:11:27.669 MYApp[5782:60b] MYWebViewController webViewDidFinishLoad: Finished loading

在我看来,这是行为出乎意料的地方。似乎在第三次将属性传递给 canInitWithRequest 时,该属性已从请求中剥离,然后,即使我们响应 YES,我们也从未真正启动过 --该页面只是完整地返回到 UIWebView,没有后续的 Assets 请求。下面是创建请求时的样子:

NSURLRequest *request = [NSURLRequest requestWithURL:myURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[self.webView loadRequest:request];

为什么,当我说我的协议(protocol)可以处理请求时,它没有机会这样做吗?我的猜测是答案在于 UIWebView 本身的实现。如果我真的希望我的协议(protocol)成为负责加载的实体,有没有关于如何解决这个问题的想法?

最佳答案

*** Way to clear the cache of webview **********
Follow this code:

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *cookie in [storage cookies]) {
        [storage deleteCookie:cookie];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];

关于ios - 在对 canInitWithRequest 的 YES 响应后,不要求加载 NSURLProtocol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26390036/

相关文章:

ios - SQLite 电话簿插入失败

ios - 如何在没有导航 Controller 的情况下导航到另一个 Controller ?

ios - 在 UINavigationController 中设置图像时,图像未显示在模拟器中

javascript - iOS HTML 相机预览输入文档

iOS webview 缩放以匹配视网膜显示

iOS 9 - 键盘在升级时将包名称显示为 "(null)"

ios - 仅在 swift 2 中左右拖动对象

ios - 在核心数据中保存对象

iphone - UITableViewCell Selectioncolor 隐藏所有内容

Objective-C 将字符串添加到 html 包中