如何检索用户邮件和名称? (PFFacebookUtils.logInInBackgroundWithReadPermissions,swift 1.2)

时间:2015-07-27 09:29:01

标签: facebook swift

我已成功登录我的用户,使用以下代码和stackoverflow上的大量阅读:

@IBAction func FBlogin(sender: UIButton) {

    self.activity.startAnimating()

    let permissions = ["public_profile", "email"]

    PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) {
        (user: PFUser?, error: NSError?) -> Void in
        if let user = user {
            self.activity.stopAnimating()
            if user.isNew {
                print(user.email)
                print(user.username)
                print("User signed up and logged in through Facebook!")
            } else {
                print(user.email)
                print(user.username)
                print("User logged in through Facebook!")
            }
        } else {
            self.activity.stopAnimating()
        }
    }

}
  1. 这会在parse.com中创建一个新用户但是没有填写电子邮件,用户名......?
  2. 问题:如何检索电子邮件和用户名详细信息?

    在搜索和摆弄更多内容后,我添加了以下代码:

    func getUserDataFromFacebookProfile(user: PFUser)
    {
    
        var facebookid : String?
        var username  : String?
        var userEmail : String?
        var userGender : String?
        var userLocation : String?
    
    
        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
    
            if ((error) != nil)
            {
                // Process error
                print("graphrequest error: \(error)")
            }
            else
            {
                //if this works, we can store user name und PFUser and mail in    PFuser....
                //not working yet
                /*
    
                id
                name
                first_name
                last_name
                link
                gender
                locale
                timezone
                updated_time
                verified
    
    
                */
    
                facebookid = result.valueForKey("id") as? String
                userEmail = result.valueForKey("email") as? String
                username = result.valueForKey("name") as? String
                let fname = result.valueForKey("first_name") as? String
                let lname = result.valueForKey("last_name") as? String
                let loc = result.valueForKey("locale") as? String
                let gend = result.valueForKey("gender") as? String
    
    
                //userGender = result.valueForKey("gender") as? String
                userLocation = result.valueForKey("location") as? String
    
                print("*****************************************************************************")
                print("")
                print("graphrequest result  \(facebookid), \(username), \(userEmail), \(gend), \(fname), \(lname)")
                print("")
                print("*****************************************************************************")
    
    
            }
    
            //persist in our DB for later
            let thisUser: PFUser = user
    
            print("updating current user")
            if let uName = username {
                thisUser.username = uName
            }
    
            if let uEmail = userEmail {
                thisUser.email = uEmail
            }
    
            thisUser.saveInBackground()
        })
    }
    

    奇怪的是我只能检索身份证,电子邮件和姓名?!

2 个答案:

答案 0 :(得分:0)

关于电子邮件:搜索"声明字段"在更改日志中:https://developers.facebook.com/docs/apps/changelog#v2_4

关于用户名:自图谱API的v2.0起,该字段不再可用:https://developers.facebook.com/docs/apps/changelog#v2_0

答案 1 :(得分:0)

所以这对我有用。显然FB再次改变了一些事情。关键是将必填字段添加到参数列表中,如下所示:

let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath:  "me", parameters: ["fields": "email,first_name,last_name,gender,picture.width(480).height(480)"])