ios - Swift Firebase check authentication more smoothly -
i'm setting window's root view controller login controller right off bat, , login controller checks user authentication firebase. if user logged in, controller changes root view controller feed controller. otherwise, login controller proceeds itself.
class logincontroller: uiviewcontroller { override func viewdidload() { super.viewdidload() if let uid = auth.auth().currentuser?.uid { database.database().reference().child("users").observesingleevent(of: .value, with: { snapshot in if snapshot.haschild(uid) { appdelegate.launchapplication() } else { self.setupui() } }) } else { self.setupui() } } ... }
where launchapplication
is
class func launchapplication() { guard let window = uiapplication.shared.keywindow else { return } window.rootviewcontroller = uinavigationcontroller(rootviewcontroller: feedcontroller()) }
in addition if let uid = auth.auth().currentuser?.uid
, i'm checking whether uid (if isn't nil
) exists in database, because have had situation deleted user still wasn't nil
.
the problem after launch screen finishes, there moment when login controller, though blank, visible. moment lasts few seconds. how can check authentication such login controller isn't visible @ all—so app decides how proceed after launch screen disappears? thanks.
1) use following code in case if want set rootcontroller app delegate . use check if currentuser.uid not nil , matched values in database perform following code in didfinishlaunchingwithoptions
of appdelegate . used me
func application(_ application: uiapplication, didfinishlaunchingwithoptions launchoptions: [uiapplicationlaunchoptionskey: any]?) -> bool { // override point customization after application launch. firebaseapp.configure() gidsignin.sharedinstance().clientid = firebaseapp.app()?.options.clientid if let uid = auth.auth().currentuser?.uid { database.database().reference().child("users").child(uid).observesingleevent(of: .value, with: { snapshot in if snapshot.exist() { let storyboard = uistoryboard.init(name: "main", bundle: nil) let vc = storyboard.instantiateviewcontroller(withidentifier: "feedcontroller") as! feedcontroller self.window?.rootviewcontroller=vc self.window?.makekeyandvisible() } else { //present logincontroller here } }) } return true }
2) method: when had initialised logincontroller instead of calling function appdelegate or code in launchapplication. make function in login class , write following code when required parameters matched
var transition: uiviewanimationoptions = .transitionflipfromleft let rootviewcontroller: uiwindow = ((uiapplication.shared.delegate?.window)!)! rootviewcontroller.rootviewcontroller = self.storyboard?.instantiateviewcontroller(withidentifier: "rootnav")//rootnav storyboard id of naviagtioncontroller attached destinationcontroller [feedcontroller] let mainwindow = (uiapplication.shared.delegate?.window!)! mainwindow.backgroundcolor = uicolor(hue: 0.6477, saturation: 0.6314, brightness: 0.6077, alpha: 0.8) uiview.transition(with: mainwindow, duration: 0.55001, options: transition, animations: { () -> void in }) { (finished) -> void in }
Comments
Post a Comment