2014-10-10 80 views
-1

我有以下圖像下載功能,在AFNetworking 1.0工作。這是我爲AFNetworking 1.0實施HTTPClient的一部分。如何在AFNetworking 2.0中使用身份驗證實現圖像下載?

- (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。正如你可以在函數中看到的,我將一個憑證傳遞給我寧靜的web服務來下載圖像。

如何在AFNetworking 2.0中實現上述圖像下載功能?

回答

-1

這在AFNetworking 2中幾乎是一回事。AFHTTPRequestOperation同時具有credential屬性和setWillSendRequestForAuthenticationChallengeBlock:方法。

+0

AFNetworking 2.0應該如何完成的任何例子? – motionpotion 2014-10-14 06:21:06

+0

你真的需要我複製粘貼你的代碼示例,並用'setWillSendRequestForAuthenticationChallengeBlock'替換'setAuthenticationChallengeBlock'?這是相同的代碼。 – mattt 2014-10-17 13:44:16