iOS中NSURLSessionDownloadTask如何設(shè)置代理實(shí)現(xiàn)文件下載

小編給大家分享一下iOS中NSURLSessionDownloadTask如何設(shè)置代理實(shí)現(xiàn)文件下載,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供撫寧網(wǎng)站建設(shè)、撫寧做網(wǎng)站、撫寧網(wǎng)站設(shè)計(jì)、撫寧網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、撫寧企業(yè)網(wǎng)站模板建站服務(wù),10余年撫寧做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

通過代理來實(shí)現(xiàn)文件下載的問題:

#import "ViewController.h"
@interface ViewController ()<NSURLSessionDownloadDelegate>
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
  [self delegate];
}

-(void)delegate
{
  //1.url
  NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_03.png"];
  
  //2.創(chuàng)建請(qǐng)求對(duì)象
  NSURLRequest *request = [NSURLRequest requestWithURL:url];
  
  //3.創(chuàng)建session :注意代理為NSURLSessionDownloadDelegate
  NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
  
  //4.創(chuàng)建Task
  NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request];
  
  //5.執(zhí)行Task
  [downloadTask resume];
}

#pragma mark ----------------------
#pragma mark NSURLSessionDownloadDelegate
/**
 * 寫數(shù)據(jù)
 *
 * @param session          會(huì)話對(duì)象
 * @param downloadTask       下載任務(wù)
 * @param bytesWritten       本次寫入的數(shù)據(jù)大小
 * @param totalBytesWritten     下載的數(shù)據(jù)總大小
 * @param totalBytesExpectedToWrite 文件的總大小
 */
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
  //1. 獲得文件的下載進(jìn)度
  NSLog(@"%f",1.0 * totalBytesWritten/totalBytesExpectedToWrite);
}

/**
 * 當(dāng)恢復(fù)下載的時(shí)候調(diào)用該方法
 *
 * @param fileOffset     從什么地方下載
 * @param expectedTotalBytes 文件的總大小
 */
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
  NSLog(@"%s",__func__);
}

/**
 * 當(dāng)下載完成的時(shí)候調(diào)用
 *
 * @param location   文件的臨時(shí)存儲(chǔ)路徑
 */
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
  NSLog(@"%@",location);
  
  //1 拼接文件全路徑
  NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
  
  //2 剪切文件
  [[NSFileManager defaultManager]moveItemAtURL:location toURL:[NSURL fileURLWithPath:fullPath] error:nil];
  NSLog(@"%@",fullPath);
}

/**
 * 請(qǐng)求結(jié)束
 */
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
  NSLog(@"didCompleteWithError");
}
@end

看完了這篇文章,相信你對(duì)“iOS中NSURLSessionDownloadTask如何設(shè)置代理實(shí)現(xiàn)文件下載”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)頁題目:iOS中NSURLSessionDownloadTask如何設(shè)置代理實(shí)現(xiàn)文件下載
鏈接URL:http://muchs.cn/article42/ighcec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站排名、品牌網(wǎng)站建設(shè)、做網(wǎng)站、域名注冊(cè)、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名