使用AFHTTPRequestOperation进行AFNetworking 2身份验证 [英] AFNetworking 2 authentication with AFHTTPRequestOperation

查看:101
本文介绍了使用AFHTTPRequestOperation进行AFNetworking 2身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过批处理操作使用AFNetworking。我想下载3个json文件。

I want to use AFNetworking with a batch operation. I want to download 3 json files.

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

How to add basic authentication with 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];

非常感谢

推荐答案

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

Use AuthenticationChallengeBlock to handle basic authentication challenge.

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

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

编辑:

另一个选择是在请求标头中通过身份验证。

Another option is to pass authentications in request header.

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];

这篇关于使用AFHTTPRequestOperation进行AFNetworking 2身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆