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];

}web

相關文章
相關標籤/搜索