AFNetworking 进度方法

AFNetworking Progress method

我正在使用 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 块。知道可能是什么问题吗?

我建议使用以下模板来加载有进度的图像:

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];

希望对您有所帮助!