我正在使用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块。知道可能是什么问题吗?
答案 0 :(得分:2)
我建议使用以下模板加载带有进度的图像:
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];
我希望这有帮助!