本文主要是介绍24-NSURLSession的用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
说明:NSURLSession实在IOS7推出的一种新技术,用来代替NSURLConnection.
利用NSURLSession可以更加方便的实现与服务器的交互,更方便的下载上传文件。
NSURLSession的常用方法:
// 常用的类方法
+ (NSURLSession *)sharedSession;
+ (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration;
+ (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration delegate:(id <NSURLSessionDelegate>)delegate delegateQueue:(NSOperationQueue *)queue;// 任务:任何一个请求都是任务
// NSURLSessionDataTask : 普通的GET\POST请求
// NSURLSessionDownloadTask : 文件下载
// NSURLSessionUploadTask : 文件上传
// 下面是常用的创建任务的方法
/* * NSURLSessionTask objects are always created in a suspended state and* must be sent the -resume message before they will execute.*//* Creates a data task with the given request. The request may have a body stream. */
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request;/* Creates a data task to retrieve the contents of the given URL. */
- (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *)url;/* Creates an upload task with the given request. The body of the request will be created from the file referenced by fileURL */
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL;/* Creates an upload task with the given request. The body of the request is provided from the bodyData. */
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(NSData *)bodyData;/* Creates an upload task with the given request. The previously set body stream of the request (if any) is ignored and the URLSession:task:needNewBodyStream: delegate will be called when the body payload is required. */
- (NSURLSessionUploadTask *)uploadTaskWithStreamedRequest:(NSURLRequest *)request;/* Creates a download task with the given request. */
- (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request;/* Creates a download task to download the contents of the given URL. */
- (NSURLSessionDownloadTask *)downloadTaskWithURL:(NSURL *)url;/* Creates a download task with the resume data. If the download cannot be successfully resumed, URLSession:task:didCompleteWithError: will be called. */
- (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData;
当需要有下载进度时,需要使用协议,实现NSURLSessionDownloadDelegate方法。
下面是利用NSURLSession实现的下载代码:
//
// ViewController.m
// NSURLSession-的使用
//
// Created by apple on 15-5-20.
// Copyright (c) 2015年 zzu. All rights reserved.
//#import "ViewController.h"@interface ViewController ()<NSURLSessionDownloadDelegate>@property (nonatomic,strong) NSURLSession *session;@end@implementation ViewController// 任务:任何一个请求都是任务
// NSURLSessionDataTask : 普通的GET\POST请求
// NSURLSessionDownloadTask : 文件下载
// NSURLSessionUploadTask : 文件上传
- (void)viewDidLoad
{[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.
}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// NSLog(@"%@",caches);[self downloadTask2];
}/*** 下载任务:不能看到下载进度*/
- (void)downloadTask
{NSURLSession *session = [NSURLSession sharedSession];NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/images/minion_01.png"];// 创建一个下载taskNSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {// location : 临时文件的路径(下载好的文件)NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];NSString *file = [caches stringByAppendingPathComponent:response.suggestedFilename];// 将临时文件剪切或复制caches文件NSFileManager *mgr = [NSFileManager defaultManager];[mgr moveItemAtPath:location.path toPath:file error:nil];}];// 开始任务[task resume];
}- (void)downloadTask2
{NSURLSessionConfiguration *cfg = [NSURLSessionConfiguration defaultSessionConfiguration];// 得到session对象NSURLSession *session = [NSURLSession sessionWithConfiguration:cfg delegate:self delegateQueue:[NSOperationQueue mainQueue]];// 创建一个下载taskNSURL *url= [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/videos/test.zip"];NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url];// 开始任务[task resume];// 如果下载任务设置了completionHandle这个block,也实现了下载的代理方法,则优先执行block
}#pragma mark - <NSURLSessionDownloadDelegate>协议的方法// 下载完毕后调用
// location : 临时文件的路径 (下载好的文件)
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];NSString *file = [caches stringByAppendingPathComponent:downloadTask.response.suggestedFilename];// 将临时文件剪切或复制到caches文件夹NSFileManager *mgr = [NSFileManager defaultManager];[mgr moveItemAtPath:location.path toPath:file error:nil];
}// 恢复下载时调用
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{}//每当下载完一部分数据时,就会调用
// bytesWriten : 本次调用写了多少数据
// totalBytesWritten : 累计写了多少数据到沙盒中
// totalBytesExceptedToWrite : 文件总长度
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{double progress = (double)totalBytesWritten / totalBytesExpectedToWrite;NSLog(@"下载进度----- %f",progress);
}@end
带断点续传功能的问文件下载:
//
// ZQViewController.m
//#import "ZQViewController.h"@interface ZQViewController () <NSURLSessionDownloadDelegate, NSURLSessionDataDelegate>
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
- (IBAction)download:(UIButton *)sender;@property (nonatomic, strong) NSURLSessionDownloadTask *task;
@property (nonatomic, strong) NSData *resumeData;
@property (nonatomic, strong) NSURLSession *session;
@end@implementation HMViewController- (NSURLSession *)session
{if (!_session) {// 获得sessionNSURLSessionConfiguration *cfg = [NSURLSessionConfiguration defaultSessionConfiguration];self.session = [NSURLSession sessionWithConfiguration:cfg delegate:self delegateQueue:[NSOperationQueue mainQueue]];}return _session;
}- (void)viewDidLoad
{[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}- (IBAction)download:(UIButton *)sender {// 按钮状态取反sender.selected = !sender.isSelected;if (self.task == nil) { // 开始(继续)下载if (self.resumeData) { // 恢复[self resume];} else { // 开始[self start];}} else { // 暂停[self pause];}
}/*** 从零开始*/
- (void)start
{// 1.创建一个下载任务NSURL *url = [NSURL URLWithString:@"http://192.168.15.172:8080/MJServer/resources/videos/minion_01.mp4"];self.task = [self.session downloadTaskWithURL:url];// 2.开始任务[self.task resume];
}/*** 恢复(继续)*/
- (void)resume
{// 传入上次暂停下载返回的数据,就可以恢复下载self.task = [self.session downloadTaskWithResumeData:self.resumeData];// 开始任务[self.task resume];// 清空self.resumeData = nil;
}/*** 暂停*/
- (void)pause
{__weak typeof(self) vc = self;[self.task cancelByProducingResumeData:^(NSData *resumeData) {// resumeData : 包含了继续下载的开始位置\下载的urlvc.resumeData = resumeData;vc.task = nil;}];
}#pragma mark - NSURLSessionDownloadDelegate
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location
{NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];// response.suggestedFilename : 建议使用的文件名,一般跟服务器端的文件名一致NSString *file = [caches stringByAppendingPathComponent:downloadTask.response.suggestedFilename];// 将临时文件剪切或者复制Caches文件夹NSFileManager *mgr = [NSFileManager defaultManager];// AtPath : 剪切前的文件路径// ToPath : 剪切后的文件路径[mgr moveItemAtPath:location.path toPath:file error:nil];
}- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTaskdidWriteData:(int64_t)bytesWrittentotalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{NSLog(@"获得下载进度--%@", [NSThread currentThread]);// 获得下载进度self.progressView.progress = (double)totalBytesWritten / totalBytesExpectedToWrite;
}- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTaskdidResumeAtOffset:(int64_t)fileOffset
expectedTotalBytes:(int64_t)expectedTotalBytes
{}//- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
//{
//
//}
//
//- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
//{
//
//}
@end
这篇关于24-NSURLSession的用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!