登录后将用户重定向到特定页面

时间:2019-05-27 21:49:35

标签: ios swift firebase firebase-realtime-database

主要问题是检查用户在Firebase数据库中是否有孩子,以便我知道他是否正在注册或登录。

第1部分:第1部分(子数据库(有效),并让用户成为默认用户(我不确定如何检查它是否起作用)

第2部分:在不同的.Swift文件中(检查是否存在用户默认值(又名“教育子级”)。我几乎什么都没有,除了我知道它必须进入viewDidAppear

第1部分

'chromeOptions': {
  args: [ '--headless', '--disable-gpu', '--no-sandbox','--disable-setuid-sandbox','--disable-dev-shm-usage']
}

第2部分     func viewDidAppear(_ animation:String){
    ?????? }

第1部分没有错误,尽管不确定是否创建了用户默认值。对于第2部分,我尝试了很多东西,但是没有用。

以下是第一个答案后的更新代码:

@IBAction func SubmitPressed(_ sender: Any) {             
let databaseRef = Database.database().reference()             
let uid = Auth.auth().currentUser!.uid
databaseRef.child("Education").child(uid).setValue(self.Education.text!)             
UserDefaults.standard.set("Education", forKey: "Education") 

有关教育/个人信息的部分进入页面

import Foundation
import UIKit
import SwiftKeychainWrapper
import Firebase
import CoreFoundation
import AVFoundation
import FirebaseDatabase


var educationCache : String {
    get {
        return (UserDefaults.standard.string(forKey: "Education")!)
    } set {
        UserDefaults.standard.set(newValue, forKey: "Education")
    }
}

主页

@IBAction func SubmitPressed(_ sender: Any) {

    let databaseRef = Database.database().reference()
    let uid = Auth.auth().currentUser!.uid
    databaseRef.child("Education").child(uid).setValue(self.Education.text!)
    // The following line will save it in userDefault itself. And you dont have to call the whole UserDefault Everywhere
    educationCache = "Education"

    self.performSegue(withIdentifier: "tohome", sender: nil)
         }

1 个答案:

答案 0 :(得分:0)

您不想检查条件是要执行特定操作的viewController。每次都检查userDefaults只是在层次结构中添加了额外的视图。 Firebase提供了.addStateDidChangeListener函数来对其进行检查。将以下代码添加到您的委托中。如果有,它将自动在用户之间切换。

AppDelegate.swift

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        FirebaseApp.configure()
        observeUser()
        return true
    }

    func observeUser(){
        Auth.auth().addStateDidChangeListener { (auth, user) in
            if (user != nil) {
                // If the user is not nil. Perform this block
                self.showInitController(with identifier: "<add_your_storyboard_reusableID_for_homepage>", animated: false)
                // The storyboard Id belongs to the HOMEPAGE/FEED, the view where user goes on signIn
            } else {
                //If there is no user. This block will perform
                self.showInitController(with identifier: "<add_your_storyboard_reusableID_for_loginpage>", animated: false)
                // The storyboard Id belongs to the SIGNUP PAGE, the view where user goes on signIn
            }
        }
    }

   func showInitController(with identifier: String, animated: Bool = false) {
        DispatchQueue.main.async {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: identifier)
            var topRootViewController: UIViewController = (UIApplication.shared.keyWindow?.rootViewController)!
            while((topRootViewController.presentedViewController) != nil){
                topRootViewController = topRootViewController.presentedViewController!
            }
            topRootViewController.present(vc, animated: animated, completion: nil)
        }
    }

在您的快速文件中。请执行以下操作,永远不要回头。如果您应用了其他功能,则可以自动重定向到视图。

@IBAction func SubmitPressed(_ sender: Any) {
        let databaseRef = Database.database().reference()
        let uid = Auth.auth().currentUser!.uid
        databaseRef.child("Education").child(uid).setValue(self.Education.text!)
        self.performSegue(withIdentifier: "tohome", sender: nil)
    }

在第二个视图中,不要在任何地方进行任何操作。如果不再需要该视图中的任何内容,请将其保留。