swift - FirebaseAuth with Swift 不起作用(显示一个空白的 Welcome VC)
问题描述
我有一个奇怪的问题。当我尝试使用 FirebaseAuth / FirebaseUI 时,我无法显示任何身份验证选项并且只能获得一个空白的 ViewController
有谁知道我做错了什么?顺便提一句。电子邮件身份验证已启用。:)
Login ViewController
import UIKit
import FirebaseAuth
import FirebaseUI
typealias FIRUser = FirebaseAuth.User
class LoginViewController: UIViewController, UITextFieldDelegate {
// MARK: Properties
@IBOutlet weak var logInandSignUp: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
}
// MARK: Actions
@IBAction func loginButtonTapped(_ sender: UIButton) {
print("Login Button tapped")
// 1 access the FUIAuth default auth UI singleton
guard let authUI = FUIAuth.defaultAuthUI()
else { return }
// 2 set FUIAuth's singleton delegate
authUI.delegate = self
// 3 present the auth view controller
let authViewController = authUI.authViewController()
present(authViewController, animated: true, completion: nil)
}
}
extension LoginViewController: FUIAuthDelegate {
func authUI(_ authUI: FUIAuth, didSignInWith authDataResult: AuthDataResult?, error: Error?) {
let user = User.current
if let error = error {
assertionFailure("Error signing in: \(error.localizedDescription)")
return
}
UserService.show(forUID: user.uid) { (user) in
if let user = user {
// handle existing user
User.setCurrent(user)
let initialViewController = UIStoryboard.initialViewController(for: .main)
self.view.window?.rootViewController = initialViewController
self.view.window?.makeKeyAndVisible()
} else {
// handle new user
self.performSegue(withIdentifier: Constants.Segue.toCreateUsername, sender: self)
}
}
}
}
用户服务.swift:
import Foundation
import FirebaseAuth.FIRUser
import FirebaseDatabase
import FirebaseUI
import FirebaseAuth
struct UserService {
static func create(_ firUser: FIRUser, username: String, completion: @escaping (User?) -> Void) {
let userAttrs = ["username": username]
let ref = Database.database().reference().child("Leader").child(firUser.uid)
ref.setValue(userAttrs) { (error, ref) in
if let error = error {
assertionFailure(error.localizedDescription)
return completion(nil)
}
ref.observeSingleEvent(of: .value, with: { (snapshot) in
let user = User(snapshot: snapshot)
completion(user)
})
}
}
static func show(forUID uid: String, completion: @escaping (User?) -> Void) {
let ref = Database.database().reference().child("users").child(uid)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let user = User(snapshot: snapshot) else {
return completion(nil)
}
completion(user)
})
}
}
用户.swift
import Foundation
import FirebaseDatabase.FIRDataSnapshot
class User {
// MARK: - Properties
let uid: String
let username: String
// MARK: - Init
init(uid: String, username: String) {
self.uid = uid
self.username = username
}
init?(snapshot: DataSnapshot) {
guard let dict = snapshot.value as? [String : Any],
let username = dict["username"] as? String
else { return nil }
self.uid = snapshot.key
self.username = username
}
// MARK: - Singleton
// Now that we've created a User singleton, we need to make sure to set it. Once we receive the user from the database, we set the singleton with our custom setter method. After the singleton is set, it will remain in memory for the rest of the app's lifecycle. It will be accessible from any view controller with the following code: let user = User.current
// 1 Create a private static variable to hold our current user.
private static var _current: User?
// 2 Create a computed variable that only has a getter that can access the private _current variable.
static var current: User {
// 3 Check that _current that is of type User? isn't nil. If _current is nil --> fatalError().
guard let currentUser = _current else {
fatalError("Error: current user doesn't exist")
}
// 4 If _current isn't nil, it will be returned to the user.
return currentUser
}
// MARK: - Class Methods
// 5 Create a custom setter method to set the current user.
static func setCurrent(_ user: User) {
_current = user
}
}
希望任何人都可以帮助我。真的不明白是什么问题。
解决方案
推荐阅读
- javascript - 为什么我收到未捕获的错误:语法错误,JavaScript 中无法识别的表达式?
- r - 如何将 fitdistrplus::fitdist 摘要转换为整洁的格式?
- c++ - extern const 在这种特殊情况下
- c# - 在运行时加载属性后的 OnPropertyChanged 事件
- nuget - 从 Artifactory Nuget 存储库获取最新版本
- reactjs - React-Native,由于批量更新,状态没有按预期改变,但我该怎么办?
- javascript - React:在 HTML 中使用 React 时如何导入另一个要使用的类
- mongodb - 在 docker 中运行 Mongodb Ops 管理器
- python - Python 将科学记数法写入文件;如何以十进制写入文件?
- jquery - 动画中的居中元素