是否已经有一种途径可以通过Facebook iOS API获取安装了您的应用程序的朋友列表?我基本上需要类似于“Words with Friends”的功能,在其中可以确定哪些FB好友正在玩游戏。我有一个解决方案,需要一些额外的数据库使用,但希望也许FB API已经内置了这种查询。
FBFriendPickerDelegate
代理类,重写此函数:-(BOOL)friendPickerViewController:(FBFriendPickerViewController *)friendPicker shouldIncludeUser:(id<FBGraphUser>)user{
BOOL installed = [user objectForKey:@"installed"] != nil;
return installed;
}
对于没有安装你的应用程序的用户,installed
字段将为 nil
,但在加载数据时你必须请求它:
self.friendsController = [[FBFriendPickerViewController alloc] initWithNibName:nil bundle:nil];
//...
NSSet *fields = [NSSet setWithObjects:@"installed", nil];
self.friendPickerController.fieldsForRequest = fields;
//...
[self.friendsController loadData];
我觉得fql对我来说有点复杂,因此我使用FBRequest中的这个函数调用Graph API来解决Facebook SDK 3.5中的这个问题。
+ (FBRequest *)requestWithGraphPath:(NSString*)graphPath parameters:(NSDictionary*)parameters HTTPMethod:(NSString*)HTTPMethod;
代码如下:
FBRequest *request = [FBRequest requestWithGraphPath:@"me/friends"
parameters:@{@"fields":@"name,installed,first_name"}
HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBRequestConnection *connection,
id result,
NSError *error){
}];
我曾经使用过这段代码从用户那里获取应用程序的好友。
facebook
是随 FB Connect 一起提供的 Facebook
类。
NSString *fql = [NSString stringWithFormat:@"SELECT name,uid, pic_small FROM user WHERE is_app_user = 1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = %@) order by concat(first_name,last_name) asc",userId,userId]];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:fql ,@"query", [facebook accessToken], @"access_token", nil];
[facebook requestWithMethodName:@"fql.query" andParams:params andHttpMethod:@"GET" andDelegate:self];
- (BOOL)friendPickerViewController: shouldIncludeUser:
最新的3.5.1 Facebook SDK(2013年6月)中实现了FBFriendPickerDelegate协议的方法,根据在获取“me/friends”时的“installed”字段,询问代理是否将朋友包含在列表中。
如果朋友也是调用应用程序的用户,则“installed”字段存在(true)。
您可以像这样使用:
- (BOOL)friendPickerViewController:(FBFriendPickerViewController *)friendPicker
shouldIncludeUser:(id <FBGraphUser>)user {
return [[user objectForKey:@"installed"] boolValue];
}
重要提示!您需要包含以下内容来在请求中包含该字段,并通知friendPicker其委托:
self.fieldsForRequest = [NSSet setWithObject:@"installed"];
self.delegate = self;
...
{
"id": "100002656xxxxxx"
},
{
"installed": true,
"id": "100004366xxxxxx"
},
...
iOS SDK
中使用 FQL - user244343