The Facebook SDK for iOS includes convenience methods to access the Graph API User object. It also supports strongly-typed access to common User
properties.
The FBRequestConnection
class method startForMeWithCompletionHandler:
can be used to initiate a Graph API call for user data. This is essentially a call to the /me
Graph API endpoint. The permissions in the access_token
that are sent with the API call control the returned data (ex: if no access token is provided, only public info will be returned). See the User doc for more details on User
properties and permissions.
If the API call returns successfully, the startForMeWithCompletionHandler:
completion handler gets result data that conforms to the FBGraphUser
protocol. This provides typed access to the following fields: id
, name
, first_name
, middle_name
, last_name
, link
, username
, birthday
and location
. You have access to other user properties with NSDictionary
and NSArray
methods on the result data.
This doc outlines how to use the SDK to request user data and retrieve user details for fields available via typed access and non-typed access.
This document walks through the following:
Before you begin, make sure you set up Facebook Login. This ensures you have the prerequisites and your app is ready for additional Facebook integration.
The completed sample lets users log in with Facebook and view a sample set of their data, including their name, birthday, current city and languages. This data illustrates the following combinations:
FBGraphUser
protocolFBGraphUser
protocolThe sample builds on top of Facebook Login, adding a non-editable text area that displays the returned user data:
In this step, you'll add the text view that displays the user data.
Open the main nib file and add the text view:
Text View
object to the view. Stretch it across the screen and as far down as you can.Editable
checkbox.When you've completed this step, your implementation file should have one new outlet.
If you followed the Facebook Login doc, you should have a sessionStateChanged:
method defined in your view controller implementation file that controls the logged-in and logged-out UI. Modify this method to only show the text view when the user is authenticated:
- (void)sessionStateChanged:(NSNotification*)notification { if (FBSession.activeSession.isOpen) { [self.authButton setTitle:@"Logout" forState:UIControlStateNormal]; self.userInfoTextView.hidden = NO; } else { [self.authButton setTitle:@"Login" forState:UIControlStateNormal]; self.userInfoTextView.hidden = YES; } }
In this step, you'll configure your app to ask for permissions to display the sample set of user data. You'll ask for user_location
to get the user's current city, user_birthday
to get their birthday, and user_likes
to get their languages. The other user data you'll display are available publicly or with basic permissions.
If you followed the Facebook Login doc, you should have a openSessionWithAllowLoginUI:
method defined in your app delegate implementation file. Open the app delegate implementation file and modify the FBSession
initialization section to request additional permissions:
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI { NSArray *permissions = [[NSArray alloc] initWithObjects: @"user_location", @"user_birthday", @"user_likes", nil]; return [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:allowLoginUI completionHandler:^(FBSession *session, FBSessionState state, NSError *error) { [self sessionStateChanged:session state:state error:error]; }]; }
In this step, you'll fetch the user info when the user authenticates. Again, the user data you can access depends on the permissions a user has granted your app along with the data a user has chosen to share with apps. To get an idea of the data you can access, here's an example Graph API raw response for the permissions in the previous step:
{
bio = "Love sports of all kinds.";
birthday = "01/01/1980";
"favorite_athletes" = (
{
id = 20242388857;
name = "Usain Bolt";
}
);
"first_name" = Chris;
hometown = {
id = 106033362761104;
name = "Campbell, California";
};
id = 100003086810435;
languages = (
{
id = 108106272550772;
name = French;
},
{
id = 312525296370;
name = Spanish;
}
);
"last_name" = Colm;
link = "http://www.facebook.com/chris.colm";
locale = "en_US";
location = {
id = 104048449631599;
name = "Menlo Park, California";
};
"middle_name" = Abe;
name = "Chris Abe Colm";
timezone = "-7";
"updated_time" = "2012-08-09T03:33:32+0000";
username = "chris.colm";
verified = 1;
}
Some of the data in this response can be accessed as strongly typed properties of the properties of the FBGraphUser
protocol - example: name
and birthday
. Other properties can be accessed by selecting a key from the appropriate NSDictionary
- example: locale
and languages
. This step shows you how to access the user's name
, birthday
, languages
, location
and locale
.
Modify the sessionStateChanged:
method to do this:
... if (FBSession.activeSession.isOpen) { [self.authButton setTitle:@"Logout" forState:UIControlStateNormal]; self.userInfoTextView.hidden = NO; [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id<FBGraphUser> user, NSError *error) { if (!error) { NSString *userInfo = @""; // Example: typed access (name) // - no special permissions required userInfo = [userInfo stringByAppendingString: [NSString stringWithFormat:@"Name: %@\n\n", user.name]]; // Example: typed access, (birthday) // - requires user_birthday permission userInfo = [userInfo stringByAppendingString: [NSString stringWithFormat:@"Birthday: %@\n\n", user.birthday]]; // Example: partially typed access, to location field, // name key (location) // - requires user_location permission userInfo = [userInfo stringByAppendingString: [NSString stringWithFormat:@"Location: %@\n\n", [user.location objectForKey:@"name"]]]; // Example: access via key (locale) // - no special permissions required userInfo = [userInfo stringByAppendingString: [NSString stringWithFormat:@"Locale: %@\n\n", [user objectForKey:@"locale"]]]; // Example: access via key for array (languages) // - requires user_likes permission if ([user objectForKey:@"languages"]) { NSArray *languages = [user objectForKey:@"languages"]; NSMutableArray *languageNames = [[NSMutableArray alloc] init]; for (int i = 0; i < [languages count]; i++) { [languageNames addObject:[[languages objectAtIndex:i] objectForKey:@"name"]]; } userInfo = [userInfo stringByAppendingString: [NSString stringWithFormat:@"Languages: %@\n\n", languageNames]]; } // Display the user info self.userInfoTextView.text = userInfo; } }];
Build and run the project to make sure it runs without errors. Tap the ''Login'' button to log in with Facebook.
During the authentication step, you should see a dialog asking for the permissions you requested. Once authenticated, you should see the sample set of user data. Try displaying other user properties and become familiar with the required permissions.
If you have any issues fetching user data, enable HTTP request logging by adding this code before requesting user data:
[FBSettings setLoggingBehavior:[NSSet setWithObjects: FBLoggingBehaviorFBRequests, nil]];
This will log details about HTTP requests and response to the output pane in Xcode.
Scrumptious
: sample included in the SDK that shows an example of getting user info.