AFNetworking 3.0 斷點續傳 使用記錄


最近項目中用到了壓縮包下載,使用AFNetworking 3.0 下載壓縮包 支持斷點續傳 代碼如下:

#import "HDInternet_handler.h"
#import "AFNetworking.h"
#import "ASIHTTPRequest.h"

@interface HDInternet_handler ()<NSURLSessionDelegate>

@end

@implementation HDInternet_handler
{
    NSURLSessionDownloadTask *_downloadTask;
}

-(void)test
{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
    
    manager.requestSerializer.timeoutInterval = 3.0;
    
    [manager setDownloadTaskDidWriteDataBlock:^(NSURLSession * _Nonnull session, NSURLSessionDownloadTask * _Nonnull downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
        
        LOG(@"%.2f / %.2f",(float)totalBytesWritten/1024.0/1024.0,(float)totalBytesExpectedToWrite/1024.0/1024.0);
    }];
    
    NSURL *URL = [NSURL URLWithString:@"http://192.168.10.155/12345/HD_DEMO_RES/CHINESE.zip"];//http://192.168.10.155/12345/HD_DEMO_RES/CHINESE.zip
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    _downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
       
    }];
    
    [_downloadTask resume];
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(suspend) userInfo:nil repeats:NO];
}

-(void)suspend
{
    //暫停下載
    [_downloadTask suspend];
    LOG(@"SUSPEND.....SUSPEND....SUSPEND....SUSPEND....SUSPEND....SUSPEND....");
    [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:@selector(resume) userInfo:nil repeats:NO];
}

-(void)resume
{//繼續下載
    [_downloadTask resume];
    LOG(@"RESUME.....RESUME.....RESUME.....RESUME.....RESUME.....RESUME.....RESUME.....");
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM