18
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Swift] Facebook SDK iOS を使って友達のリストを取得する

Posted at

SwiftでFacebook SDKを使って友達のリストを取得する方法です。

SDKの導入からの方はこちら。
SwiftでFacebookのユーザー認証からデータ取得まで(Facebook SDK)

実装

facebookには単純に友達を取得するAPIがないので、taggable_friendsを利用して友達のリストを取得します。

パラメータを設定せずにリクエストを送ると25人分しか返ってこないので、パラメータとしてlimitに友達の数を設定することで一回で取得するようにします。
友達の数を取得するためにはfriendsを利用します。

※補足
taggable_friendsは名前の通りtaggableな友達しか取得できないので全ての友達が取得できるわけではありません。
しかし、untaggableな友達は少ないと思うので、ほとんどの友達が取得できると思います。
僕の場合は877人中859人の友達が取得できました。

それにしてもuntaggableな友達って誰なんでしょう。
わかる方いらっしゃいましたらご教授ください。

func friendsList() {

        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me/friends", parameters: nil)
        graphRequest.startWithCompletionHandler( { (connection, result, error) -> Void in
            
            if ((error) != nil)
            {
                // Process error
                println("Error: \(error)")
                return
            }
            
            
            let summary = result.valueForKey("summary") as! NSDictionary
            let counts = summary.valueForKey("total_count") as! NSNumber
            
            let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me/taggable_friends", parameters: ["fields": "id,name,picture", "limit": "\(counts)"])
            graphRequest.startWithCompletionHandler( { (connection, result, error) -> Void in
                
                if ((error) != nil)
                {
                    // Process error
                    println("Error: \(error)")
                    return
                }
                else
                {
                    let friends = result.valueForKey("data") as! NSArray
                    var count = 1
                    if let array = friends as? [NSDictionary] {
                        for friend : NSDictionary in array {
                            let name = friend.valueForKey("name") as! NSString
                            println("\(count) \(name)")
                            count++
                        }
                    }
                }       
            })
        })   
    }
18
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?