Isso porque a tarefa é executada de forma assíncrona, então o método retornará antes token
é até atribuído.
O que você deve fazer é passar um bloco de conclusão para este método e chamá -lo depois de ter um token. Dessa forma, você não bloqueia o segmento principal enquanto a solicitação está sendo executada.
É assim que você pode fazer isso:
- (void)extractTokenFromURL:(NSURL *)tokenURL completion:(void (^)(NSString *token, NSError* error))completion {
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
[[session dataTaskWithURL:self.tokenURL
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
if (httpResp.statusCode == 200) {
NSString *token = // ...;
if (token) { // we have a valid token
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(token, nil);
});
}
} else if (completion) {
// create an error indicating why the token is not valid
NSError *error = // ...;
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, error);
});
}
}
} else if (completion) {
// send the http error. you could also wrap it on your own error domain and code
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, error);
});
}
}] resume];
}
Você também pode ter dois blocos diferentes: um para sucesso e outro para falha.