AFNetworking not resuming download

前端 未结 1 702
半阙折子戏
半阙折子戏 2020-12-16 07:13

I am using AFNetworking to download large file into my iPad app.

An instance of AFHTTPRequestOperation is used to download this file. Below is the code for referenc

相关标签:
1条回答
  • 2020-12-16 07:33

    After spending sometime I figured out how to pause and resume the download.

    AFNetworking has extensions one of them is AFDownloadRequestOperation which is essentially used to handle the pause and resume of large files. So instead of using AFHTTPRequestOperation here AFDownloadRequestOperation is to be used. Below is sample code

    //request is the NSRequest object for the file getting downloaded and targetPath is the final location of file once its downloaded. Don't forget to set shouldResume to YES
    AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request
                                                                                         targetPath:targetPath
                                                                                       shouldResume:YES];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        //handel completion
        }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         //handel failure
     }];
    [operation setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
        //handel progress
    
    }];
    //since this class is subclass of AFHTTPClient so the operation is added to request queue
    [self enqueueHTTPRequestOperation:operation];
    
    //used to pause the download
    -(void)pauseDownload{
        [operation pause];
    }
    //used to resume download
    -(void)resumeDownload{
       [operation resume];
    }
    
    0 讨论(0)
提交回复
热议问题