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

时间:2015-10-30 17:52:59

标签: facebook swift ios9

尝试从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)")

3 个答案:

答案 0 :(得分:1)

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

"然而,人们可以控制谁可以从月份和日期分别看出他们出生的那一年,所以这个字符串只能是年份(YYYY)或月份+日期(MM / DD)&#34 ;

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

答案 1 :(得分:0)

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

答案 2 :(得分:-2)

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