Using Parse to login via Facebook and retrieve Profile and Friends

I’m using parse.com to handle user authentication, either via Facebook or custom user type with email and password.

If a user signs up via Facebook we want to request access to their profile and friend list and use that information to find FB friends who are already playing our game (Just Picture It).

This example doesn’t include a lot of the prerequisite steps to get your parse app working with Facebook – see the parse.com docs for more details on that.

An example of an initial call to parse to login to Facebook with a specific set of permissions.

NSArray *permissionsArray = [NSArray arrayWithObjects:@"user_about_me",
@"user_relationships",@"user_birthday",@"user_location",
@"offline_access", @"email", @"publish_stream", nil];

[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {

if (!user) {
   NSLog(@"Uh oh. The user cancelled the Facebook login.");

   [self doFacebookUserHasCanceled];

   } else {

     [self performSelector:@selector(doFaceBookUserHasLoggedIn) withObject:nil afterDelay:0.1];
     }
}];

A successful login and permission request approval by the user to Facebook sends us to the function doFaceBookUserHasLoggedIn.

NSString *requestPath = @"me/?fields=name,location,gender,birthday,relationship_status,picture,email,id";

PF_FBRequest *request = [[PF_FBRequest alloc] initWithSession:[PFFacebookUtils session] graphPath:requestPath];

[request startWithCompletionHandler:^(PF_FBRequestConnection *connection, id result, NSError *error) {
    if (!error) {
       NSDictionary *userData = (NSDictionary *)result; // The result is a dictionary

       NSString *name = [userData objectForKey:@"name"];

       NSString *email = [userData objectForKey:@"email"];

       NSString *sID = [userData objectForKey:@"id"];

       // get the FB user's profile image
       NSDictionary *dicFacebookPicture = [userData objectForKey:@"picture"];   
       NSDictionary *dicFacebookData = [dicFacebookPicture objectForKey:@"data"];       
       NSString *sUrlPic= [dicFacebookData objectForKey:@"url"];
       UIImage* imgProfile = [UIImage imageWithData:
                                [NSData dataWithContentsOfURL:
                                 [NSURL URLWithString: sUrlPic]]];

       //do something interesting with this data...

       //...

       // now request FB friend list
       PF_FBRequest *request = [[PF_FBRequest alloc] initWithSession:[PFFacebookUtils session] graphPath:@"me/friends"];

       [request startWithCompletionHandler:^(PF_FBRequestConnection *connection, id result, NSError *error) {
          if (!error) {
             NSArray *data = [result objectForKey:@"data"];

             if (data) {
                //we now have an array of NSDictionary entries contating friend data
                for (NSMutableDictionary *friendData in data) {
                    // do something interesting with the friend data...

                    }
                }

             }
          }];
   }
}];

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s