Using NSURLSession delegate for authorization

When URLSession:didReceiveChallenge:completionHandler: method of NSURLSessionDelegate is called? Did it called when I get response with 403 status code?

Can I use this delegate method for authorization if I should change request body for second request after authorization? (I should change @"ticket")

NSURLSession *session = [NSURLSession sharedSession];
NSError *error;
NSDictionary *mapData = @{
                          @"userIdentity": @{
                                  @"ticket": [SecretStorage sharedInstance].ticket,
                                  @"hotelId": [SecretStorage sharedInstance].hotelId,
                                  @"language": @"ru"
                                  }
                          };
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"example.com"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60.f];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                                                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                                                NSLog(@"%@", json);
                                            }];
[dataTask resume];

Upvotes: 0

Views: 618

Answers (1)

user5917312
user5917312

Reputation:

There are two different challenge/response handlers in NSURLSession's delegates. The first, which you're implementing, is at the session level, and basically handles server-level authentication.

-For session-level challenges—NSURLAuthenticationMethodNTLM, NSURLAuthenticationMethodNegotiate, NSURLAuthenticationMethodClientCertificate, or NSURLAuthenticationMethodServerTrust—the NSURLSession object calls the session delegate’s URLSession:didReceiveChallenge:completionHandler: method. If your app does not provide a session delegate method, the NSURLSession object calls the task delegate’s URLSession:task:didReceiveChallenge:completionHandler: method to handle the challenge.

-For non-session-level challenges (all others), the NSURLSession object calls the session delegate’s URLSession:task:didReceiveChallenge:completionHandler: method to handle the challenge. If your app provides a session delegate and you need to handle authentication, then you must either handle the authentication at the task level or provide a task-level handler that calls the per-session handler explicitly. The session delegate’s URLSession:didReceiveChallenge:completionHandler: method is not called for non-session-level challenges.

So, you probably want to handle task-level authentication by adding protocol support for NSURLSessionTaskDelegate in your delegate object, and supplying a handler at the task level, i.e. URLSession(_:task:didReceiveChallenge:completionHandler:).

For more information go to this link

Upvotes: 2

Related Questions