i'm trying to make an iOS App that communicates with an API using AFHTTPClient
. The first step is to authentificate the user, to do so, i use this code:
-(void)authorize
{
NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"login.php" parameters:@{@"login": account.username, @"passwd":account.password}];
AFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
[httpClient setDefaultHeader:@"Token" value:[[operation.response allHeaderFields] objectForKey:@"CSRF-Token"]];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error.localizedDescription);
}];
[httpClient enqueueHTTPRequestOperation:operation];
[httpClient.operationQueue waitUntilAllOperationsAreFinished];
}
As you can see, my code gets a token from the server response and sets it as a default header for all future requests.
I then proceed to other requests using - (void)postPath:(NSString *)path parameters:(NSDictionary *)parameters success:success failure:failure
But when i used the debugger, i found out that those other requests where executed before the authorize operation was completed, therefore they failed because they didn't have the auth token. I added [httpClient.operationQueue waitUntilAllOperationsAreFinished];
but it doesn't seem to work...
Thank you for your help