我在AFNetworking 1.0中有以下图像下载功能。这是我为AFNetworking 1.0实现HTTPClient的一部分。
- (void)downloadImageWithCompletionBlock:(void (^)(UIImage *downloadedImage))completionBlock identifier:(NSString *)identifier {
NSString* urlString = identifier;
AFImageRequestOperation* operation = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
//LogInfo(@"SUCCESS GETTING PHOTO: %@", response);
completionBlock(image);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
LogInfo(@"ERROR GETTING PHOTO IN downloadImageWithCompletionBlock.");
}];
[operation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:self.strSPUser password:self.strSPPW persistence:NSURLCredentialPersistenceForSession];
[challenge.sender useCredential:newCredential forAuthenticationChallenge:challenge];
}];
[self enqueueHTTPRequestOperation:operation];
}
我找不到为AFNetworking 1.0编写的HTTPClient自定义代码轻松过渡/升级到AFNetworking 2.0。正如您在函数中看到的那样,我将凭据传递给我的restful webservice以下载图像。
如何在AFNetworking 2.0中实现上述图像下载功能?
答案 0 :(得分:-1)
AFNetworking 2中的情况大致相同。AFHTTPRequestOperation
同时拥有credential
属性和setWillSendRequestForAuthenticationChallengeBlock:
方法。