Search code examples
iosfacebookfacebook-graph-apifacebook-ios-sdk

Calls to FBRequest:requestForMe: returns a nil result


I'm experimenting trying to call FBRequest:requestForMe:startWithCompletionHandler and couldn't get it to work in my own code so have tried adding it to the example project SessionLoginSample but still get the same result which is that the result is nil.

I've added to the updateView method within the example project, but it makes no difference where its placed, id is always nil:

- (void)updateView {
    // get the app delegate, so that we can reference the session property
    SLAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
    if (appDelegate.session.isOpen) {
        // valid account UI is shown whenever the session is open
        [self.buttonLoginLogout setTitle:@"Log out" forState:UIControlStateNormal];
        [self.textNoteOrLink setText:[NSString stringWithFormat:@"https://graph.facebook.com/me/friends?access_token=%@",
                                 appDelegate.session.accessTokenData.accessToken]];

        FBRequest *me = [FBRequest requestForMe];
        [me startWithCompletionHandler:^(FBRequestConnection *connection,
                                         id result,
                                         NSError *error) {
            NSDictionary<FBGraphUser> *my = (NSDictionary<FBGraphUser> *) result;
            NSLog(@"My dictionary: %@", my.first_name);
        }];


    } else {
        // login-needed account UI is shown whenever the session is closed
        [self.buttonLoginLogout setTitle:@"Log in" forState:UIControlStateNormal];
        [self.textNoteOrLink setText:@"Login to create a link to fetch account data"];
    }
}

The NSError contains domain: com.facebook.sdk code: 5.

After some googling and experimentation I got requestForMe to work by adding a call to [FBSession setActiveSession:appDelegate.session] just prior to it, however I can not transfer this working code into my own project where I get the same error code of 5, however I cannot see any difference with my project's code to that of the example project.


Solution

  • After lots of googling I managed to fix this by adding the following line just before the call to requestForMe

    [FBSession setActiveSession:appDelegate.session];