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

Can't get permissions for user_birthday and user_hometown in facebook-ios-sdk


I am not able to get the permissions for user_birthday and user_hometown. I tried it with this code before but it only asks for the public profile and ignores others.

[FBSession openActiveSessionWithReadPermissions:@[@"public_profile",@"user_birthday",@"user_hometown"]
                                           allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (error) {
 NBAppDelegate* appDel = (NBAppDelegate*)`[UIApplication sharedApplication].delegate;
 [appDel sessionStateChanged:session state:status error:error];
}

if ([session isOpen]) {
    [self loginWithFBToken:session name:sender];
}

}];

Then someone suggested to ask for additional permissions after getting the public profile, so i even tried that to no good.

Here is the code for that

- (void)loadFbDetails
{
    NSArray *permissionsNeeded = @[@"user_hometown", @"user_birthday"];
    // Request the permissions the user currently has
    [FBRequestConnection startWithGraphPath:@"/me/permissions"
       completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                          if (!error){
                              // These are the current permissions the user has:
                              NSDictionary *currentPermissions= [(NSArray *)[result data] objectAtIndex:0];

                              // We will store here the missing permissions that we will have to request
                              NSMutableArray *requestPermissions = [[NSMutableArray alloc] initWithArray:@[]];

                              // Check if all the permissions we need are present in the user's current permissions
                              // If they are not present add them to the permissions to be requested
                              for (NSString *permission in permissionsNeeded){
                                  if (![currentPermissions objectForKey:permission]){
                                      [requestPermissions addObject:permission];
                                  }
                              }

                              // If we have permissions to request
                              if ([requestPermissions count] > 0){
                                  // Ask for the missing permissions
                                  [FBSession.activeSession
                                   requestNewReadPermissions:requestPermissions
                                   completionHandler:^(FBSession *session, NSError *error) {
                                       if (!error) {
                                           // Permission granted
                                           NSLog(@"new permissions %@", [FBSession.activeSession permissions]);
                                           // We can request the user information
                                           [self makeRequestForUserData];
                                       } else {
                                           // An error occurred, we need to handle the error
                                           // See: https://developers.facebook.com/docs/ios/errors
                                       }
                                   }];
                              } else {
                                  // Permissions are present
                                  // We can request the user information
                                  [self makeRequestForUserData];
                              }

                          } else {
                              // An error occurred, we need to handle the error
                              // See: https://developers.facebook.com/docs/ios/errors
                          }
                      }];

}

-(void)makeRequestForUserData
{
[FBRequestConnection startWithGraphPath:@"me?fields=birthday,hometown"
                      completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                          if (!error) {
                              // Sucess! Include your code to handle the results here
                              NSLog(@"user events: %@", result);
                          } else {
                              NSLog(@"error: %@" , error);
                          }
                      }];
}

All it does is recursively go between my ios app and the fb native app, returning only with the public_profile in the permissions array.

Looks like i am missing something?


Solution

  • It should work if you're using an admin user of the app. Once you want to use the extended permissions with other users, you have to get your app reviewed by Facebook first.

    See my answer here: facebook extended permission