Brian Coleman

  • Home
  • ABOUT
  • SERVICES
  • PORTFOLIO
  • BLOG
  • Contact

Tutorial: Get Facebook Friends using the Facebook SDK in Swift

Note: Newer tutorial available. If you would like to learn how to implement FBSDKGraphRequest using Facebook SDK 4.0 using Swift, read Tutorial: How To Use Login in Facebook SDK 4.0 for Swift

You’ve probably noticed that most successful games contain a Facebook Leaderboard so users can compete against their friends. You can read more about Game Strategy: Make It Social in one of my previous posts.

Below is a tutorial to help you access the users friends list using the latest Facebook SDK and Graph API in Swift.

Get Friends Using Facebook Graph API v2.0

  1. Visit the Getting Started with the Facebook iOS SDK documentation to download the Facebook SDK and install it.
  2. Read and implement my previous post Tutorial: Facebook Login in Swift using a Bridging Header to handle the Facebook Login into your app using Swift.
  3. In v2.0 of the Graph API, you must request the user_friends permission from each user. user_friends is no longer included by default in every login. Each user must grant the user_friends permission in order to appear in the response to /me/friends.
  4. Apps are no longer able to retrieve the full list of a user’s friends (only those friends who have specifically authorized your app using the user_friends permission). This has been confirmed by Facebook as ‘by design’.
  5. Use the following piece of code to get the list of the users friends. This can be used anywhere within your app as long as the user has an active logged in session.
    // Get List Of Friends
    var friendsRequest : FBRequest = FBRequest.requestForMyFriends()
    friendsRequest.startWithCompletionHandler{(connection:FBRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
        var resultdict = result as NSDictionary
        println("Result Dict: \(resultdict)")
        var data : NSArray = resultdict.objectForKey("data") as NSArray
        
        for i in 0..<data.count {
            let valueDict : NSDictionary = data[i] as NSDictionary
            let id = valueDict.objectForKey("id") as String
            println("the id value is \(id)")
        }
        
        var friends = resultdict.objectForKey("data") as NSArray
        println("Found \(friends.count) friends")
    }
    
  6. If successful you’ll be returned an NSDictionary in JSON with an array of data that contains the basic data for each of the users friends who use your app. Note: Be sure to test using another Facebook account that is friends with your account.
    Result Dict: {
        data =     (
                    {
                "first_name" = Brian;
                id = ;
                "last_name" = Coleman;
                name = "Brian Coleman";
            }
        );
        paging =     {
            next = "https://graph.facebook.com/v2.0//friends?fields=id,name,first_name,last_name&format=json&access_token=&offset=5000&__after_id=";
        };
        summary =     {
            "total_count" = 1;
        };
    }
    the id value is 
    Found 1 friends
    
  7. The next step is to use the list of Facebook user id’s returned in the JSON and populate them into an array so you can compare those user id’s with the user scores in your database, then display that subset of scores in a table view leaderboard.
Learn Swift by VideoTutorial: Rate Me using UIAlertController in Swift
Brian Coleman

Manager, Mobile Development at Rogers Communications with over 15 years of multifaceted experience including development, design, business analysis and project management working directly with clients in a consulting capacity throughout the full software life cycle.

July 29, 2014 Frameworks, Swift, Tutorialsfacebook, ios8, swift, tutorial
Follow Me
    
Categories
  • About Me
  • Frameworks
  • My Apps
  • News
  • Strategy
  • Swift
  • Tools
  • Tutorials
  • tvOS
  • Uncategorized
  • Videos
  • Watch
Archives
  • May 2016
  • January 2016
  • October 2015
  • July 2015
  • May 2015
  • April 2015
  • March 2015
  • November 2014
  • October 2014
  • September 2014
  • July 2014
  • June 2014
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
Recent Posts
  • Classix for iPhone, iPad & Apple TV
  • Tutorial: How to test your app for IPv6 compatibility
  • Tutorial: Testing SSL using Charles Proxy on an iOS Device
  • Tutorial: 3D Touch – Quick Actions in Swift
  • tvOS Tutorial: Top Shelf in Swift
Featured Apps
Classix
Sportsnet
TAGS
tutorialswiftios8iosobjective-cvideogamesstrategynewsframeworkappsmonitizefacebookwatchappleios7toolstvosios9apiprovisionsocialtutorialsbooksdesignbookiapIPv6iTunes Connect
Search
TAGS
tutorialswiftios8iosobjective-cvideogamesstrategynewsframeworkappsmonitizefacebookwatchappleios7toolstvosios9apiprovisionsocialtutorialsbooksdesignbookiapIPv6iTunes Connect
ABOUT
Brian is a Lead iOS/tvOS Developer from Toronto with over 18 years of multifaceted experience including development, design, business analysis and project management.

FOLLOW ME
    
Email Subscription
Sign up for my newsletter to receive the latest news and tutorials posted.

Enter your email address:

2023 © Brian Coleman