NSURLSession(四)NSURLSessionDownloadTask下載任務

    //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];
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章