想通过dataTaskWithRequest方法得到access_token然后返回,但是如何能在外部使用得到的值?
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSString *accessToken = [dictionary objectForKey:@"access_token"];
[[NSUserDefaults standardUserDefaults] setObject:[dictionary objectForKey:@"access_token"] forKey:@"access_token"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"%@",accessToken);
}];
[task resume];
Set a variable externally
If you want to use multiple methods, write them as attributes
If you use this method, just declare a variable outside the dataTadk block first