NSURLRequestCachePolicy 缓存策略

1> NSURLRequestUseProtocolCachePolicy = 0, 默认的缓存策略, 如果缓存不存在,直接从服务端获取。如果缓存存在,会根据response中的Cache-Control字段判断下一步操作,如: Cache-Control字段为must-revalidata, 则询问服务端该数据是否有更新,无更新的话直接返回给用户缓存数据,若已更新,则请求服务端.

2> NSURLRequestReloadIgnoringLocalCacheData = 1, 忽略本地缓存数据,直接请求服务端.

3> NSURLRequestIgnoringLocalAndRemoteCacheData = 4, 忽略本地缓存,代理服务器以及其他中介,直接请求源服务端.

4> NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData

5> NSURLRequestReturnCacheDataElseLoad = 2, 有缓存就使用,不管其有效性(即忽略Cache-Control字段), 无则请求服务端.

6> NSURLRequestReturnCacheDataDontLoad = 3, 死活加载本地缓存. 没有就失败. (确定当前无网络时使用)

7> NSURLRequestReloadRevalidatingCacheData = 5, 缓存数据必须得得到服务端确认有效才使用(貌似是NSURLRequestUseProtocolCachePolicy中的一种情况)

Tips: URL Loading System默认只支持如下5中协议: 其中只有http://和https://才有缓存策略.
(1) http://
(2) https://
(3) ftp://
(4) file://
(5) data://



使用方法:


-(void) downloadURL:(NSURL *)paramURL{

NSURLCache *urlCache = [NSURLCache sharedURLCache];
[urlCache setMemoryCapacity:1*1024*1024];

NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:paramURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0f];

NSCachedURLResponse *response =[urlCache cachedResponseForRequest:request];
if (response != nil){
    FLOG(@"Cached response exists. Loading data from cache...");
    [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
}

UIWebView *webView = [[UIWebView alloc] initWithFrame:self.bounds];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
webView.scalesPageToFit = YES;
webView.backgroundColor = [UIColor whiteColor];
webView.delegate = self;
[webView loadRequest:request];

_progressProxy = [[NJKWebViewProgress alloc] init];
_progressProxy.webViewProxyDelegate = self;
_progressProxy.progressDelegate = self;
webView.delegate  = _progressProxy;
CGFloat progressBarHeight = 2.f;
CGRect barFrame = CGRectMake(0,0, kScreenWidth, progressBarHeight);
_progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
_progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
self.webView = webView;
[self addSubview:self.webView];
[self addSubview:_progressView];

}

你可能感兴趣的:(uiwebview)