【问题标题】:Trying to get user birthday from Facebook but I get nil. What am I doing wrong?试图从 Facebook 获取用户生日,但我得到了零。我究竟做错了什么?
【发布时间】: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

标签: facebook swift ios9


【解决方案1】:

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

“但是,人们可以控制谁可以看到他们出生的年份与月份和日期分开,因此这个字符串只能是年份 (YYYY) 或月份 + 日期 (MM/DD)”

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

【讨论】:

    【解决方案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)"]
    

    【讨论】:

    • 通常被认为是解释您的建议/答案的好方法。即使在技术上是正确的,仅代码的答案也可能会令人惊讶地缺乏信息。
    【解决方案3】:

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

    【讨论】:

    • @brimstone 请更改此行let requestParameters = ["fields" : "id, email, name, gender, **birthday**"] let userDetails = FBSDKGraphRequest(graphPath: "**/me**", parameters: requestParameters)
    猜你喜欢
    • 2013-08-06
    • 1970-01-01
    • 2016-07-18
    • 2019-12-23
    • 2011-09-22
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 2015-08-26
    相关资源
    最近更新 更多