iOS 使用NSURLSession下載大檔案

來源:互聯網
上載者:User

標籤:super   ltm   sel   imp   父類   interface   ror   arc   允許   

 NSURLSession *session = [NSURLSession sharedSession];

    // 可以不用像connection一樣用代理監聽, 直接會下載檔案, 但是無法獲得下載的進度

    NSURLSessionDownloadTask *task =  [session

    downloadTaskWithURL:[NSURL URLWithString:@""]

     completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {

         // 檔案儲存體的真實路徑

         NSString *file = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];

         // 剪下location的臨時檔案到真實路徑

         NSFileManager *mgr = [NSFileManager defaultManager];

         [mgr moveItemAtURL:location toURL:[NSURL fileURLWithPath:file] error:nil];

   }];

    [task resume];

///////////////////////////////////////////////////////////////////////////////////////////////////////////

// 遵搜三種協議 ,但是 最基本的哪一個 協議的繼承性

@interface ViewController ()<NSURLSessionDataDelegate>// 繼承子類就相當於繼承了父類協議

@end

 //協議間的繼承關係: <NSURLSessionDataDelegate>:<NSURLSessionTaskDelegate>:<NSURLSessionDelegate>

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // NSURLSession *session = [NSURLSession sharedSession];

    // session.delegate  = self; // 不能直接設定delegate是 readonly

    // 只能一開始的時候建立其代理

    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];

 

    // 父協議,但是可以傳入子協議:nullable id <NSURLSessionDelegate>

    NSURLSessionDownloadTask *task =  [session downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {}];

    [task resume];

}

 // 代理方法

-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler{

      // 要啟動,否則無法下載

    completionHandler(NSURLSessionResponseAllow);// 允許 後再繼續請求數

}

 -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{

}

 -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{

}

iOS 使用NSURLSession下載大檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.