iOS 大檔案斷點下載,iOS檔案斷點下載

來源:互聯網
上載者:User

iOS 大檔案斷點下載,iOS檔案斷點下載

iOS 在下載大檔案的時候,可能會因為網路或者人為等原因,使得下載中斷,那麼如何能夠進行斷點下載呢?

// resumeData的檔案路徑#define XMGResumeDataFile [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"resumeData.tmp"]#import "ViewController.h"@interface ViewController () <NSURLSessionDownloadDelegate>/** 下載任務 */@property (nonatomic, strong) NSURLSessionDownloadTask *task;/** 儲存上次的下載資訊 */@property (nonatomic, strong) NSData *resumeData;/** session */@property (nonatomic, strong) NSURLSession *session;@end

 

 

@implementation ViewController/** session */- (NSURLSession *)session{    if (!_session) {        _session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];    }    return _session;}/** * 開始下載 */- (IBAction)start:(id)sender {        self.task = [self.session downloadTaskWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"]];    [self.task resume];}/** * 暫停下載 */- (IBAction)pause:(id)sender {    // 一旦這個task被取消了,就無法再恢複    [self.task cancelByProducingResumeData:^(NSData *resumeData) {        self.resumeData = resumeData;    }];}/** * 繼續下載 */- (IBAction)goOn:(id)sender {    self.task = [self.session downloadTaskWithResumeData:self.resumeData];    [self.task resume];}#pragma mark - <NSURLSessionDownloadDelegate>- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{    NSLog(@"didCompleteWithError");        // 儲存恢複資料    self.resumeData = error.userInfo[NSURLSessionDownloadTaskResumeData];}- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes{    NSLog(@"didResumeAtOffset");}/** * 每當寫入資料到臨時檔案時,就會調用一次這個方法 * totalBytesExpectedToWrite:總大小 * totalBytesWritten: 已經寫入的大小 * bytesWritten: 這次寫入多少 */- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{    NSLog(@"--------%f", 1.0 * totalBytesWritten / totalBytesExpectedToWrite);}/** *  * 下載完畢就會調用一次這個方法 */- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{    // 檔案將來存放的真實路徑    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:downloadTask.response.suggestedFilename];        // 剪下location的臨時檔案到真實路徑    NSFileManager *mgr = [NSFileManager defaultManager];    [mgr moveItemAtURL:location toURL:[NSURL fileURLWithPath:file] error:nil];}@end

 

相關文章

聯繫我們

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