Ios 升级到Firebase 5并存在身份验证问题。无法转换类型为';(用户?,错误?)
我刚刚升级了Firebase的播客,但却犯了很多错误。我使用了Firebase文档网页上的文档,因此我能够修复其中的大部分。这个错误我被卡住了,需要一些帮助 以下是我的错误:(我将它们标记为错误1和错误2。)Ios 升级到Firebase 5并存在身份验证问题。无法转换类型为';(用户?,错误?),ios,swift,firebase,Ios,Swift,Firebase,我刚刚升级了Firebase的播客,但却犯了很多错误。我使用了Firebase文档网页上的文档,因此我能够修复其中的大部分。这个错误我被卡住了,需要一些帮助 以下是我的错误:(我将它们标记为错误1和错误2。) 无法将类型为“(用户?,错误?)->()”的值转换为预期的参数类型“AuthDataResultCallback?”(也称为“可选()>”) “用户”类型的值没有成员“UpdateMail” 这是我的AuthService.Swift代码:(所有相同的Swift代码,只需拆分以显示错误)
这意味着您需要更改数据库5的语法 在error1中,您可以尝试以下操作:
static func signUp(username:String, email:String, password: String, imageData:Data, onSuccess: @escaping () -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
Auth.auth().createUser(withEmail: email, password: password) { (user , error) in
if error != nil {
onError(error!.localizedDescription)
return
}
//Here you have to change the uid
let uid = user?.user.uid
//It should be Storage.storage().reference()
let storageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOT_REF).child("profile_image").child(uid!)
//storageRef.put was removed and it's changed be storage.putData
storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in
if error != nil {
return
}
//downloadURL is changed too, you can read at documents of Firebase to know how to use this
storageRef.downloadURL(completion: { (url, error) in
if let error = error {
return
}else {
let profileImageUrl = url!.absoluteString
self.setUserInformation(profileImageUrl: profileImageUrl, username: username, email: email, uid: uid!, onSuccess: onSuccess)
}
})
})
}
}
Api.User.CURRENT_USER?.updateEmail(to: email, completion: { (error) in
if error != nil {
onError(error?.localizedDescription)
}else {
let uid = Api.User.CURRENT_USER?.uid
//Change here
let storageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOT_REF).child("profile_image").child(uid!)
//Change here
storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in
if error != nil {
return
}
storageRef.downloadURL(completion: { (url, error) in
if let error = error {
return
}else {
let profileImageUrl = url!.absoluteString
self.updateDatabase(profileImageUrl: profileImageUrl, username: username, email: email, onSuccess: onSuccess, onError: onError)
}
})
})
}
})
static func updateDatabase(profileImageUrl: String, username: String, email: String, onSuccess: @escaping () -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
let dict = ["username": username, "username_lowercase": username.lowercased(), "email": email, "profileImageUrl": profileImageUrl]
Api.User.REF_CURRENT_USER?.updateChildValues(dict, withCompletionBlock: { (error, ref) in
if error != nil {
onError(error?.localizedDescription)
}else {
onSuccess()
}
})
}
static func logOut(onSuccess: @escaping () -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
do {
try Auth.auth().signOut()
onSuccess()
}catch let logoutError {
onError(logoutError.localizedDescription)
}
}
在error2中,它与error1的错误相同,您可以尝试以下操作:
static func signUp(username:String, email:String, password: String, imageData:Data, onSuccess: @escaping () -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
Auth.auth().createUser(withEmail: email, password: password) { (user , error) in
if error != nil {
onError(error!.localizedDescription)
return
}
//Here you have to change the uid
let uid = user?.user.uid
//It should be Storage.storage().reference()
let storageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOT_REF).child("profile_image").child(uid!)
//storageRef.put was removed and it's changed be storage.putData
storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in
if error != nil {
return
}
//downloadURL is changed too, you can read at documents of Firebase to know how to use this
storageRef.downloadURL(completion: { (url, error) in
if let error = error {
return
}else {
let profileImageUrl = url!.absoluteString
self.setUserInformation(profileImageUrl: profileImageUrl, username: username, email: email, uid: uid!, onSuccess: onSuccess)
}
})
})
}
}
Api.User.CURRENT_USER?.updateEmail(to: email, completion: { (error) in
if error != nil {
onError(error?.localizedDescription)
}else {
let uid = Api.User.CURRENT_USER?.uid
//Change here
let storageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOT_REF).child("profile_image").child(uid!)
//Change here
storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in
if error != nil {
return
}
storageRef.downloadURL(completion: { (url, error) in
if let error = error {
return
}else {
let profileImageUrl = url!.absoluteString
self.updateDatabase(profileImageUrl: profileImageUrl, username: username, email: email, onSuccess: onSuccess, onError: onError)
}
})
})
}
})
static func updateDatabase(profileImageUrl: String, username: String, email: String, onSuccess: @escaping () -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
let dict = ["username": username, "username_lowercase": username.lowercased(), "email": email, "profileImageUrl": profileImageUrl]
Api.User.REF_CURRENT_USER?.updateChildValues(dict, withCompletionBlock: { (error, ref) in
if error != nil {
onError(error?.localizedDescription)
}else {
onSuccess()
}
})
}
static func logOut(onSuccess: @escaping () -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
do {
try Auth.auth().signOut()
onSuccess()
}catch let logoutError {
onError(logoutError.localizedDescription)
}
}
您可以再次阅读Firebase的文档,看看有什么不同
Api.User.CURRENT_USER?.updateEmail(to: email, completion: { (error) in
if error != nil {
onError(error?.localizedDescription)
}else {
let uid = Api.User.CURRENT_USER?.uid
//Change here
let storageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOT_REF).child("profile_image").child(uid!)
//Change here
storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in
if error != nil {
return
}
storageRef.downloadURL(completion: { (url, error) in
if let error = error {
return
}else {
let profileImageUrl = url!.absoluteString
self.updateDatabase(profileImageUrl: profileImageUrl, username: username, email: email, onSuccess: onSuccess, onError: onError)
}
})
})
}
})
static func updateDatabase(profileImageUrl: String, username: String, email: String, onSuccess: @escaping () -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
let dict = ["username": username, "username_lowercase": username.lowercased(), "email": email, "profileImageUrl": profileImageUrl]
Api.User.REF_CURRENT_USER?.updateChildValues(dict, withCompletionBlock: { (error, ref) in
if error != nil {
onError(error?.localizedDescription)
}else {
onSuccess()
}
})
}
static func logOut(onSuccess: @escaping () -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
do {
try Auth.auth().signOut()
onSuccess()
}catch let logoutError {
onError(logoutError.localizedDescription)
}
}