AFNetworking 进度方法
Posted
技术标签:
【中文标题】AFNetworking 进度方法【英文标题】:AFNetworking Progress method 【发布时间】:2016-10-14 13:58:10 【问题描述】:我正在使用AFNetworking
发出HTTP
请求:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress)
int t=0;
t++;
success:^(NSURLSessionTask *task, id responseObject)
NSMutableArray * items = [self parseSearchCategoryWithData:responseObject];
finishBlock(items);
failure:^(NSURLSessionTask *operation, NSError *error)
finishBlock(nil);
];
我想获取 url GET 的进度,但没有调用 Progress 块。知道可能是什么问题吗?
【问题讨论】:
How to get download progress in AFNetworking 2.0?的可能重复 【参考方案1】:我建议使用以下模板来加载带有进度的图像:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFImageResponseSerializer serializer];
manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
[manager GET:url.absoluteString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress)
float progress = downloadProgress.fractionCompleted;
success:^(NSURLSessionTask *task, id responseObject)
UIImage *responseImage = responseObject;
failure:^(NSURLSessionTask *operation, NSError *error)
NSLog(Failed with error: %@", error);
];
还有这个下载进度视频的模板:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress)
float progress = downloadProgress.fractionCompleted;
destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response)
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error)
if(error)
NSLog(Error %@", error);
else
NSString *path = filePath.relativeString;
];
[downloadTask resume];
我希望这会有所帮助!
【讨论】:
以上是关于AFNetworking 进度方法的主要内容,如果未能解决你的问题,请参考以下文章
AFNetworking:如何知道响应是不是使用缓存? 304 或 200
AFNetworking 上传进度发送 SOAP NSData