AFNetWorking3.1

2017-08-22  本文已影响22人  JingYa_Lu

                 AFNetWorking3.1下载视频文件

// 以下是下载部分的逻辑,

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

AFURLSessionManager *sessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];

//这个是自己公司的证书。

sessionManager.securityPolicy = [ZSWebInterface customSecurityPolicy];

self.task = [sessionManager downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]] progress:^(NSProgress * _Nonnull downloadProgress) {

dispatch_async(dispatch_get_main_queue(), ^{

// 这里是监测下载进度的,

//之前写的过程遇到一个问题,就是进度条一直不动,但其实数据已经更新了,后来才发现反了个错误:整个类创建后没有强引用,所以释放掉了。self.progess就一直是空的。

CGFloat progressnum = 1.0*downloadProgress.completedUnitCount/downloadProgress.totalUnitCount;

NSLog(@"下载进度 :%f",progressnum);

weakself.progressView.progress = progressnum;

});

} destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

//这里是下载后文件的存储地址是一个url  

NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];

return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];

} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {     

//下载完成文件的路径,[filePath path]这样写就可以了,之前写的是[filePath absoultestring]一直报错。

NSLog(@"filePah:%@",[filePath path]);

if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([filePath path])) {

UISaveVideoAtPathToSavedPhotosAlbum([filePath path], weakself, @selector(video:didFinishSavingWithError:contextInfo:), nil);

}

}];

[self.task resume];

}

//想要cancel这个任务。用下边的方法。

- (void)cancelDownloadTask {

if (self.task) {

[self.task cancel];

}

}

上一篇下一篇

猜你喜欢

热点阅读