您不能直接从 facebook 获取所有朋友,因为它不允许这些。但是您可以使用以下内容,但是您将无法获得该朋友的 facebook id,您只能获得姓名和个人资料图片
注意:您需要获得 facebook 的批准才能使用 taggable_friends。
var request = FBSDKGraphRequest(graphPath:"/me/taggable_friends", parameters: nil);
您需要检查以下是否成功授予您的权限,然后您可以检查好友列表。
if (FBSession.activeSession.state == FBSessionStateOpen
|| FBSession.activeSession.state == FBSessionStateOpenTokenExtended)
{
[self loadFacebookFriends];
}
else
{
[FBSession openActiveSessionWithReadPermissions:@[@"public_profile,email,user_friends"] allowLoginUI:YES completionHandler:
^(FBSession *session, FBSessionState state, NSError *error)
{
if (!error)
{
if (state == FBSessionStateOpen)
{
[FBSession setActiveSession:session];
[self loadFacebookFriends];
}
else
{
NSLog(@"Status :%ld",(unsigned long)state);
}
}
else
{
// Could not connect
}
}];
}
-(void)loadFacebookFriends
{
// SEND REQUEST FOR FRIEND LISTs
FBRequest* friendsRequest = [FBRequest requestForGraphPath:@"/me/friends"];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,NSDictionary* result,NSError *error)
{
[appDelegate activityHide];
if (error)
{
return ;
}
NSArray* friends = [result objectForKey:@"data"];
NSLog(@"Found: %lu friends", (unsigned long)friends.count);
}];
}
**注意:如果您使用的是 iOS 9,您需要在 info.plist 中为您的应用授予以下权限。您也可以参考此链接https://developers.facebook.com/docs/ios/ios9
**
在您的 info.plist 中添加以下 Transport Securtiy
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>graph.facebook.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
<key>facebook.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
<key>fbcdn.net</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>akamaihd.net</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
</dict>
</dict>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fbapi20130214</string>
<string>fbapi20130410</string>
<string>fbapi20130702</string>
<string>fbapi20131010</string>
<string>fbapi20131219</string>
<string>fbapi20140410</string>
<string>fbapi20140116</string>
<string>fbapi20150313</string>
<string>fbapi20150629</string>
<string>fbauth</string>
<string>fbauth2</string>
<string>fb-messenger-api20140430</string>
<string>fb-messenger-platform-20150128</string>
<string>fb-messenger-platform-20150218</string>
<string>fb-messenger-platform-20150305</string>
</array>