How to access property in sub method after it runs?
Posted
by
Warren
on Stack Overflow
See other posts from Stack Overflow
or by Warren
Published on 2014-06-09T03:15:30Z
Indexed on
2014/06/09
3:25 UTC
Read the original article
Hit count: 129
objective-c
|xcode
I'm having a hard time working this one out but I think it should be pretty simple. Basically, I have this method which talks to a webservice and I need to return some data from the sub method, the "authCode". What am I doing wrong? How can I get the authCode out of the manager, or can I create a block or something to to ensure that the manager block runs first? Am I even using the right words - blocks, sub methods??? Please help :)
- (NSString *)getAuthCodeEXAMPLE {
__block NSString *returnString = @"nothing yet!";
NSURL *baseURL = [NSURL URLWithString:BaseURLString];
NSDictionary *parametersGetAuthCode = @{@"req": @"getauth"};
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:APIscript parameters:parametersGetAuthCode success:^(NSURLSessionDataTask *task, id responseObject) {
if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *r = (NSHTTPURLResponse *)task.response;
if ([r statusCode] == 200) {
self.returnedData = responseObject;
NSString *authCode = [self.returnedData authcode];
NSLog(@"Authcode: %@", authCode);
returnString = authCode;
}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alertView show];
}];
//this currently returns "nothing yet!"
return returnString;
}
© Stack Overflow or respective owner