在我的应用中,我使用AFHTTPRequestOperationManager
进行API集成及其工作。我需要显示数据下载的进度,并使用setCompletionBlockWithSuccess
回调进行检查,但未调用。如何在AFHTTPRequestOperationManager
。
请帮帮我。
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager manager] initWithBaseURL:[NSURL URLWithString:@""]];
[manager.requestSerializer setValue:SourceType forHTTPHeaderField:@"Source"];
[manager.requestSerializer setValue:sig forHTTPHeaderField:@"Sig"];
[manager GET:mtdName parameters:params success:^(AFHTTPRequestOperation *operation, id responseDict)
{}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{}];
答案 0 :(得分:0)
我发现: http://cocoadocs.org/docsets/AFNetworking/2.2.3/
Creating a Download Task
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
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) {
NSLog(@"File downloaded to: %@", filePath);
}];
[downloadTask resume];
答案 1 :(得分:0)
使用此:
AFHTTPRequestOperation *operation;
operation =
[manager HTTPRequestOperationWithRequest:[[AFJSONRequestSerializer serializer] requestWithMethod:method URLString:url parameters:info] success:^(AFHTTPRequestOperation *operation, id responseObject) {
}];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {
//do something here.
}];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.securityPolicy.allowInvalidCertificates = YES ;//if use https
[manager.operationQueue addOperation:operation];