如何使用 Facebook iOS SDK 4.0 获取包含用户名和 ID 的用户好友列表
How get User's Friends list that contain Username and ids using Facebook iOS SDK 4.0
我正在尝试获取用户的好友列表。
我在 FacebookSDK 3.12 中实现的最后一个工作代码现在没有用了,因为 facebook 已经更改了他们所有的 类 甚至删除了旧的 类,下面是我的旧代码:
#pragma mark - Facebook Methods -
-(void)initiateFacebookSessionIfNot
{
if (FBSession.activeSession.isOpen)
{
[self getUserFriendsIds];
} else
{
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"user_birthday",@"friends_hometown",
@"friends_birthday",@"friends_location",
nil];
[FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status,NSError *error)
{
if (error) {
NSString *alertMessage, *alertTitle;
if (error.fberrorShouldNotifyUser) {
alertTitle = @"Something Went Wrong";
alertMessage = error.fberrorUserMessage;
} else if (error.fberrorCategory == FBErrorCategoryUserCancelled) {
NSLog(@"user cancelled login");
} else {
// For simplicity, this sample treats other errors blindly.
alertTitle = @"Unknown Error";
alertMessage = @"Error. Please try again later.";
NSLog(@"Unexpected error:%@", error);
}
if (alertMessage)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertTitle
message:error.fberrorUserMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
} else if (FB_ISSESSIONOPENWITHSTATE(status)) {
[self getUserFriendsIds];
}
}];
}
}
-(void)getUserFriendsIds
{
FBRequest *friendRequest = [FBRequest requestForGraphPath:@"me/friends?fields=name,username"];
[ friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSMutableArray *fbFriendIDs;
NSMutableArray *fbFriendsName;
NSMutableArray *fbFriendsUserName;
NSMutableArray *data = [[NSMutableArray alloc]initWithArray:[result objectForKey:@"data"]];
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (!(NSDictionary*)obj[@"username"]) {
NSDictionary *newDictionary =@{@"id": (NSDictionary*)obj[@"id"],
@"name": (NSDictionary*)obj[@"name"],
@"username": @"0"
};
[data replaceObjectAtIndex:idx withObject:newDictionary];
}
}];
fbFriendsUserName = [[NSMutableArray alloc]initWithArray:[data valueForKeyPath:@"@unionOfObjects.username"]];
fbFriendsName = [[NSMutableArray alloc]initWithArray:[data valueForKeyPath:@"@unionOfObjects.name"]];
fbFriendIDs = [[NSMutableArray alloc]initWithArray:[data valueForKeyPath:@"@unionOfObjects.id"]];
NSDictionary *queryDictionary =
@{@"user_id": userObj.user_id,
@"friend_ids":[fbFriendIDs componentsJoinedByString:@","],
@"names":[fbFriendsName componentsJoinedByString:@","],
@"usernames":[fbFriendsUserName componentsJoinedByString:@","]
};
[[RequestHandler sharedClient] CheckFBFriendsWithDictionary:queryDictionary WithCompletionBlock:^(NSArray *TUNFriendsArray, NSArray *FBFriendsArray, NSString *errorMessage) {
if (!errorMessage) {
tunFriendsArray = [[NSMutableArray alloc]initWithArray:TUNFriendsArray];
// fbFriendsArray = [[NSMutableArray alloc]initWithArray:FBFriendsArray];
[_facebookTableView reloadData];
}
else{
[ApplicationUtilities ShowAlertViewWithTitle:APP_NAME Message:errorMessage];
}
}];
}];
[SVProgressHUD dismiss];
}
无法再接收 username
字段,以及除 public_profile 之外的用户好友的所有信息,因为所有 friends_*
权限已被删除v2.0.
此外,您不会收到完整的好友列表,只会收到那些也在使用您的应用的好友。
见
- https://developers.facebook.com/docs/apps/changelog#v2_0_graph_api
- https://developers.facebook.com/docs/apps/changelog#v2_0_permissions
引述:
/me/username
is no longer available.
The /me/friends
endpoint no longer includes the full list of a person's friends. Instead, it now returns the list of that person's friends who are also using your app.
All friends_*
permissions have been removed.
Facebook 在其新版本即 V2.0 中更改了很多实现,其中 Tag-gable Friends API, Invitable Friend API,Social Context API,Business Mapping API and Tagged Places API 是新功能。
他们还进行了许多权限更改以提高安全性从以下 link
阅读更多相关信息
希望您能从上述 link 中找到所需的实施更改。
我正在尝试获取用户的好友列表。 我在 FacebookSDK 3.12 中实现的最后一个工作代码现在没有用了,因为 facebook 已经更改了他们所有的 类 甚至删除了旧的 类,下面是我的旧代码:
#pragma mark - Facebook Methods -
-(void)initiateFacebookSessionIfNot
{
if (FBSession.activeSession.isOpen)
{
[self getUserFriendsIds];
} else
{
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"user_birthday",@"friends_hometown",
@"friends_birthday",@"friends_location",
nil];
[FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status,NSError *error)
{
if (error) {
NSString *alertMessage, *alertTitle;
if (error.fberrorShouldNotifyUser) {
alertTitle = @"Something Went Wrong";
alertMessage = error.fberrorUserMessage;
} else if (error.fberrorCategory == FBErrorCategoryUserCancelled) {
NSLog(@"user cancelled login");
} else {
// For simplicity, this sample treats other errors blindly.
alertTitle = @"Unknown Error";
alertMessage = @"Error. Please try again later.";
NSLog(@"Unexpected error:%@", error);
}
if (alertMessage)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertTitle
message:error.fberrorUserMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
} else if (FB_ISSESSIONOPENWITHSTATE(status)) {
[self getUserFriendsIds];
}
}];
}
}
-(void)getUserFriendsIds
{
FBRequest *friendRequest = [FBRequest requestForGraphPath:@"me/friends?fields=name,username"];
[ friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSMutableArray *fbFriendIDs;
NSMutableArray *fbFriendsName;
NSMutableArray *fbFriendsUserName;
NSMutableArray *data = [[NSMutableArray alloc]initWithArray:[result objectForKey:@"data"]];
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (!(NSDictionary*)obj[@"username"]) {
NSDictionary *newDictionary =@{@"id": (NSDictionary*)obj[@"id"],
@"name": (NSDictionary*)obj[@"name"],
@"username": @"0"
};
[data replaceObjectAtIndex:idx withObject:newDictionary];
}
}];
fbFriendsUserName = [[NSMutableArray alloc]initWithArray:[data valueForKeyPath:@"@unionOfObjects.username"]];
fbFriendsName = [[NSMutableArray alloc]initWithArray:[data valueForKeyPath:@"@unionOfObjects.name"]];
fbFriendIDs = [[NSMutableArray alloc]initWithArray:[data valueForKeyPath:@"@unionOfObjects.id"]];
NSDictionary *queryDictionary =
@{@"user_id": userObj.user_id,
@"friend_ids":[fbFriendIDs componentsJoinedByString:@","],
@"names":[fbFriendsName componentsJoinedByString:@","],
@"usernames":[fbFriendsUserName componentsJoinedByString:@","]
};
[[RequestHandler sharedClient] CheckFBFriendsWithDictionary:queryDictionary WithCompletionBlock:^(NSArray *TUNFriendsArray, NSArray *FBFriendsArray, NSString *errorMessage) {
if (!errorMessage) {
tunFriendsArray = [[NSMutableArray alloc]initWithArray:TUNFriendsArray];
// fbFriendsArray = [[NSMutableArray alloc]initWithArray:FBFriendsArray];
[_facebookTableView reloadData];
}
else{
[ApplicationUtilities ShowAlertViewWithTitle:APP_NAME Message:errorMessage];
}
}];
}];
[SVProgressHUD dismiss];
}
无法再接收 username
字段,以及除 public_profile 之外的用户好友的所有信息,因为所有 friends_*
权限已被删除v2.0.
此外,您不会收到完整的好友列表,只会收到那些也在使用您的应用的好友。
见
- https://developers.facebook.com/docs/apps/changelog#v2_0_graph_api
- https://developers.facebook.com/docs/apps/changelog#v2_0_permissions
引述:
/me/username
is no longer available.The
/me/friends
endpoint no longer includes the full list of a person's friends. Instead, it now returns the list of that person's friends who are also using your app.All
friends_*
permissions have been removed.
Facebook 在其新版本即 V2.0 中更改了很多实现,其中 Tag-gable Friends API, Invitable Friend API,Social Context API,Business Mapping API and Tagged Places API 是新功能。 他们还进行了许多权限更改以提高安全性从以下 link
阅读更多相关信息希望您能从上述 link 中找到所需的实施更改。