How to retrieve user DOB and Gender in Google Sign iOS in swift

夙愿已清 提交于 2019-12-21 21:36:00

问题


I am using google sign In in one of my app. I cant find a way to retrieve Date of Birth and Gender. Can anybody please help. Here is my code using swift -

 func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
        withError error: NSError!) {
            if (error == nil) {

                // Perform any operations on signed in user here.
                let userId = user.userID                 
                let idToken = user.authentication.idToken              
                let name = user.profile.name
                let email = user.profile.email

                print(userId)
                print(idToken)
                print(email)
                print(name)
      } else {
                print("\(error.localizedDescription)")

            }
    }

回答1:


You can only access what a user has set to public. If the user doesn't want this information public then you cant have the information.

Just because I authenticate your application doesn't mean that I want you to for example to see my address or in this case my gender. So I don't set them public you cant see them.




回答2:


self.databaseref = Database.database().reference() self.databaseref.child("user_profiles") self.databaseref.child("user_profiles").child(user!.uid).observeSingleEvent(of: .value, with: { (snapshot) in

            let snapshot = snapshot.value as? NSDictionary

            if(snapshot == nil)
            {
                self.databaseref.child("user_profiles").child(user!.uid).child("name").setValue(user?.displayName)
                self.databaseref.child("user_profiles").child(user!.uid).child("profilephoto").setValue(user?.photoURL)
                self.databaseref.child("user_profiles").child(user!.uid).child("phonenumber").setValue(user?.phoneNumber)
                self.databaseref.child("user_profiles").child(user!.uid).child("email").setValue(user?.email)
            }
            print("snapshot:",user?.displayName)
            print("snapshot:",user?.photoURL)
            print("snapshot:",user?.phoneNumber)
            print("snapshot:",user?.email)
        })
    }


来源:https://stackoverflow.com/questions/33407330/how-to-retrieve-user-dob-and-gender-in-google-sign-ios-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!