问题
in iOS 7 cachePolicy doesn't work, it just cache the downloaded json.
//URLRequest
NSString *url = [NSString stringWithFormat:@"http://www.semhora.com/jsonparser/categories/categories_%d_test.json", _categoriesIndex];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLCacheStorageNotAllowed
timeoutInterval:60.0];
How can I disallow cache in iOS 7?
回答1:
I encountered the same problem and I verified that setting cachePolicy = 0
instead of cachePolicy = NSURLCacheStorageNotAllowed
fixes the problem.
This doesn't make sense to me either since 0 corresponds to NSURLCacheStorageAllowed
.
We can't just set it to 0 since Apple will probably fix this in a future release.
You might try calling:
[NSURLCache sharedURLCache] removeCachedResponseForRequest:yourRequest]
just before initiating the request.
UPDATE: After further research I found that the code that broke has been using the wrong enum. Take a look at NSURLRequestCachePolicy in NSURLRequest.h. That's the one you need and it explains why setting to 0 fixed the problem for you.
回答2:
I just used:
//URLRequest
NSString *url = [NSString stringWithFormat:@"http://www.semhora.com/jsonparser/categories/categories_%d_test.json", _categoriesIndex];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:0
timeoutInterval:60.0];
And now it works, got any answer from apple dev forum until now why it happens.
回答3:
The correct enum for the cache Policy is :
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:downloadURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
If you are over 3G, some provider use caching even if you disable it in your NSMutableURLRequest
, so if the cache policy doesn't work set the HTTP header field cache-control to no-cache.
[request setValue:@"no-cache" forHTTPHeaderField:@"cache-control"];
来源:https://stackoverflow.com/questions/18923675/nsurlrequest-cache-issue-ios-7