AFNetworking 2 使用 AFHTTPRequestOperation 进行身份验证

Posted

技术标签:

【中文标题】AFNetworking 2 使用 AFHTTPRequestOperation 进行身份验证【英文标题】:AFNetworking 2 authentication with AFHTTPRequestOperation 【发布时间】:2013-10-25 10:33:02 【问题描述】:

我想将 AFNetworking 与批处理操作一起使用。我要下载3个json文件。

如何使用 AFHTTPRequestOperation 添加基本身份验证?

NSMutableArray *mutableOperations = [NSMutableArray array];

for (NSString *fileURL in filesToDownload) 
    NSURLRequest *request = [NSURLRequest 
                                requestWithURL:[NSURL URLWithString:fileURL]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                                initWithRequest:request];
    [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation
                                                , id responseObject) 
        NSLog(@"success: %@", operation.responseString);
    
    failure:^(AFHTTPRequestOperation *operation, NSError *error) 
        NSLog(@"error: %@",  operation.responseString);
    ];
    [mutableOperations addObject:operation];


NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:mutableOperations 
                                    progressBlock:^(NSUInteger numberOfFinishedOperations
                                        , NSUInteger totalNumberOfOperations) 
    NSLog(@"%d of %d complete", numberOfFinishedOperations, totalNumberOfOperations);
 completionBlock:^(NSArray *operations) 
    NSLog(@"All operations in batch complete");
];

[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];

非常感谢

【问题讨论】:

【参考方案1】:

使用AuthenticationChallengeBlock 处理基本身份验证质询。

[operation setAuthenticationChallengeBlock:
        ^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) 
   NSURLCredential *cred = [NSURLCredential 
    credentialWithUser:@"username" 
    password:@"password" 
    persistence:NSURLCredentialPersistenceForSession];

   [[challenge sender] useCredential:cred forAuthenticationChallenge:challenge];
];

编辑:

另一种选择是在请求标头中传递身份验证。

NSURLMutableRequest *request = [NSURLMutableRequest
                                requestWithURL:[NSURL URLWithString:fileURL]];
NSData* authData = [[[NSString 
    stringWithFormat:@"username:password"] 
        stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] 
            dataUsingEncoding: NSASCIIStringEncoding];
NSString *finalAuth = [authData base64EncodedString];
finalAuth = [NSString stringWithFormat:@"Basic %@",finalAuth];
[request setValue:finalAuth forHTTPHeaderField:@"Authorization"];

另一种解决方案:

NSURLCredential *credential = [NSURLCredential 
    credentialWithUser:@"login" 
    password:@"password" 
    persistence:NSURLCredentialPersistenceForSession];

[operation setCredential:credential];

【讨论】:

谢谢,AuthenticationChallengeBlock 在 AFNeworking 2 中不再存在 感谢它的工作。我找到了另一个解决方案:NSURLCredential *credential = [NSURLCredential credentialWithUser:_loginField.text password:_passwordField.text persistence:NSURLCredentialPersistenceForSession]; [operation setCredential:credential]; @mdespeuilles 编辑答案并添加您的解决方案。这将对其他人有所帮助。 感谢您的帮助 :-)

以上是关于AFNetworking 2 使用 AFHTTPRequestOperation 进行身份验证的主要内容,如果未能解决你的问题,请参考以下文章

AFNetworking 2.0 - 是不是使用子类?

AFNetworking 2 使用 AFHTTPRequestOperation 进行身份验证

AfNetworking 2.0 发布问题

AFNetworking 2.0,使用 NSURLSession 下载图片

iOS开发网络数据之AFNetworking使用

使用 AFNetworking 2.0 加载图像