gpt4 book ai didi

ios - Cloud Firestore 身份验证 iOS

转载 作者:行者123 更新时间:2023-11-28 14:02:47 24 4
gpt4 key购买 nike

我是 iOS 开发的新手(边走边学),我正在使用 Cloud Firestore 通过电子邮件/密码对用户进行身份验证。我正在尝试进行简单的电子邮件和密码身份验证。我使用了文档中的数据库规则之一,我认为我不必在规则中创建集合和文档。我已经测试了我与数据库的连接,所以这也不是问题。我到目前为止的代码如下:

import UIKit
import Firebase


class SignUpViewController: UIViewController {

//Outlets
@IBOutlet weak var firstNameText: UITextField!
@IBOutlet weak var lastNameText: UITextField!
@IBOutlet weak var emailText: UITextField!
@IBOutlet weak var passwordText: UITextField!
@IBOutlet weak var signUpButton: UIButton!


override func viewDidLoad() {
super.viewDidLoad()
}

@IBAction func signUpButtonTapped(_ sender: Any) {
guard let firstName = firstNameText.text,
let lastName = lastNameText.text,
let email = emailText.text,
let password = passwordText.text else { return }

Auth.auth().createUser(withEmail: email, password: password) { (authResult, error) in
if let error = error {
debugPrint("Error creating user: \(error.localizedDescription)")
}
let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
changeRequest?.displayName = firstName
changeRequest?.commitChanges(completion: { (error) in
if let error = error {
debugPrint(error.localizedDescription)
}
})
guard let userId = authResult?.user else { return }
Firestore.firestore().collection(USERS_REF).document(userId).setData([
USERNAME : firstName,
DATE_CREATED : FieldValue.serverTimestamp()
], completion: { (error) in

if let error = error {
debugPrint(error.localizedDescription)
} else {
self.dismiss(animated: true, completion: nil)
}
})
}
}



}

但是我得到了错误:

  • 无法使用类型为“([String : Any],完成:((Error?) -> Void)?)”的参数列表调用“setData”

有什么我没有做的吗?有人可以帮助进行此电子邮件/密码身份验证吗?

最佳答案

首先检查变量的命名方式是否正确。在 swift 中,我们使用驼峰命名法。

其次,您尝试将 textField 对象分配给字典中的密码。

第三,您已尝试在登录操作之外打印并执行 segue。考虑编辑您的代码:

  @IBAction func SMLoginTapped(_ sender: Any) {
print("Login button tapped!")
guard let emailText = EmailTextField.text, !emailText.isEmpty else { return }
guard let passwordText = PasswordTextField.text, !passwordText.isEmpty else { return }
let saveData: [String: Any] = ["email": emailText, "password": passwordText]
docRef.setData(saveData) { (error) in
if let error = error {
print("Oh no! Got an error: \(error.localizedDescription)")
} else {
print("Data has been saved to Firestore!")
self.performSegue(withIdentifier: "SecondViewSegue", sender: self)
}
}
}

关于ios - Cloud Firestore 身份验证 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53374479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com