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

Facebook location display null


I am integrating facebook login.I need to display friends name and location. name is displaying but location is showing null. The below is my code. Tell me where i am going wrong.

if (FBSession.activeSession.isOpen)
   {
        NSLog(@"active fb session");
        FBRequest* friendsRequest = [FBRequest requestForMyFriends];

        [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                                      NSDictionary* result,
                                                      NSError *error)
        {
            NSArray* friends = [result objectForKey:@"data"];
            NSLog(@"Found: %i friends", friends.count);
            for (NSDictionary<FBGraphUser>* friend in friends) {
                NSLog(@"I have a friend named %@ with id %@", friend.name, friend.location);
        }
        }];
    }

Solution

  • Add the permission: friends_location in your login code.

    Note that:

    1. API for getting the friends location: /me/friends?fields=location,name; and you're calling for default: /me/friends

    2. You'll get the location as an object:

      "location": {
         "id": "130646063637019", 
         "name": "Noida, India"
      }, 
      

      And you'll not get location for all the friends of-course (due to their privacy settings).

    Live Demo