NSURLSession *session = [NSURLSession sharedSession]; NSURL *url = [NSURL URLWithString:@"https://xxx.yy.zz/check"]; NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){ if(!error){ NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; NSString *status = responseDic[@"status"]; if ([status isKindOfClass:[NSString class]] && [status isEqualToString:@"1"]) { self.needAutoCode = YES ; [self.tableView reloadData]; } }else { NSLog(@"%@", error); } }]; [task resume];
NSURL *url = [NSURL URLWithString:@"https://xxx.xxx.xx/accessToken"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; NSString *param = [NSString stringWithFormat:@"code=%@&client_secret=3QmKoBbku7&client_id=apollo&grant_type=code",self.serviceTicket]; request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){ if((!error) && ([(NSHTTPURLResponse *)response statusCode] == 200)) { NSError *error0 = nil; NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error0]; if(!error0){ NSString *refreshToken = jsonDic[@"refresh_token"]; NSString *accessToken = jsonDic[@"access_token"]; //此处一些业务处理 }else{ NSLog(@"%@",error0.nv_message); } }else{ NSLog(@"%@",error.nv_message); } }]; [task resume];
NSURLSession *session = [NSURLSession sharedSession]; NSURL *url = [NSURL URLWithString:@"http://www.daka.com/resources/image/icon.png"] ; NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) { // location是沙盒中tmp文件夹下的一个临时url,文件下载后会存到这个位置,因为tmp中的文件随时可能被删除,因此咱们须要本身须要把下载的文件挪到须要的地方 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename]; // 剪切文件 [[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:path] error:nil]; }]; // 启动任务 [task resume];
// 使用这种方式取消下载能够获得未来用来恢复的数据,保存起来 [self.task cancelByProducingResumeData:^(NSData *resumeData) { self.resumeData = resumeData; }]; // 因为下载失败致使的下载中断会进入此协议方法,也能够获得用来恢复的数据 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{ // 保存恢复数据 self.resumeData = error.userInfo[NSURLSessionDownloadTaskResumeData]; } // 恢复下载时接过保存的恢复数据 self.task = [self.session downloadTaskWithResumeData:self.resumeData]; // 启动任务 [self.task resume];
NSURLSessionUploadTask *uploadtask1 = [self.session uploadTaskWithRequest:request fromFile:fileName completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {}]; //或者,出于安全性考虑,一般使用POST方式进行文件上传 NSURLSessionUploadTask *uploadtask2 = [self.session uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"-------%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);}];
//使用代理方法须要设置代理,可是session的delegate属性是只读的,要想设置代理只能经过这种方式建立session self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@""]]; // 建立任务(由于要使用代理方法,就不须要block方式的初始化了)和启动任务 //(1)通常请求任务 NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request]; [task resume]; //(2)上传任务 NSURLSessionUploadTask *uploadtask = [self.session uploadTaskWithRequest:request fromData:request.HTTPBody]; [uploadtask resume]; //(3)下载任务 NSURLSessionDownloadTask *downloadTask = [self.session downloadTaskWithRequest:request]; [downloadTask resume];
//对应的代理方法以下:
# pragma mark NSURLSessionTaskDelegate // 请求成功或者失败(若是失败,error有值) - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { // 请求完成,成功或者失败的处理 if (self.completion) { NSData *data = nil; if (self.receivedData) { data = [self.receivedData copy]; self.receivedData = nil; } self.completion(self.cmd, task.response, data, error,error); } [self.session invalidateAndCancel]; } - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend { if (self.uploadBlock) { int64_t totalUnitCount = totalBytesExpectedToSend; if(totalUnitCount == NSURLSessionTransferSizeUnknown) { NSString *contentLength = [task.originalRequest valueForHTTPHeaderField:@"Content-Length"]; if(contentLength) { totalUnitCount = (int64_t) [contentLength longLongValue]; } } NSInteger totalBytesSentInt = [[NSNumber numberWithLongLong:totalBytesSent] integerValue]; NSInteger totalUnitCountInt = [[NSNumber numberWithLongLong:totalUnitCount] integerValue]; self.uploadBlock(totalBytesSentInt,totalUnitCountInt); } } - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest * __nullable))completionHandler{ //禁止302 301重定向 completionHandler(nil); } # pragma mark NSURLSessionDataDelegate // 接收到服务器的响应 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler { // 容许处理服务器的响应,才会继续接收服务器返回的数据 completionHandler(NSURLSessionResponseAllow); } // 接收到服务器的数据(可能调用屡次) - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data { // 处理每次接收的数据 [self.receivedData appendData:data]; } # pragma mark NSURLSessionDownloadDelegate // 每次写入调用(会调用屡次) - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { if (self.downloadBlock) { NSInteger bytesWrittenInt = [[NSNumber numberWithLongLong:totalBytesWritten] integerValue]; NSInteger totalBytesExpectedToWriteInt = [[NSNumber numberWithLongLong:totalBytesExpectedToWrite] integerValue]; self.downloadBlock(bytesWrittenInt,totalBytesExpectedToWriteInt); } //另外, 可在这里经过已写入的长度和总长度算出下载进度 CGFloat progress = 1.0 * totalBytesWritten / totalBytesExpectedToWrite; NSLog(@"%f",progress); } // 下载完成调用 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { self.receivedData = [[NSData dataWithContentsOfURL:location.filePathURL] mutableCopy]; }
- (void)suspend; - (void)resume; - (void)cancel;
+ (NSURLSessionConfiguration *)defaultSessionConfiguration; + (NSURLSessionConfiguration *)ephemeralSessionConfiguration; + (NSURLSessionConfiguration *)backgroundSessionConfigurationWithIdentifier:(NSString *)identifier
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; // 超时时间 config.timeoutIntervalForRequest = 10; // 是否容许使用蜂窝网络(后台传输不适用) config.allowsCellularAccess = YES; // 还有不少能够设置的属性
特别说明,本文是对简书《使用NSURLSession》的学习与记录,在使用方面作了一些调研和实验。json