
学习NSURLSession之前、先撸一遍NSURLSessionConfiguration头文件里的属性和API
本文链接
@interface NSURLSessionConfiguration : NSObject <NSCopying>
/*
硬盘缓存、存储证书到用户钥匙链、存储cookie到shareCookie
*/
@property (class, readonly, strong) NSURLSessionConfiguration *defaultSessionConfiguration;
/*
无痕浏览、不储存证书和cookie。缓存随着session而销毁
*/
@property (class, readonly, strong) NSURLSessionConfiguration *ephemeralSessionConfiguration;
/*
当APP被切入后台的时候依旧可以进行网络会话
甚至进程被关闭后重新开启、只要有同一个identifier、也能继续会话(
*/
+ (NSURLSessionConfiguration *)backgroundSessionConfigurationWithIdentifier:(NSString *)identifier API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));
/* background时所设置的identifier */
@property (nullable, readonly, copy) NSString *identifier;
/* 缓存策略、默认值是NSURLRequestUseProtocolCachePolicy */
@property NSURLRequestCachePolicy requestCachePolicy;
/* 请求超时时间 默认60s */
@property NSTimeInterval timeoutIntervalForRequest;
/* Resource超时时间 默认7天 */
@property NSTimeInterval timeoutIntervalForResource;
/* 指定网络传输类型 默认 NSURLNetworkServiceTypeDefault*/
@property NSURLRequestNetworkServiceType networkServiceType;
/* 是否使用蜂窝网络 默认yes */
@property BOOL allowsCellularAccess;
/*
ios11新出的一个属性、看了看翻译似乎是说不会因为最大并发数的等待限制而超时。默认是No、后台会话时会被忽略。
*/
@property BOOL waitsForConnectivity API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));
/* 是否允许系统根基性能自动控制`后台任务`、background默认YES、其余默认NO*/
@property (getter=isDiscretionary) BOOL discretionary API_AVAILABLE(macos(10.10), ios(7.0), watchos(2.0), tvos(9.0));
/*
服务于扩展服务(也就是跨APP通信)
通过sharedContainerIdentifier可以获取该共享容器
*/
@property (nullable, copy) NSString *sharedContainerIdentifier API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));
/*
当`后台任务`完成时、是否允许APP自动启动或者重新启动。
*/
@property BOOL sessionSendsLaunchEvents API_AVAILABLE(ios(7.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);
/*
指定了会话连接中的代理服务器。
大多数面向消费者的应用程序都不需要代理、所以基本上不需要配置这个属性。
默认为空
*/
@property (nullable, copy) NSDictionary *connectionProxyDictionary;
/* TLS协议的最小允许版本 枚举值*/
@property SSLProtocol TLSMinimumSupportedProtocol;
/* TLS协议的最大允许版本 枚举值*/
@property SSLProtocol TLSMaximumSupportedProtocol;
/* 允许使用HTTP管道、可以缩短请求加载时间。但是不太被服务器支持、默认为No */
@property BOOL HTTPShouldUsePipelining;
/*
是否在会话中自动插入来自shareCookieStorge的cookies
如果想要自己提供cookie、可以使用HTTPAdditionalHeaders来提供。
默认为YES
*/
@property BOOL HTTPShouldSetCookies;
/*
cookie的接收策略 枚举值
typedef NS_ENUM(NSUInteger, NSHTTPCookieAcceptPolicy) {
NSHTTPCookieAcceptPolicyAlways, //接收全部 默认
NSHTTPCookieAcceptPolicyNever, //拒绝全部
NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain
//仅接收从主文档域来的cookie。
};
*/
@property NSHTTPCookieAcceptPolicy HTTPCookieAcceptPolicy;
/*
默认为空。
请求头设置
(重要:不要修改以下请求头:Authorization、Connection、Host、WWW-Authenticate
如果这里边添加的请求头跟NSURLRequest中重复了,侧优先使用NSURLRequest中的头。)
*/
@property (nullable, copy) NSDictionary *HTTPAdditionalHeaders;
/* 对于某一个host、最大的链接数 现在是ios11.2 默认还是4 */
@property NSInteger HTTPMaximumConnectionsPerHost;
/* 存储cookie。可以置nil */
@property (nullable, retain) NSHTTPCookieStorage *HTTPCookieStorage;
/* 存储证书 */
@property (nullable, retain) NSURLCredentialStorage *URLCredentialStorage;
/* 缓存 */
@property (nullable, retain) NSURLCache *URLCache;
/* Enable extended background idle mode for any tcp sockets created. Enabling this mode asks the system to keep the socket open
* and delay reclaiming it when the process moves to the background (see https://developer.apple.com/library/ios/technotes/tn2277/_index.html)
也是和后台扩展有关的、网上也没查到相关的东西。先放着吧
*/
@property BOOL shouldUseExtendedBackgroundIdleMode API_AVAILABLE(macos(10.11), ios(9.0), watchos(2.0), tvos(9.0));
/*
处理NSURLRequest的NSURLProtocol的子类
通过 + (BOOL)canInitWithRequest:(NSURLRequest *)request;
来拦截和处理NSURLRequest请求
官方文档
https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration/1411050-protocolclasses?language=objc
以及另一篇关于NSURLProtocol的帖子:
https://blog.csdn.net/xanxus46/article/details/51946432
*/
@property (nullable, copy) NSArray<Class> *protocolClasses;
/*
用于配置链接的多路径服务策略。默认是NSURLSessionMultipathServiceTypeNone
NSURLSessionMultipathServiceTypeNone = 0//不使用多路径TCP服务
NSURLSessionMultipathServiceTypeHandover = 1//多路径TCP服务,提供Wi-Fi和蜂窝之间的无缝切换,以保持连接。
NSURLSessionMultipathServiceTypeInteractive = 2//尝试使用最低延迟接口的服务
NSURLSessionMultipathServiceTypeAggregate = 3//聚合其他多路径选项容量的服务,旨在提高吞吐量和最小化延迟。
官方文档:
https://developer.apple.com/documentation/foundation/nsurlsessionmultipathservicetype?language=objc
*/
@property NSURLSessionMultipathServiceType multipathServiceType API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos);
@end
参考
《objc系列译文(5.4):忘记NSURLConnection,拥抱NSURLSession吧!》
《NSURLSessionConfiguration API详解》
《官方文档:setCookies:forURL:mainDocumentURL:》
《iOS开发之NSURLProtocol的那些坑》
《官方文档:NSURLSessionMultipathServiceType》
网友评论