AFNetworking 2.0 and HTTP Basic Authentication

Cannot find AFHTTPClient in AFNetworking 2.0 to use:

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://examplewebsite.com]]; [client setAuthorizationHeaderWithUsername:@"username" password:@"password"]; 

How does he need to manage in AFNetworking 2.0?

+44
ios7 afnetworking-2
Sep 30 '13 at 19:37
source share
3 answers

The new ARNetworking 2.0 architecture uses serializers to create queries and analyze parsing. To configure the authorization header, you must first initialize the request operation manager, which replaces AFHTTPClient, create a serializer, and then call the dedicated method to set the header.

For example, the code would look like this:

 AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://examplewebsite.com"]]; manager.requestSerializer = [AFHTTPRequestSerializer serializer]; [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"userName" password:@"password"]; 

You should read the documentation and the migration guide to understand the new concepts that come with AFNetworking version 2.0.

+95
01 Oct '13 at 8:48
source

Here is an example of basic HTTP authentication with AFNetworking 2.0 using NSURLCredential. The advantage of this approach is to use the AFHTTPRequestSerializer setAuthorizationHeaderFieldWithUsername:password: method that you can automatically save the username and password in the keychain by changing the persistence: parameter for NSURLCredential. (See this answer .)

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSURLCredential *credential = [NSURLCredential credentialWithUser:@"user" password:@"passwd" persistence:NSURLCredentialPersistenceNone]; NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"GET" URLString:@"https://httpbin.org/basic-auth/user/passwd" parameters:nil]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setCredential:credential]; [operation setResponseSerializer:[AFJSONResponseSerializer alloc]]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failure: %@", error); }]; [manager.operationQueue addOperation:operation]; 
+15
Jan 05 '14 at
source

As @gimenete mentions that multiple requests will fail when using the @titaniumdecoy credential approach, as this applies in the request block, and the current version of AFNetworking has a problem with this. Instead of using a credential approach, you can embed authentication in the NSMutableRequest header

  NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"PUT" URLString:path parameters:myParams constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { [formData appendPartWithFileData:imageData name:imageName fileName:imageName mimeType:@"image/jpeg"]; } error:&error]; NSString *authStr = [NSString stringWithFormat:@"%@:%@", [self username], [self password]]; NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding]; NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedString]]; [request setValue:authValue forHTTPHeaderField:@"Authorization"]; 

If you need to use a third-party BASE64 encoding library such as NSData + Base64.h and .m File from Matt Gallaghers to solve ARC BASE64

+6
Jul 12 '14 at
source



All Articles