IOS アプリの Facebook の友達リストを取得しようとしましたが、Facebook から空の応答を受け取りました。
迅速にフェイスブックの友達リストを取得するには?
IOS アプリの Facebook の友達リストを取得しようとしましたが、Facebook から空の応答を受け取りました。
迅速にフェイスブックの友達リストを取得するには?
Graph API v2.0 以降では、/me/friends を呼び出すと、同じアプリをインストールしたユーザーの友達が返されます。さらに、各ユーザーに user_friends パーミッションを要求する必要があります。user_friends は、すべてのログインにデフォルトで含まれなくなりました。/me/friends への応答に表示するには、各ユーザーが user_friends パーミッションを付与する必要があります。
アプリを使用している友達のリストを取得するには、次のコードを使用します。
let params = ["fields": "id, first_name, last_name, middle_name, name, email, picture"]
let request = FBSDKGraphRequest(graphPath: "me/friends", parameters: params)
request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error != nil {
let errorMessage = error.localizedDescription
/* Handle error */
}
else if result.isKindOfClass(NSDictionary){
/* handle response */
}
}
アプリを使用していない友達のリストにアクセスする場合は、次のコードを使用します。
let params = ["fields": "id, first_name, last_name, middle_name, name, email, picture"]
let request = FBSDKGraphRequest(graphPath: "me/taggable_friends", parameters: params)
request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error != nil {
let errorMessage = error.localizedDescription
}
else if result.isKindOfClass(NSDictionary){
/* Handle response */
}
}
フレンドリストを取得するには、ログイン時にユーザーパーミッションuser_friendsを許可する必要があります。Swift 3.0の場合、フレンド リストを取得するためのコードを以下に追加します。
let params = ["fields": "id, first_name, last_name, middle_name, name, email, picture"]
FBSDKGraphRequest(graphPath: "me/taggable_friends", parameters: params).start { (connection, result , error) -> Void in
if error != nil {
print(error!)
}
else {
print(result!)
//Do further work with response
}
}
うまくいくことを願っています!
あなたのアプリを使用している友人だけが取得されることを忘れないでください。ログイン中にuser_friendsの読み取り許可をユーザーに取得する必要があります。
var fbRequest = FBSDKGraphRequest(graphPath:"/me/friends", parameters: nil);
fbRequest.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error == nil {
println("Friends are : \(result)")
} else {
println("Error Getting Friends \(error)");
}
}
参照: https://developers.facebook.com/docs/graph-api/reference/v2.3/user/friends#read
ここで私の問題を解決する組合を作るだけです:
Dheeraj Singh から、あなたのアプリを使って友達を作るには:
var request = FBSDKGraphRequest(graphPath:"/me/friends", parameters: nil);
request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error == nil {
println("Friends are : \(result)")
} else {
println("Error Getting Friends \(error)");
}
}
そして、Meghs Dhameliya からすべての facebook の友達を取得するには、swift に移植します。
var request = FBSDKGraphRequest(graphPath:"/me/taggable_friends", parameters: nil);
request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error == nil {
println("Friends are : \(result)")
} else {
println("Error Getting Friends \(error)");
}
}
taggable_friends を使用してフレンドリストを取得できます User Taggable Friend のグラフ API リファレンス
/* make the API call */
[FBRequestConnection startWithGraphPath:@"/me/taggable_friends"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
/* handle the result */
}];
Swift - 4
これは、同じアプリを使用している Facebook の友達のみを返します。
// First you have to check that `user_friends` has associated with your `Access Token`, If you are getting false, please allow allow this field while you login through facebook.
if FBSDKAccessToken.current().hasGranted("user_friends") {
// Prepare your request
let request = FBSDKGraphRequest.init(graphPath: "me/friends", parameters: params, httpMethod: "GET")
// Make a call using request
let _ = request?.start(completionHandler: { (connection, result, error) in
print("Friends Result: \(String(describing: result))")
})
}
user_friendsパーミッションを使用すると、アプリを使用している友達のリストにアクセスできるため、アプリが友達に使用されていない場合、リストは空になります。user_friends パーミッションリファレンスを参照してください