【发布时间】:2016-01-31 05:43:00
【问题描述】:
试图从 Facebook 获取用户生日,但我得到了 nil。我做错了什么?
@IBAction func SignInWithFacebookButton(sender: AnyObject) {
let permissions = ["public_profile", "email", "user_birthday"]
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions, block: {(user: PFUser?, error: NSError?) -> Void in
if(error != nil)
{
let userMessage = error!.localizedDescription
let alertAction = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
let doAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
alertAction.addAction(doAction)
self.presentViewController(alertAction, animated: true, completion: nil)
return
}
self.loadUserFacebookDetails()
})
}
func loadUserFacebookDetails(){
//Fields read from Facebook.
let requestParameters = ["fields" : "id, email, name, gender, user_birthday"]
let userDetails = FBSDKGraphRequest(graphPath: "me", parameters: requestParameters)
userDetails.startWithCompletionHandler({ (connection, result, error: NSError? ) -> Void in
if(error != nil){
let userMessage = error!.localizedDescription
let alertAction = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
let doAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
alertAction.addAction(doAction)
self.presentViewController(alertAction, animated: true, completion: nil)
//Log the user out if there is an error loading user details from facebook.
PFUser.logOut()
return
}
let userID : String = result["id"] as! String
print("User id = \(userID)")
let userFullName : String = result["name"] as! String
print("User id = \(userFullName)")
let userEmail : String = result["email"] as! String
print("User id = \(userEmail)")
let userGender : String = result["gender"] as! String
print("User id = \(userGender)")
let userBirthday : String = result["user_birthday"] as! String
print("User id = \(userBirthday)")
【问题讨论】:
-
用户需要给你 user_birthday 权限。该字段称为生日而不是 user_birthday