(キャッシュクリアの続き)
NSURLCache を調べてみた。
NSURLCache Class Reference
まず以前のサンプルコードに "Dump Cache" と "Clear Cache" ボタンを追加する(右下)。
コードはこう。
AppController.m
- (IBAction)dumpCache:(id)sender
{
NSURLCache* cache = [NSURLCache sharedURLCache];
NSLog(@"currentDiskUsage: %d", [cache currentDiskUsage]);
NSLog(@"diskCapacity: %d", [cache diskCapacity]);
NSLog(@"currentMemoryUsage: %d", [cache currentMemoryUsage]);
NSLog(@"memoryCapacity: %d", [cache memoryCapacity]);
}
- (IBAction)clearCache:(id)sender
{
NSURLCache* cache = [NSURLCache sharedURLCache];
[cache removeAllCachedResponses];
}
実行してAppleのページを開いた後のキャッシュファイルの状態はこう。

"Dump Cache"ボタンを押すと、currentDiskUsage: の値が一致している。メモリは使っていないことがわかる。

次に "Clear Cache"ボタンを押してみる。

おお、サイズが小さくなった。
"Dump Cache" で確認。

しつこく sqlite3 コマンドでテーブルの中身を調べてみる。cfurl_cache_response と cfurl_cache_blob_data は空だ。

うまくいったようだ。
まとめ:
- WebView で開いたページのキャッシュは [NSURLCache removeAllCachedResponses] でクリアできる。
- ファイルは削除されない(SQLiteデータベースのレコードが削除される)
- - - -
長々と引っ張ったがやっとキャッシュをクリアすることができた。ちょっとうれしい。