我刚刚在我的应用中实现了电子邮件登录(firebase),用户可以在文本字段中手动添加名称,当到达配置文件页面时,找到刚刚在第一个viewController中输入的名称(标签)(这是部分) profileViewController的代码)
override func viewDidLoad() {
super.viewDidLoad()
self.user = Auth.auth().currentUser
self.databaseRef.child("user_profiles").child(self.user!.uid).observeSingleEvent(of: .value) { (snapshot:DataSnapshot) in
let snapshotValue = snapshot.value as? NSDictionary
self.name.text = snapshotValue?["name"] as? String
self.handle.text = snapshotValue?["handle"] as? String
if(snapshotValue?["about"] != nil){
self.usernameField.text = snapshotValue?["about"] as? String
}
if(snapshotValue?["profile_pic"] != nil){
let databaseProfilePic = snapshotValue!["profile_pic"] as! String
let data = try? Data(contentsOf: URL(string: databaseProfilePic)!)
self.setProfilePicture(imageView: self.ProfilePicture,imageToSet:UIImage(data:data!)!)
}
self.imageLoader.stopAnimating()
}
}
与Facebook登录不同的是,用户不输入任何数据,但我接受了这个
let graphRequest:FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"first_name,email, picture.type(large)"])
来自FBSDK,那么当用户登录Facebook并进入个人资料页面时,如何使用这些参数来查找他的名字(即fb个人资料名称)?我试图找到一个解决方案,但我没有找到任何东西。
答案 0 :(得分:0)
创建用户帐户并通过以下方法登录后:
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth()?.signIn(with: credential)
您应该能够使用以下方式访问用户基本个人资料信息:
Auth.auth()?.currentUser
例如,要获取用户名,您可以执行以下操作:
guard let currentUser = Auth.auth()?.currentUser else {
return
}
userNameLabel.text = currentUser.displayName
当您首次通过signIn方法创建用户帐户时,您也可以在数据库中写入所有用户数据,如下所示:
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
// Create and sign in the user account using the Facebook access token.
Auth.auth()?.signIn(with: credential) { (currentUser, error) in
guard error == nil else {
return
}
// Create an array of user data using authenticationData.
let currentUserData: [String: String] = ["name" : authenticationData.providerData["displayName"],
"email": authenticationData.providerData["email"] ]
// Write user data in the database.
usersRef.updateChildValues(currentUserData, withCompletionBlock: { (error, ref) in
guard error == nil else {
return
}
// ...Segue to your home controller here
}
然后使用observeSingleEvent(of: .value)
上的usersRef
来阅读您的用户个人资料数据。