NSURLSessionConfiguration
NSURLSessionConfiguration為NSURLSession配置一些請求所需要的策略。如:超時、緩存策略、鏈接需求的。
NSURLSession會拷貝configuration。所以session一旦初始化結(jié)束就不會再更改configuration。除非初始化一個session。
重要:如果NSURLRequest中也做了一些指定。session也會遵循NSURLRequest的限定,但是如果configuration有更加嚴(yán)格的限定,仍以configuration為主。
一、NSURLSessionConfiguration的描述
NSURLSessionConfiguration為NSURLSession配置一些請求所需要的策略。如:超時、緩存策略、鏈接需求的。
NSURLSession會拷貝configuration。所以session一旦初始化結(jié)束就不會再更改configuration。除非初始化一個session。
重要:如果NSURLRequest中也做了一些指定。session也會遵循NSURLRequest的限定,但是如果configuration有更加嚴(yán)格的限定,仍以configuration為主。
1.defaultSessionConfiguration
默認(rèn)配置使用的是持久化的硬盤緩存,存儲證書到用戶鑰匙鏈。存儲cookie到shareCookie。
2.ephemeralSessionConfiguration
返回一個不適用永久持存cookie、證書、緩存的配置,最佳優(yōu)化數(shù)據(jù)傳輸。
標(biāo)注:當(dāng)程序作廢session時,所有的ephemeral session 數(shù)據(jù)會立即清除。此外,如果你的程序處于暫停狀態(tài),內(nèi)存數(shù)據(jù)可能不會立即清除,但是會在程序終止或者收到內(nèi)存警告或者內(nèi)存壓力時立即清除。
3.backgroundSessionConfigurationWithIdentifier
生成一個可以上傳下載HTTP和HTTPS的后臺任務(wù)(程序在后臺運(yùn)行)。在后臺時,將網(wǎng)絡(luò)傳輸交給系統(tǒng)的單獨(dú)的一個進(jìn)程。
重要:identifier 是configuration的唯一標(biāo)示,不能為空或nil.
如果程序是被系統(tǒng)正常終止的和重新啟動,可以使用同一個identifier創(chuàng)建configuration和session,并且能恢復(fù)終止時的傳輸狀態(tài)。
如果程序是被用戶在手動退出的,session會取消所有的后臺任務(wù),屆時不能再喚醒a(bǔ)pplication,如果想要再次開始傳輸,必須用戶手動開啟application。
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLCache
只對 GET 請求生效
defaultSessionConfiguration,默認(rèn)值的是sharedURLCache。
backgroundSessionConfiguration,默認(rèn)值是nil
ephemeralSessionConfiguration,默認(rèn)一個私有的cache于內(nèi)存,session失效,cache自動清除。
緩存策略
NSURLRequestUseProtocolCachePolicy = 0, //默認(rèn)的緩存策略(取決于協(xié)議)
NSURLRequestReloadIgnoringLocalCacheData = 1, // 忽略緩存,重新請求
NSURLRequestReloadIgnoringLocalAndRemoteCacheData = 4, // Unimplemented
NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData,
NSURLRequestReturnCacheDataElseLoad = 2, // 有緩存就用緩存,沒有緩存就重新請求
NSURLRequestReturnCacheDataDontLoad = 3, // 有緩存就用緩存,沒有緩存就不發(fā)請求,當(dāng)做請求出錯處理(用于離線模式)
NSURLRequestReloadRevalidatingCacheData = 5, // Unimplemented
NSString *cachesDirectory = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
NSString *cachePath = [cachesDirectory stringByAppendingPathComponent:@"MyCache"];
NSLog(@"cachePath:%@",cachePath);
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:16384 diskCapacity:268435456 diskPath:cachePath];
configuration.URLCache = cache;
configuration.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;
NSURL *url = [NSURL URLWithString:@"http://==xxx===xx=="];
NSMutableURLRequest *requset = [NSMutableURLRequest requestWithURL:url];
requset.HTTPMethod = @"GET";
//設(shè)置緩存策略
requset.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
```
//獲得緩存
NSCachedURLResponse *cachedURLResponse = [cache cachedResponseForRequest:requset];
NSData *cacheData = cachedURLResponse.data;
if(cacheData){//如果有緩存
NSDictionary *dicJson=[NSJSONSerialization JSONObjectWithData:cacheData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"dicJson: %@",dicJson);
}
###請求
NSOperationQueue *operationQueue = [NSOperationQueue mainQueue];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:operationQueue];
[[defaultSession dataTaskWithRequest:requset completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *dicJson=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"dicJson: %@",dicJson);
}] resume];
http://www.itdecent.cn/p/28cc386fd460
http://blog.csdn.net/growinggiant/article/details/50483127