New version of SDK is 4.16.x(Swift) and Graph API Version is 2.8.
With the Graph API 2.6 Facebook has given support to fetch (read only) the Reactions on Timeline Posts.
Post is updated for iOS 10 and Swift 3
API documentation is available here.
Let’s learn how to get reactions from the post
Install pods
1 2 3 4 5 6 7 8 9 |
# Uncomment the next line to define a global platform for your project platform :ios, '9.0' target 'FBSwiftLogin' do # Comment the next line if you're not using Swift and don't want to use dynamic frameworks use_frameworks! # Pods for FBSwiftLogin pod 'FacebookCore' pod 'FacebookLogin' end |
Login with Facebook
I have already written a tutorial on Facebook Login.
Get Facebook Posts via Graph API
We can get the Facebook post via graph API : /me/posts for that we have to add user_posts permission with login.
1 2 3 4 5 6 7 |
func getFacebookUserPosts() { FBSDKGraphRequest(graphPath: "/me/posts", parameters: nil, httpMethod: "GET").start(completionHandler: { (connection, result, error) in if (error == nil){ print(result) } }) } |
Get Reactions from one of the Post
We can get the Facebook post reactions via graph API : /{post-id}/reactions. We have to pass parameters like fields and summary as described in getReactions function.
Note : Here I am writing sample for only first post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
func getFacebookUserPosts() { var strGraphPath : String = "" FBSDKGraphRequest(graphPath: "/me/posts", parameters: nil, httpMethod: "GET").start(completionHandler: { (connection, result, error) in if (error == nil){ print(result) let data = (result as! [String : Any])["data"] as! [[String : Any]] if(data.count > 0) { let strId = data[0]["id"] as! String strGraphPath = "/"+strId+"/reactions" self.getReactions(strGraphPath) } } }) } func getReactions(_ strGraphPath:String) { FBSDKGraphRequest(graphPath: strGraphPath, parameters: ["fields":"id, name, type", "summary":"total_count, viewer_reaction"], httpMethod: "GET").start(completionHandler: { (connection, result, error) in if (error == nil){ print(result) } }) } |
Response should be like
Type of reactions : NONE, LIKE, LOVE, WOW, HAHA, SAD, ANGRY
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
{ data = ( { id = ACCOUNT_ID; name = "NAME"; type = HAHA; }, { id = ACCOUNT_ID; name = "NAME"; type = LIKE; } ); paging = { cursors = { after = REFERENCE_ID; before = REFERENCE_ID; }; next = "NEXT_REFERENCE_LINK"; }; summary = { "total_count" = 56; "viewer_reaction" = NONE; }; } |
#Like #Share #React 🙂 😛 😀
Happy coding 🙂