NSURLSession實現下載(代理),nsurlsession實現

來源:互聯網
上載者:User

NSURLSession實現下載(代理),nsurlsession實現
NSURLSession實現下載(代理)

- (void)downloadTask2
{
   

{
    NSURLSessionConfiguration *cfg = [NSURLSessionConfigurationdefaultSessionConfiguration];
   
    // 1.得到session對象
    NSURLSession *session = [NSURLSessionsessionWithConfiguration:cfgdelegate:selfdelegateQueue:[NSOperationQueuemainQueue]];
   
    // 2.建立一個下載task
    NSURL *url = [NSURLURLWithString:@"http://localhost:8080/MJServer/resources/test.mp4"];
//    NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
//        NSLog(@"%@",location);
//    }];
    NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url];
    // 3.開始任務
    [task resume];
   
    // 如果給下載任務設定了completionHandler這個block,也實現了下載的代理方法,優先執行block
}
注意:NSURLSessionDownloadTask下載完成之後,將資料儲存在沙箱裡面的tmp臨時檔案中,需要將臨時檔案將臨時檔案剪下或者複製Caches檔案夾。 
#pragma mark - NSURLSessionDownloadDelegate
/**
 *  下載完畢後調用
 *
 *  @param location     臨時檔案的路徑(下載好的檔案)
 */
- (void)URLSession:(NSURLSession*)session downloadTask:(NSURLSessionDownloadTask*)downloadTask didFinishDownloadingToURL:(NSURL*)location
{
    // location : 臨時檔案的路徑(下載好的檔案)
   
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) lastObject];
    // response.suggestedFilename : 建議使用的檔案名稱,一般跟伺服器端的檔案名稱一致
   
    NSString *file = [cachesstringByAppendingPathComponent:downloadTask.response.suggestedFilename];
   
    // 將臨時檔案剪下或者複製Caches檔案夾
    NSFileManager *mgr = [NSFileManagerdefaultManager];
   
    // AtPath : 剪下前的檔案路徑
    // ToPath : 剪下後的檔案路徑
    [mgr moveItemAtPath:location.pathtoPath:fileerror:nil];
}

/**
 *  恢複下載時調用
 */
- (void)URLSession:(NSURLSession*)session downloadTask:(NSURLSessionDownloadTask*)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{

}
/**
 *  每當下載完(寫完)一部分時就會調用(可能會被調用多次)
 *
 *  @param bytesWritten              這次調用寫了多少
 *  @param totalBytesWritten         累計寫了多少長度到沙箱中了
 *  @param totalBytesExpectedToWrite 檔案的總長度
 */
- (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);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.