How to use Graph API to retrieve Facebook user information in iOS
Ngày 20 tháng 08 năm 2016
What is Graph API?
It’s just a collection of HTTP-based APIs to get data in and out of Facebook.
Why would you want to use Graph API?
You want to obtain the users’s information to better understand them.
Graph API is simple to use. It’s already integrated inside the Facebook iOS SDK.
It’s easy to customize your requests to get the desired returned values.
There are a lot of information that you can access using the Graph API.
How to make a Graph API request?
Before you can make any Graph API requests, you need to authenticate with Facebook first. You can have a look at the previous post for step-by-step guide.
A Graph API request looks like this:
Just like any other HTTP requests, it takes 2 parameters:
graphPath: a url String to specify where the request should go.
parameters: a dictionary to specify what to include in the request.
Here is an example of initializing a Graph API request that points to the me path with no parameters:
And to send it:
Here’s typical completion handler:
In case the request succeeds, you’ll receive a result dictionary in JSON format which contains all the user information in it.
To avoid a lot of ugly code for parsing that dictionary, I highly recommend that we use the SwiftyJSON pod to make the process more smoothly. Here’s why.
Integrate SwiftyJSON to your project
First, add the SwiftyJSON pod to your Podfile:
Run pod install on your terminal.
Import it where you want to use: (Ex: HomeViewController.swift)
Alright! Let’s putting it all together
To get user public profile:
The public_profile permission allow us to access the following information:
This is the request that fetch all the above information:
The print(json) will give you this output:
Here’s how we can easily access each individual information inside the JSON:
Get user email:
To get this, you need to ask for the email permission. Open LoginViewController and add email to the permission array:
After changing permission, you have to logout and authenticate again.
Then you can make a Graph API request just like what you did with public profile:
Or you can chain all fields together and fetch at the same time:
Get user friend list
To get this, you need to ask for the user_friends permission:
Now we’ll make a request to me/friends instead of me.
This does not return all friends on the users’s account. Only those that already installed your app will be returned. If there are too many friends, Facebook only returns the first 25 results. The remaining friends can be accessed using the nextPageToken and prevPageToken.
To request for the next 25 friends:
And you can fetch the previous 25 results by using the same method, only change the parameters:
You can also change the returned limit to some bigger number instead of just 25 results:
This can be applied to fetching for next/prev page as well:
Get user avatar with different sizes:
Although you can access the picture field when fetching public profile, it only contains a very small version of the user’s avatar (50x50).
To get bigger sizes, you should make a request to me/picture with some custom parameters:
There are 4 parameters that we can consider here:
type: this is some preset sizes for the avatar. It can be one of the following:
if it is true, it will return the actual image data.
if it is false, it will return a JSON response.
width: the width of the avatar in pixels.
height: the height of the avatar in pixels.
You can specify custom width and height in the parameters and it will fall down to the closest size that match.
For example: If you set width to 300, it will return 320. If set to 400, it’ll return 480.
To get maximum size, just set width and height to some large numbers:
Today we learnt about some very basic usage of the Facebook Graph API. We then went on to fetch some pieces of user information including: public profile, email, friend list and avatar.
Please notice that this is just scratching the surface. Graph API is a powerful set of APIs that leverage the immense amount of information that billions of Facebook users generate over time. There are a whole lot of things that you can do with Graph API that I didn’t mention here. Maybe we can save it for a later post if you’re still interested.