NSURLSession(四)NSURLSessionDownloadTask下载任务

来源:互联网 发布:java规则引擎源码 编辑:程序博客网 时间:2024/04/28 04:00
    //1.url
    NSURL *url = [NSURL URLWithString:@"http://bcs.duapp.com/chenwei520/media/music.mp3"];
    
    //2.request(config)
    
    //3.session
    NSURLSession *session = [NSURLSession sharedSession];
    
    //4.task
    NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
        /**
         *
         *  @param location 文件下载完成保存的位置
         *  @param response 响应头
         *  @param error    错误信息
         *
         */
        //移动下载的文件到沙盒路径下
        NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/music1.mp3"];
        NSURL *toURL = [NSURL fileURLWithPath:filePath];
        
        NSFileManager *manager = [NSFileManager defaultManager];
        [manager moveItemAtURL:location toURL:toURL error:nil];
        NSLog(@"%@", filePath);
        
        NSLog(@"response: %@", response);
        
    }];
    
    //5.resume
    [task resume];
0 0
原创粉丝点击