Objective-c 清除缓存 NSCachedURLResponse

Objective-c clear cache NSCachedURLResponse

我 运行 这段代码是因为我认为我 运行 遇到了缓存问题:

NSCachedURLResponse *response = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
    if(response){
        NSLog(@"Got Response");
    }else{
        NSLog(@"No Response");
    }

我的问题是如何清除请求的缓存 URL?

NSURLRequest有一个cachePolicy属性,它指定了请求的缓存行为。

设置以下缓存策略 NSURLRequestReloadIgnoringLocalCacheData 当像下面的示例发出请求时将从 url 而不是缓存加载数据。

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];

NSURLRequestReloadIgnoringLocalCacheData

Specifies that the data for the URL load should be loaded from the originating source. No existing cache data should be used to satisfy a URL load request.

https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/index.html#//apple_ref/c/tdef/NSURLRequestCachePolicy

使用NSURLCache的removeCachedResponseForRequest:方法class.

Documentation link