2015-08-03 149 views

回答

1

這裏什麼工作對我來說

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:@"Username" password:@"Password" persistence:NSURLCredentialPersistenceForSession]; 
[manager setCredential:credential]; 
+0

這不再適用於AFNetworking 3 – itinance

3

要使用AFNetworking 3.0工作,可以使用下面的代碼爲AFURLSessionManager。

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];  
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

[manager setTaskDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLAuthenticationChallenge * _Nonnull challenge, NSURLCredential *__autoreleasing _Nullable * _Nullable credential) { 
    NSString *authenicationMethod = challenge.protectionSpace.authenticationMethod; 
    NSLog(@"Receive challenge: %@", authenicationMethod); 
    if ([authenicationMethod isEqualToString:NSURLAuthenticationMethodHTTPDigest]) { 
     *credential = [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession]; 
     return NSURLSessionAuthChallengeUseCredential; 
    } 
    return NSURLSessionAuthChallengeCancelAuthenticationChallenge; 
}];