NSURLSession 请求

参考网站:http://ningandjiao.iteye.com/blog/2010753

            http://www.cocoachina.com/industry/20131106/7304.html

代码部分:

- (void)viewDidLoad {

    [super viewDidLoad];

//-----------不能监测下载进度的方法------------

//    //创建NSURLSeesion

//    NSURLSession *session = [NSURLSession sharedSession];//是一个单例

//    //在session中每一个请求都是一个任务

////    NSURLSessionDataTask //做get和post请求的

////    NSURLSessionDownloadTask//用于下载

////    NSURLSessionUploadTask//用于上传

//    NSURL *url = [NSURL URLWithString:@"http://nmo.ouj.yymommy.com/185cd4217a3a4799/1441088092/mp3_190_67/30/ac/30e767b8bf61d844ed628ff084b067ac.mp3?s=t"];

//    //创建任务

//    NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

//        NSLog(@"%@",location.path);//NSURL *location为默认路径 location.path转换成

//        //session默认将文件下载到temp目录下 下载完会默认移除,所以想要永久保存要将它移植到caches文件夹下

//        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];

//        NSString *string = [path stringByAppendingPathComponent:@"text1.mp3"];

//        //创建文件管理器 移植文件

//        NSFileManager *manager = [NSFileManager defaultManager];

////        [manager moveItemAtPath:location.path toPath:string error:nil];

//        //也可以用copy的方法

//        [manager copyItemAtPath:location.path toPath:string error:nil];

//        

//        

//    }];

//    //开启任务

//    [task resume];

//----------------------注销线----------------

    

//-----------------可以监测下载进度的方法----------

    //创建默认配置

    NSURLSessionConfiguration *cfn = [NSURLSessionConfiguration defaultSessionConfiguration];

    //创建下载任务

    NSURLSession *session = [NSURLSession sessionWithConfiguration:cfn  delegate:self delegateQueue:[NSOperationQueue mainQueue]];//1参 控制器 2参 download代理

    //开启下载任务 (如果调用的downloadTaskWithURL带block 那么代理方法不会走)

     NSURL *url = [NSURL URLWithString:@"http://nmo.ouj.yymommy.com/185cd4217a3a4799/1441088092/mp3_190_67/30/ac/30e767b8bf61d844ed628ff084b067ac.mp3?s=t"];

    NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url];

    //开启任务

    [task resume];

                                      

    

}

#pragma mark --代理方法

//下载完成会调用的方法

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask

didFinishDownloadingToURL:(NSURL *)location

{

    

}

//每下载一段数据就会调用以下方法

//bytesWritten 这个参数表示这次下载了多少数据

//totalBytesWritten 下载的累计总数据长度

//totalBytesExpectedToWrite 文件总长度

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask

      didWriteData:(int64_t)bytesWritten

 totalBytesWritten:(int64_t)totalBytesWritten

totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite

{

    

}

//断点续传

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask

 didResumeAtOffset:(int64_t)fileOffset

expectedTotalBytes:(int64_t)expectedTotalBytes

{

    

}

原文地址:https://www.cnblogs.com/wusang/p/4766999.html