1 2 3 4 5 6
| NSURLRequestUseProtocolCachePolicy: 对特定的 URL 请求使用网络协议中实现的缓存逻辑。这是默认的策略。 NSURLRequestReloadIgnoringLocalCacheData:数据需要从原始地址加载。不使用现有缓存。 NSURLRequestReloadIgnoringLocalAndRemoteCacheData:不仅忽略本地缓存,同时也忽略代理服务器或其他中间介质目前已有的、协议允许的缓存。 NSURLRequestReturnCacheDataElseLoad:无论缓存是否过期,先使用本地缓存数据。如果缓存中没有请求所对应的数据,那么从原始地址加载数据。 NSURLRequestReturnCacheDataDontLoad:无论缓存是否过期,先使用本地缓存数据。如果缓存中没有请求所对应的数据,那么放弃从原始地址加载数据,请求视为失败(即:“离线”模式)。 NSURLRequestReloadRevalidatingCacheData:从原始地址确认缓存数据的合法性后,缓存数据就可以使用,否则从原始地址加载
|
iOS 使用AFNetWorking监听APP网络状态变化(可用于更改缓存策略、提示网络等)
AFNetworking简单封装
http://flyweights.top/2015/09/23/AFNetworking%E6%93%8D%E4%BD%9C%E9%98%9F%E5%88%97%E8%AF%B7%E6%B1%82/
http://www.jianshu.com/p/8a7594df4659
https://github.com/Meeca/AFNDemo/tree/master/AFNetworkingDemo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| 1 ---- 查看 request 是否被缓存 -- NSCachedURLResponse * response = [cache cachedResponseForRequest:request];
if (response != nil) {
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; NSLog(@"有缓存"); }
2 -----
走 - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse --------------------
如果你不想过多的操作,可以直接返回 cachedResponse -- 除非你不想缓存数据 -- 可以 return nil --
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse { NSLog(@"cache response uerInfo== %@ \n response == %@ ", [cachedResponse userInfo], [cachedResponse response]);
NSString * str = [[NSString alloc] initWithData:[cachedResponse data] encoding:NSUTF8StringEncoding]; NSLog(@"data == %@", str);
return cachedResponse;
NSMutableDictionary * userInfo = [[cachedResponse userInfo] mutableCopy]; NSMutableData * data = [[cachedResponse userInfo] mutableCopy]; NSURLCacheStoragePolicy storagePolicy = NSURLCacheStorageNotAllowed;
NSCachedURLResponse * response = [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response] data:data userInfo:userInfo storagePolicy:storagePolicy]; return response;
return nil;
}
|
http://blog.originate.com/blog/2014/02/20/afimagecache-vs-nsurlcache/
http://blog.csdn.net/mengxiangone/article/details/48623785