1

NSURLProtocol の実装を登録して、特定の URL 要求のカスタム処理を行います (これらの要求にプロパティを設定してタグを付けます)。これらの URL リクエストは、UIWebView の load メソッドから来ています。

  1. UI Web ビューを作成する (起動後の最初の読み込み)
  2. コンテンツを読み込む
  3. Web ビューを破棄する
  4. 新しい Web ビューを作成する (後続の読み込み)
  5. コンテンツを読み込む

ステップ 2 と 5 の間で動作が大きく異なります。私の NSURLProtocol 実装は、キャッシュされたデータを管理し、リクエストを処理するように設計されています。リクエストで自分のプロパティが検出されない場合は、特定の URL について 2 回目のチェックを行います (デバッグ用)。どちらの場合も次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

他の人は、同じアセットに関するプロトコルへの呼び出しが複数あることを指摘していますが、これは問題ではありませんが、新しいオブジェクトで呼び出されるたびに、それが 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

これは、私の見解では、予期しない動作です。プロパティが に渡される 3 回目までにリクエストから取り除かれているように見えますcanInitWithRequest。その後、応答YESしても、実際には開始されません。ページは単にUIWebView全体に戻され、その後のリクエストはありません。資産。作成時のリクエストは次のようになります。

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

私のプロトコルがリクエストを処理できると言ったとき、なぜそうする機会が与えられていないのでしょうか? 私の推測では、答えは UIWebView 自体の実装にあると思います。私のプロトコルをロードの責任エンティティにしたい場合、これを回避する方法について何か考えはありますか?

4

2 に答える 2

1
*** 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];
于 2016-12-27T08:44:50.047 に答える