gpt4 book ai didi

iOS 应用崩溃在线 Database().reference() of Firebase

转载 作者:行者123 更新时间:2023-11-28 15:20:32 25 4
gpt4 key购买 nike

这个问题突然出现。在过去几天使用 Facebook 帐户进行身份验证时,它一直运行良好。今天我尝试实现 Auth.auth().createUser(email: ... password:... completion:...) 突然我的应用程序在函数 中进行身份验证后立即开始崩溃func completeSignIn(id:..., userData:...)DataService.ds.createFirebaseDBUser(uid: usr.uid, userData: ["provider": "Email"]),它在 Database().reference() 行的 Firebase Singleton 中崩溃。我很迷茫。我检查了一个它曾经工作的分支,但现在它也不起作用,所以我在 Firebase 中创建了一个新应用程序,但我遇到了同样的问题。我在几个地方看到过这个问题,但没有答案。它抛出错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil'

但我已经检查了我传递的 ID 和字典,并且值在那里。感谢您的帮助。

创建账户代码:

@IBAction func createAccount(_ sender: Any) {
if let email = emailField.text, let pwd = pwdField.text{
Auth.auth().createUser(withEmail: email, password: pwd, completion: { (user, error) in
if error != nil {
print("Unable to Authenticate E-mail with Firebase - Error: \(String(describing: error))")
}else{
print("Successfully Authenticated E-mail with Firebase")
if let usr = user {
let keychainResult = KeychainWrapper.standard.set(usr.uid, forKey: KEY_UID)
print("Data saved to Keychain: \(keychainResult)")
DataService.ds.createFirebaseDBUser(uid: usr.uid, userData: ["provider": "Email"])
}
}
})
}
}

FBLogin 和电子邮件验证:

@IBAction func facebookLogin(_ sender: Any) {
print("Start Login Process")
let fbLogin = FBSDKLoginManager()
print("Set Read Permissions")
//TODO: add Read Permissions
fbLogin.logIn(withReadPermissions: ["email","user_location"], from: self) { (result, error) in
if error != nil{
print("Unable to provide Authentication with Facebook - \(String(describing: error))")
}else if result?.isCancelled == true{
print("User Cancelled FB Authentication")
}else {
print("User Successfully Logged In")
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
self.firebaseAuth(credential)
}
}

}

@IBAction func emailLogin(_ sender: Any) {
if let email = emailField.text, let password = passwordField.text{
Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
if error != nil{
print("Unable to Authenticate Firebase - \(String(describing: error))")
} else {
print("Succesfully authenticated with Firebase")
if let user = user{
let userData = ["provider": "email"]
self.completeSignIn(id: user.uid, userData: userData)
}
}

})
}
}


func firebaseAuth(_ credential: AuthCredential){
Auth.auth().signIn(with: credential, completion: { (user, error) in
if error == nil {
print("Succesfully authenticated with Firebase")
if let user = user{
let userData = ["provider": credential.provider]
self.completeSignIn(id: user.uid, userData: userData)
}
} else{
print("Unable to Authenticate Firebase - \(String(describing: error))")
}
})
}


func completeSignIn(id: String, userData: Dictionary<String, String>){
DataService.ds.createFirebaseDBUser(uid: id, userData: userData)
let keychainResult = KeychainWrapper.standard.set(id, forKey: KEY_UID)
print("Data saved to Keychain: \(keychainResult)")
performSegue(withIdentifier: "goToMain", sender: nil)
}

Firebase 单例类数据服务:

import Foundation
import Firebase

let DB_BASE = Database().reference()

class DataService {

static let ds = DataService()

private(set) var REF_BASE = DB_BASE
private(set) var REF_PROMOS = DB_BASE.child("promos")
private(set) var REF_NEGOCIOS = DB_BASE.child("negocios")
private(set) var REF_USERS = DB_BASE.child("users")

func createFirebaseDBUser(uid: String, userData: Dictionary<String, String>){
REF_USERS.child(uid).updateChildValues(userData)
}

}

最佳答案

您只需要获取对现有数据库的引用,而不是尝试初始化一个新数据库。将 Database().reference() 更改为 Database.database().reference()

关于iOS 应用崩溃在线 Database().reference() of Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46081165/

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