NSURLCache不在iOS 7中存储缓存数据

Rat*_*tan 5 iphone objective-c nsurlcache ios ios7

下面给出的代码适用于iOS 6,但它不适用于iOS 7.

NSCachedURLResponse cachedURLResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data userInfo:nil storagePolicy:NSURLCacheStorageAllowed];
[[NSURLCache sharedURLCache] storeCachedResponse:cachedURLResponse forRequest:request];
NSLog(@"Cached response is %@", [[NSURLCache sharedURLCache] cachedResponseForRequest:request]);
Run Code Online (Sandbox Code Playgroud)

在iOS 6中,我获得了缓存的响应数据,但在iOS 7中,它返回null.我使用以下命令在App委托中设置了NSURLCache对象:

NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:2 * 1024 * 1024
                                                     diskCapacity:2 * 1024 * 1024
                                                         diskPath:nil];
[NSURLCache setSharedURLCache:urlCache];
Run Code Online (Sandbox Code Playgroud)

缓存无法在iOS 7中运行的原因是什么?

Rat*_*tan 0

我正在执行 POST 方法并使用所需的 NSData 设置 http_body 。在 iOS 6 中,响应被缓存,但这在 iOS 7 中不起作用。

因此,现在我们不再设置 http_body,而是在 url 中附加参数并发出请求,并且缓存现在正在工作。