- (id)initWithURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval;
cachePolicy表示缓存策略
1、 NSURLRequestUseProtocolCachePolicy = 0,
@constant NSURLRequestUseProtocolCachePolicy Specifies that the
caching logic defined in the protocol implementation, if any, is
used for a particular URL load request. This is the default policy
for URL load requests.
默认策略,使用缓存
2.NSURLRequestReloadIgnoringLocalCacheData = 1,
data for the URL load should be loaded from the origin source. No
existing local cache data, regardless of its freshness or validity,
should be used to satisfy a URL load request.
忽略本地缓存
3.NSURLRequestReturnCacheDataElseLoad = 2,
data from the original source. The cached data could be protocol-based cached or
locally cached. If there is no cached data, the data will be downloaded from the
original source.
如果有缓存,不管过期时间优先使用本地缓存,如果没有本地缓存,才从原地址下载
4. NSURLRequestReturnCacheDataDontLoad = 3
@constant NSURLRequestReturnCacheDataDontLoad Specifies that the
existing cache data should be used to satisfy a URL load request,
regardless of its age or expiration date. However, if there is no
existing data in the cache corresponding to a URL load request, no
attempt is made to load the URL from the origin source, and the
load is considered to have failed. This constant specifies a
behavior that is similar to an "offline" mode
只使用缓存,如果没有匹配的缓存则报告离线模式,而不会从网上load数据
sample:
NSURLRequest *request=[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.sohu.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3];