首页 文章

用户 Profiles 中的Facebook名称

提问于
浏览
0

我刚刚在我的应用程序中实现了电子邮件登录(firebase),用户可以在文本字段中手动添加名称,当到达配置文件页面时,找到刚刚在第一个viewController中输入的名称(标签)(这是代码的一部分) of 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并进入 Profiles 页面时,如何使用这些参数来查找他的名字(即fb Profiles 名称)?我正试图找到一个解决方案,但我还没有找到任何东西 .

1 回答

  • 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
    
    }
    

    然后使用 usersRef 上的 observeSingleEvent(of: .value) 来读取您的用户配置文件数据 .

相关问题