我曾尝试获取IOS应用程序的facebook好友列表,但收到来自facebook的空响应。
如何在swift中获取脸书好友列表?
在Graph API v2.0或更高版本中,调用/me/friends会返回安装了相同应用程序的用户的好友。此外,您必须向每个用户请求user_friends权限。默认情况下,每次登录都不再包含user_friends。每个用户都必须授予user_friends权限才能出现在对/me/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)");
}
}
并获取所有facebook好友,从Meghs Dhamelya,移植到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获取好友列表图形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权限参考