【发布时间】:2018-03-26 06:31:16
【问题描述】:
在应用代理中,我编写代码来决定如果用户已经登录,用户是否会直接进入主标签栏,否则在应用启动时用户将被发送到欢迎屏幕 VC。
这是我的 App Delegate 中的代码。
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
checkUserLogin()
// to track the user default info.plist location in the device (simulator) for checking purpose.
print("The Location of info.plist of User Default: \(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String)")
return true
}
}
extension AppDelegate {
// MARK: - HELPER METHODS
func checkUserLogin() {
// to check whether the user has already logged in or not
SVProgressHUD.show()
Auth.auth().addStateDidChangeListener { (auth, user) in
if user == nil {
SVProgressHUD.dismiss()
self.goToWelcomeVC()
} else {
SVProgressHUD.dismiss()
self.goToMainTabBar()
}
// if user is not nil then automatically go to maintab bar (initialVC is indeed main tab bar controller)
SVProgressHUD.dismiss()
}
}
func goToMainTabBar () {
print("XXXXXXXX")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainTabBar = storyboard.instantiateViewController(withIdentifier: "mainTabBar")
window?.rootViewController = mainTabBar
}
func goToWelcomeVC () {
let storyboard = UIStoryboard(name: "Welcome", bundle: nil)
let login = storyboard.instantiateViewController(withIdentifier: "WelcomeVC")
window?.rootViewController = login
}
}
我正在尝试创建 registerVC,在用户填写文本字段并按下注册按钮后我尝试使用 Firebase 身份验证创建用户,当用户在 registerVC 中按下注册按钮时将触发代码
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
// ...
}
当Auth.auth().createUser() 被触发时,它会在我的应用程序委托中自动触发Auth.auth().addStateDidChangeListener() 方法,因此它使我的应用程序自动转到主标签栏,此时我希望我的应用程序仍在RegistrationVC 中,因为用户有首先验证他们的电子邮件,然后登录到主标签栏。
当Auth.auth().createUser(withEmail: email, password: password)被触发时,如何防止用户进入MainTab Bar?但我也想保留如果用户已经登录,用户将直接进入主选项卡栏的功能,否则用户将在应用程序启动时被发送到欢迎屏幕 VC。 ?
【问题讨论】:
-
当您创建一个用户时,该用户会自动登录,所以我认为如果您只想在验证电子邮件后转到下一个屏幕,您将不得不稍微更改您的逻辑。您可以查看this answer
标签: ios swift firebase firebase-authentication