Facebook SDK 收集用户信息和好友

Posted

技术标签:

【中文标题】Facebook SDK 收集用户信息和好友【英文标题】:Facebook SDK collecting user information and friends 【发布时间】:2013-03-25 19:54:23 【问题描述】:

我正在尝试使用 Facebook SDK 来收集用户信息和朋友。理想情况下,我想使用 FBRequest 而不是 FBConnectionRequest。正如 Facebook 开发者页面上所详述的那样,它说将 FBRequest 用于多个信息请求,而 Connection 仅用于一个。

可以重用一个 FBRequest 对象来发出多个连接到 Facebook。然而,每个 FBRequestConnection 实例将管理一个 连接。

Source

我目前已经设置了一个 FBRequestConnection ,它工作正常:

[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id<FBGraphUser> user, NSError *error) 

我想要实现的是收集用户信息,例如姓名、用户名和电子邮件地址等(这与上述情况很好,我已经处理了会话/具有此权限。

现在我需要将连接更改为 FBRequest,这样我可以使用以下委托方法:

- (void)request:(FBRequest *)request didLoad:(id)result 

然后在委托方法中,我将调用第二个 requestForMyFriends 并在一个方法中处理这两个大量信息。我的请求创建在我的登录块中完成,就是这样:

    FBRequest *request = [FBRequest requestForGraphpath:@"me/?fields=name,picture"];
    [request startWithCompletionHandler:NULL];

首先,这是处理这个问题的正确方法吗?

其次,当它到达 FBRequest 时我收到一个错误,所以这里肯定有问题,但我不确定什么是不正确的。我在 Facebook 上找不到更多信息,但如果有人可以使用这种方法为我指出正确的教程方向,那也会有所帮助。

编辑—— 错误

编辑 2 -- 附加代码示例

// TODO : logging can be turned off/removed
[FBSettings setLoggingBehavior:[NSSet setWithObjects:FBLoggingBehaviorFBRequests, nil]];

NSDictionary *params = @ @"fields" : @[ @"name", @"picture" ] ;
FBRequest *request = [FBRequest requestWithGraphPath:@"me"
                                          parameters:params
                                          HTTPMethod:nil]; //nil will default on GET
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) ];

【问题讨论】:

@GabrielePetronella - 更新了,虽然错误并没有告诉我太多 【参考方案1】:

您收到EXC_BAD_ACCESS 错误,因为您无法将空的完成处理程序块传递给该方法。

下面的代码已经过测试,可以按预期工作。

FBRequest *request = [FBRequest requestForGraphPath:@"me/?fields=name,picture"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) 
    NSLog(@"%@", result);
];

【讨论】:

很遗憾上述代码的第一行错误并且格式不正确? 我已经尝试不使用第一行,并将参数包含为 nil,因为我已经有一个可以在此时使用的访问令牌。不过,这确实会导致错误,包含在问题中,但对帮助没有太多帮助... 对不起,我打错了,现在应该可以正确编译了。 我相信错误可能来自我们没有提供完成处理程序的事实。尝试传递一个空块而不是 nil,就像我上面的示例一样。 不幸的是,我得到了一个 SIGBART - 原因:'-[__NSArrayI 长度]:无法识别的选择器发送到实例【参考方案2】:

仅通过读取权限获得用户权限

    let permissionsDesired = ["email", "public_profile", "user_friends"]

    let facebookLogin = FBSDKLoginManager()
    facebookLogin.logInWithReadPermissions(permissionsDesired,
                                        fromViewController: sender)
         (facebookLogin:FBSDKLoginManagerLoginResult!, facebookError:NSError!) -> Void in

            if facebookError != nil
            
                print("Facebook login. Error: \(facebookError.localizedDescription)")

                self.delegate?.permissionResults(false)
            
            else
            
                   // access granted, you can start 
                   //requesting user data now
                   self.requestUserInfo()
            
    

以下是您获得用户授予权限后如何获取用户数据

func requestUserInfo( )


    let graphPath:String            = "me"
    let httpMethod:String           = "GET"
    var listOfrequest:String = String("about")
    listOfrequest = listOfrequest .stringByAppendingString(",age_range")
    listOfrequest = listOfrequest .stringByAppendingString(",bio")
    listOfrequest = listOfrequest .stringByAppendingString(",birthday")
    listOfrequest = listOfrequest .stringByAppendingString(",context")
    listOfrequest = listOfrequest .stringByAppendingString(",cover")
    listOfrequest = listOfrequest .stringByAppendingString(",currency")
    listOfrequest = listOfrequest .stringByAppendingString(",devices")
    listOfrequest = listOfrequest .stringByAppendingString(",education")
    listOfrequest = listOfrequest .stringByAppendingString(",email")
    listOfrequest = listOfrequest .stringByAppendingString(",favorite_athletes")
    listOfrequest = listOfrequest .stringByAppendingString(",favorite_teams")
    listOfrequest = listOfrequest .stringByAppendingString(",first_name")
    listOfrequest = listOfrequest .stringByAppendingString(",gender")
    listOfrequest = listOfrequest .stringByAppendingString(",hometown")
    listOfrequest = listOfrequest .stringByAppendingString(",id")
    listOfrequest = listOfrequest .stringByAppendingString(",inspirational_people")
    listOfrequest = listOfrequest .stringByAppendingString(",installed")
    listOfrequest = listOfrequest .stringByAppendingString(",install_type")
    listOfrequest = listOfrequest .stringByAppendingString(",is_shared_login")
    listOfrequest = listOfrequest .stringByAppendingString(",is_verified")
    listOfrequest = listOfrequest .stringByAppendingString(",languages")
    listOfrequest = listOfrequest .stringByAppendingString(",last_name")
    listOfrequest = listOfrequest .stringByAppendingString(",link")
    listOfrequest = listOfrequest .stringByAppendingString(",locale")
    listOfrequest = listOfrequest .stringByAppendingString(",location")
    listOfrequest = listOfrequest .stringByAppendingString(",meeting_for")
    listOfrequest = listOfrequest .stringByAppendingString(",middle_name")
    listOfrequest = listOfrequest .stringByAppendingString(",name")
    listOfrequest = listOfrequest .stringByAppendingString(",name_format")
    listOfrequest = listOfrequest .stringByAppendingString(",payment_pricepoints")
    listOfrequest = listOfrequest .stringByAppendingString(",picture")
    listOfrequest = listOfrequest .stringByAppendingString(",political")
    listOfrequest = listOfrequest .stringByAppendingString(",public_key")
    listOfrequest = listOfrequest .stringByAppendingString(",quotes")
    listOfrequest = listOfrequest .stringByAppendingString(",relationship_status")
    listOfrequest = listOfrequest .stringByAppendingString(",religion")
    listOfrequest = listOfrequest .stringByAppendingString(",security_settings")
    listOfrequest = listOfrequest .stringByAppendingString(",shared_login_upgrade_required_by")
    listOfrequest = listOfrequest .stringByAppendingString(",significant_other")
    listOfrequest = listOfrequest .stringByAppendingString(",sports")
    listOfrequest = listOfrequest .stringByAppendingString(",test_group")
    listOfrequest = listOfrequest .stringByAppendingString(",third_party_id")
    listOfrequest = listOfrequest .stringByAppendingString(",timezone")
    listOfrequest = listOfrequest .stringByAppendingString(",updated_time")
    listOfrequest = listOfrequest .stringByAppendingString(",verified")
    listOfrequest = listOfrequest .stringByAppendingString(",video_upload_limits")
    listOfrequest = listOfrequest .stringByAppendingString(",viewer_can_send_gift")
    listOfrequest = listOfrequest .stringByAppendingString(",website")
    listOfrequest = listOfrequest .stringByAppendingString(",work")

    let params:[NSObject:AnyObject] =  ["fields": listOfrequest ]

    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: graphPath,
                                                            parameters: params,
                                                            HTTPMethod: httpMethod)


    graphRequest.startWithCompletionHandler(
         (connection:FBSDKGraphRequestConnection!, userData:AnyObject!, error:NSError!) -> Void in

            if (error == nil)
            
               print(userData) 
            
            else
            
                print("Error: \(error.localizedDescription)")
                //Handle error here
            
    )

//eom

【讨论】:

以上是关于Facebook SDK 收集用户信息和好友的主要内容,如果未能解决你的问题,请参考以下文章

我如何在 iOS sdk 的 facebook 上获取好友生日和用户生日

如何使用 Facebook iOS SDK 4.0 获取包含用户名和 ID 的用户好友列表

如何使用 facebook SDK 获取总 facebook 好友列表? [复制]

通过 Facebook iOS SDK 检索 Facebook 好友列表

Facebook iOS SDK“访问您的基本个人资料信息和好友列表”删除好友

如何使用 facebook ios sdk 发出好友请求?