iOS基础深入补完计划--NSURLSessionConfiguration相关API

学习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》

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 用到的组件 1、通过CocoaPods安装 2、第三方类库安装 3、第三方服务 友盟社会化分享组件 友盟用户反馈 ...
    SunnyLeong阅读 15,401评论 1赞 180
  • 从加入到写作打卡群,到今天有一段时间了,期间基本上没有中断过。因为我承诺自己的事,至少200个字,我觉得并不难。但...
    Richard1015阅读 208评论 0赞 0
  • 1.预习物联网导论46页(166页-212页) 2.给弟听写英语单词到为了做...
    i噼里啪啦i阅读 115评论 0赞 0
  • 这本书又读的慢了,因为几乎全部内容对我来说都是新的,我得反复看,才能真正记住。不过坚持吧。 9.我没有时间读完整本...
    歌呗lrf阅读 227评论 0赞 0
  • 人的一生中能遇到几个喜欢的人啊?正好那个人也喜欢你?遇到了一定别有遗憾,全力以赴才是对自己的负责。爱情有时是盲目的...
    清窈阅读 302评论 0赞 1

友情链接更多精彩内容