如何在AFNetworking 2.0中实现带身份验证的图像下载?

时间:2014-10-10 23:14:57

标签: ios8 nsurlconnection afnetworking xcode6 afnetworking-2

我在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中实现上述图像下载功能?

1 个答案:

答案 0 :(得分:-1)

AFNetworking 2中的情况大致相同。AFHTTPRequestOperation同时拥有credential属性和setWillSendRequestForAuthenticationChallengeBlock:方法。