我想使用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];
非常感谢你。
AuthenticationChallengeBlock
在AFNetworking 2中已经不存在了。 - mdespeuillesNSURLCredential *credential = [NSURLCredential credentialWithUser:_loginField.text password:_passwordField.text persistence:NSURLCredentialPersistenceForSession]; [operation setCredential:credential];
- mdespeuilles