0

In my app I have called authentication api and below is code for same

NSMutableDictionary* paraDict = [[NSMutableDictionary alloc] init];
[paraDict setObject:@"userid" forKey:@"username"];
[paraDict setObject:@"password" forKey:@"password"];

NSError *error;    
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"https://my.oktapreview.com/api/v1/authn"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60.0];

[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSData *postData = [NSJSONSerialization dataWithJSONObject:paraDict options:0 error:&error];
[request setHTTPBody:postData];

[request setHTTPShouldHandleCookies:YES];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if ([(NSHTTPURLResponse*)response statusCode] == 200) {
        aBlock(YES);
    }
}];

[postDataTask resume];

After this I am getting success response, but not able to get all cookies associated with ( When checked with PostMan with same request, have received around 12 cookies with response )

For reading cookie, I am using

for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
    NSLog(@"name: '%@'\n",   [cookie name]);
    NSLog(@"value: '%@'\n",  [cookie value]);
    NSLog(@"domain: '%@'\n", [cookie domain]);
    NSLog(@"path: '%@'\n",   [cookie path]);
}

is there way to set cookie acceptance policy.

Sagar...
  • 1,062
  • 3
  • 15
  • 35

1 Answers1

0

Perhaps you can extract the returned cookies from the response using NSHTTPCookie's -cookiesWithResponseHeaderFields:forURL: method passing in the response your data task receives.

Like so

task = [session dataTaskWithRequest:... completionHandler:^(...) {
    NSArray<NSHTTPCookie *> *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[(NSHTTPURLResponse *)response allHeaderFields] forURL:request.URL];
    // Use cookies
}];
Tim Johnsen
  • 1,471
  • 14
  • 33