试图从Facebook获得用户生日,但我没有.我做错了什么



试图从Facebook获得用户生日,但我没有。我做错了什么?

  @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)")

根据https://developers.facebook.com/docs/graph-api/reference/user,

"然而,人们可以控制谁可以将自己出生的年份与月份和日期分开查看,因此此字符串只能是年份(YYYY)或月份+日期(MM/DD)"

所以人们可以控制谁可以看到他们的生日。您可以通过API访问,也可以不访问,具体取决于个人的隐私设置。

FBSDKGraphRequest(graphPath: **"/me"**, parameters: ["fields": "name, email, **birthday**....

                let snapshotValue = requestParameters as? NSDictionary
                let id = snapshotValue?["id"] as? String
                let name = snapshotValue?["name"] as? String
                let email = snapshotValue?["email"] as? String
                let gender = snapshotValue?["gender"] as? String
                let events = snapshotValue?["events"]
                let user_birthday = snapshotValue?["birthday"]
                //let phone = snapshotValue?["phone"] as? String
                let link = snapshotValue?["link"] as? String
                let locale = snapshotValue?["locale"] as? String
                let timezone = snapshotValue?["timezone"]
                let verified = snapshotValue?["verified"]
                let last_name = snapshotValue?["last_name"] as? String
                let first_name = snapshotValue?["first_name"] as? String
                let middle_name = snapshotValue?["middle_name"] as? String
                let picture = snapshotValue?["picture.width(480).height(480)"]

最新更新