Ios8 如何在AFNetworking 2.0中通过身份验证实现图像下载?
我有以下在AFNetworking 1.0中使用的图像下载功能。这是我为AF1.0实现HTTPClient的一部分Ios8 如何在AFNetworking 2.0中通过身份验证实现图像下载?,ios8,nsurlconnection,afnetworking,xcode6,afnetworking-2,Ios8,Nsurlconnection,Afnetworking,Xcode6,Afnetworking 2,我有以下在AFNetworking 1.0中使用的图像下载功能。这是我为AF1.0实现HTTPClient的一部分 - (void)downloadImageWithCompletionBlock:(void (^)(UIImage *downloadedImage))completionBlock identifier:(NSString *)identifier { NSString* urlString = identifier; AFImageRequestOperation* ope
- (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 Web服务传递一个凭证以下载映像
如何在AFNetworking 2.0中实现上述图像下载功能?这与AFNetworking 2中的功能基本相同
AFHTTPRequestOperation
既有credential
属性,又有setWillSendRequestForAuthenticationChallengeBlock:
方法。您确实需要我复制粘贴您的代码示例,并将setAuthenticationChallengeBlock
替换为setWillSendRequestForAuthenticationChallengeBlock
?这是相同的代码。