标签:
一、创建一个Session,并且对其进行初始化
包含三种创建方式:
NSURLSessionConfiguration *myconfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"backgroundsession"]; //请求的缓存策略 myconfiguration.requestCachePolicy = NSURLRequestUseProtocolCachePolicy; //数据传输超时,当恢复传输时会清零 myconfiguration.timeoutIntervalForRequest = 5; //单条请求超时,决定一条请求的最长生命周期 myconfiguration.timeoutIntervalForResource = 5; //请求的服务类型 myconfiguration.networkServiceType = NSURLNetworkServiceTypeDefault; //是否允许使用移动网络(电话网络)default is YES myconfiguration.allowsCellularAccess = YES; //后台模式生效,YES允许自适应系统性能调节 myconfiguration.discretionary = YES; self.testSession = [NSURLSession sessionWithConfiguration:myconfiguration delegate:self delegateQueue:[NSOperationQueue currentQueue]];
二、实现代理(Session-Level)
选用第三种初始化方式创建的Session会有三个Session级别的代理供大家使用
//session关闭回调 -(void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error { NSLog(@"%s,%@",__func__,error); } //证书安全问题 -(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler { } //后台网络任务完成 // 将回调 :-application:handleEventsForBackgroundURLSession:completionHandler: -(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session { }
标签:
原文地址:http://www.cnblogs.com/firephoenix-wings/p/5780272.html