0
+ (NSString *) simpleAuth {

[SimpleAuth authorize:@"instagram" completion:^(NSDictionary *responseObject, NSError *error) {
    NSLog(@"plump: %@", responseObject);
    NSString *accessToken = responseObject[@"credentials"][@"token"];


}];

return accessToken

}

trying to get my instagram accesstoken as a string so I can use it to download data in my swift viewcontroller file. I had to write simple auth in Objective C since it doesn't work for swift atm.

alemac852
  • 99
  • 1
  • 11
  • You can't do that, you have to notify your controller that you finally get the token. Using Notification or delegation – KIDdAe Oct 23 '14 at 10:17

2 Answers2

4

Since the method is run asynchronously, you cant return the access token just like that. I would suggest you to add a completion block in your simpleAuth: method which passses the access token to the callee when it gets the accesstoken.

Something like this would be a better approach,

+ (void)simpleAuth:(void(^)(NSString*))completionHandler
{
  [SimpleAuth authorize:@"instagram" completion:^(NSDictionary *responseObject, NSError *error)   {
    NSString *accessToken = responseObject[@"credentials"][@"token"];
    completionHandler(accessToken)
  }];
} 

That way you would call it like this,

[SomeClass simpleAuth:^(NSString *accessToken){
  NSLog(@"Received access token: %@", accessToken);
}];
Sandeep
  • 20,908
  • 7
  • 66
  • 106
0

It's not possible to 'return' an object from a response block. This is because response blocks run asynchronized, and thus your code continues running besides the Auth call.

To solve this you can use a delegate, or use NSNotifications. And example for NSNotifications would be:

In the listening controller add something like:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(authCompleted:)
                                                 name:@"NotificationIdentifier"
                                               object:nil];

The listening method:

-(void)authCompleted:(NSNotification *)notification {
    NSString *accessToken = [notification object];
    //now continue your operations, like loading the profile, etc
}

And in the completion block of the add:

[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationIdentifier" object: accessToken];
ejazz
  • 2,458
  • 1
  • 19
  • 29