How to I go to the "login" ViewController if FirebaseAuth is null? - swift

I am really new to Swift and have a really simple setup. There is a login screen and if the user logs in with correct credentials they go to the first VC in a tab bar.
The transition from login to the tab bar view controllers work fine but when I load the app I am not taken to the login screen if currentUser is nil.
Here is my check in the first VC in the tab bar:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if Auth.auth().currentUser == nil {
goToLogin()
}
}
And this is the function in the same VC to go to login
func goToLogin() {
let loginViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.loginViewController) as? LoginViewController
view.window?.rootViewController = loginViewController
view.window?.makeKeyAndVisible()
}
The storyboard ID for my login VC is "loginViewController" and the Tab bar controller is my initial view controller
Update
My auth check in App Delegate looks like this:
if Auth.auth().currentUser == nil {
// considering the StoryboardID is same as View Controllers name.
self.makeRootVC(storyBoardName: "Main", vcName: Constants.Storyboard.loginViewController)
} else {
self.makeRootVC(storyBoardName: "Main", vcName: Constants.Storyboard.homeTabBarController)
}
But I am getting this error from the "homeTabBarController":
Type 'Constants.Storyboard' has no member 'homeTabBarController'
homeTabBarController is the StoryboardId for my tab bar controller in the Main storyboard. So why is it not being recognized?

You should do this check in didFinishLaunchingWithOptions in AppDelegate not in ViewController. You can use the below function to change your root view controller.
In AppDelegate.swift
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if Auth.auth().currentUser == nil {
// considering the StoryboardID is same as View Controllers name.
self.makeRootVC(storyBoardName: "Main", vcName: Constants.Storyboard.loginViewController)
} else {
self.makeRootVC(storyBoardName: "Main", vcName: Constants.Storyboard.yourTabBarController)
}
return true
}
func makeRootVC(storyBoardName : String, vcName : String) {
let vc = UIStoryboard(name: storyBoardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcName)
let nav = UINavigationController(rootViewController: vc)
nav.navigationBar.isHidden = true
self.window?.rootViewController = nav
let options: UIView.AnimationOptions = .transitionCrossDissolve
let duration: TimeInterval = 0.6
UIView.transition(with: self.window!, duration: duration, options: options, animations: {}, completion: nil)
}

Related

Completely black screen on setting initial view controller in AppDelegate

All I want to do is go to the login screen if the user = nil. Otherwise it goes to the main/home section of the app which consists of the Tab Bar Controller's first view controller
Here is my app delegate:
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if Auth.auth().currentUser == nil && !UserDefaults.standard.bool(forKey: "hasViewedWalkthrough") {
let initialViewController = storyboard.instantiateViewController(withIdentifier: "WalkthroughViewController") as? WalkthroughViewController
self.window?.rootViewController = initialViewController
} else if Auth.auth().currentUser == nil && UserDefaults.standard.bool(forKey: "hasViewedWalkthrough") {
let initialViewController = storyboard.instantiateViewController(withIdentifier: "loginViewController") as? LoginViewController
self.window?.rootViewController = initialViewController
} else {
let initialViewController = storyboard.instantiateViewController(withIdentifier: "homeTabBarController") as? MainTabBarViewController
self.window?.rootViewController = initialViewController
}
self.window?.makeKeyAndVisible()
return true
}
I am not able to get to the onboarding or login screen. I am always led to the main screen. I know the screens are rendering fine if I manually set them as initial view controllers in storyboard.
I have tried numerous solutions and none of them are working.
Update
Try this:
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyBoard.instantiateViewControllerWithIdentifier("homeTabBarController") as? TabBarViewController // whatever your swift file is called
self.window?.rootViewController = storyBoard
use that code for each statement for the if statement, obviously change the names of the view controllers to the login one respectively.
For people reading this in the future, iOS 13 introduced SceneDelegate and now all UI related stuff should be configured from that point.
I faced the same issue and solved it by using this code.
The custom window should be configured from here now.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let sceneWindow = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: sceneWindow.coordinateSpace.bounds)
window?.windowScene = sceneWindow
window?.makeKeyAndVisible()
if let navigationController = Storyboard.Main.initialViewController as? UINavigationController,
let viewController = navigationController.children.first as? CountriesViewController {
viewController.inject(CountriesViewModel(provider: CountryAPI(),
informTo: viewController),
navigator: CountriesNavigator(navigationController: navigationController))
window?.rootViewController = navigationController
}
}

Whose view is not in the window hierarchy only in First Launch

I have an Onboarding screen that I'm showing to the new users when they first open the app.
In my appDelegate I check whether is the first launch or not.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var initialViewController = storyboard.instantiateViewController(withIdentifier: "OnBoarding")
let userDefaults = UserDefaults.standard
if userDefaults.bool(forKey: "onBoardingComplete") {
initialViewController = storyboard.instantiateViewController(withIdentifier: "MainApp")
}
window?.rootViewController = initialViewController
window?.makeKeyAndVisible()
}
Also I have a collectionViewCell that I have some buttons and when I click them I get an Alert with informations.
Example of one button
#IBAction func guide3Btn(_ sender: Any) {
let infoVC = infoService.info(title: "Title", body: "Information")
self.window?.rootViewController?.present(infoVC, animated: true, completion: nil)
}
When the user first launches the app if he clicks the info button gets this:
Warning: Attempt to present <MyApp.InfoViewController: 0x7f91db45cfb0> on <MyApp.OnbBoardViewController: 0x7f91db506af0> whose view is not in the window hierarchy!
If the user reopens the app everything is ok. I know that when we have first launch we have onBoarding as root controller but I can't understand how to fix this.
Update
This is the infoService class. I use a new storyboard to create the alert.
class InfoService {
func info(title: String, body: String) -> InfoViewController {
let storyboard = UIStoryboard(name: "InfoStoryboard", bundle: .main)
let infoVC = storyboard.instantiateViewController(withIdentifier: "InfoVC") as! InfoViewController
infoVC.infoBody = body
infoVC.infoTitle = title
return infoVC
}
}
You can try add your storyboard instantiate code blocks to main thread using DispatchQueue.main.async like below:
I solved almost all of my whose view is not in the window hierarchy! problems.
DispatchQueue.main.async {
let infoVC = storyboard.instantiateViewController(withIdentifier: "InfoVC") as! InfoViewController
infoVC.infoBody = body
infoVC.infoTitle = title
}
return infoVC
Referenced from : https://stackoverflow.com/a/45126338/4442254

Google sign out and return to login page

I want to click the Sign Out button on the ListViewController to sign out the user, and return to the SignInViewController, like below picture.
In ListViewController for the SignOut button:
#IBAction func didTapSignOut(_ sender: Any) {
//Sign Out
GIDSignIn.sharedInstance().signOut()
//Go to the `SignInViewController`
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let desVC = mainStoryboard.instantiateViewController(withIdentifier: "SignInViewController") as! SignInViewController
self.navigationController?.pushViewController(desVC, animated: true)
}
When back to the SignInViewController, there's a back button, which is directed back to the ListViewController on it. It seems like the app still has the cache of the user's data so the user doesn't actually sign out.
But what I want is to go back to the app's initial state that the user has to sign in again.
My Storyboard:
How I move from SignInViewController to ListViewController: in AppDelegate
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
withError error: Error!) {
if let error = error {
print("\(error.localizedDescription)")
} else {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabbarVC = storyboard.instantiateViewController(withIdentifier: "TabbarIdentifier") as! UITabBarController
self.window?.rootViewController?.present(tabbarVC, animated: false, completion: nil)
}
}
Solution I've tried:
var window: UIWindow?
#IBAction func didTapSignOut(_ sender: Any) {
GIDSignIn.sharedInstance().signOut()
let desVC: UIViewController = SignInViewController()
if let window = self.window{
window.rootViewController = desVC
}
self.navigationController?.popToRootViewController(animated: true)
}
but now the view didn't change after I clicked the button.
In google login you are presenting the tabbar controller from signinViewcontroller. So just dismiss the tabbar controller when logout button is tapped
#IBAction func didTapSignOut(_ sender: Any) {
//Sign Out
GIDSignIn.sharedInstance().signOut()
//Go to the `SignInViewController`
self.tabBarController?.dismiss(animated: true, completion: nil)
}
change self.navigationController?.pushViewController to self.navigationController?.popToRootViewController (this will have a pop animation)
if you need a push animation, use self.navigationController?.setViewControllers([desVC])
UPDATED 1:
If you don't mind having no push/pop animation, you can directly change the window
let nvc = UINavigationController(rootViewController: desVC)
let window = UIApplication.shared.window
window.rootViewController = nvc
UPDATED 2:
Yea, updating via navigationController would not be proper in your hierarchy. Just change the rootViewController of the window (as stated in Updated 1)

Select storyboard to launch on launchScreen completion

After completion of launchScreeen(Splash) I want to be able to select which storyboard to launch. For example launch login.storyboard if user is not logged in or launch dashboard.storyboard if user is logged in. Currently Main.storyboard is launched after launchScreen. in appdelegate I have a code for checking the login status as follows:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
var launchDashBoard = false
let isUserLoggedIn = UserDefaults.standard.object(forKey: TAG_IS_USER_LOGGEDIN) as? Bool
if isUserLoggedIn != nil {
launchDashBoard = isUserLoggedIn!
}
if launchDashBoard {
self.loadDashBoard()
}else{
self.loadIntro()
}
return true
}
func loadHome(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let home = storyboard.instantiateViewController(withIdentifier: "dashboard") as! Dashboard
let navigationController = UINavigationController(rootViewController: home)
self.window?.rootViewController = navigationController
}
func loadLogin(_ viewController: UIViewController?){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let home = storyboard.instantiateViewController(withIdentifier: "signIn") as! SignInVC
home.previousViewController = viewController
let navigationController = UINavigationController(rootViewController: home)
self.window?.rootViewController = navigationController
}
If I run the app with this code it crashes with following log:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'
*** First throw call stack:
Is there a way to include a function that determines which storyboard to launch on launchScreen loading is finished or is there something wrong with my code?
You can create a new Navigation.storyboard. That storyboard can have an initial view controller(Call it StartUpNavigationViewController), that will be opened when Launch screen has been shown.
In that StartUpNavigationViewController, check for the logic if the user is logged in or not, based on that you can navigate to login or dashboard storyboard.

Blank screen first time launching app xcode

I am having a little issue with logging into an app the first time with a blank screen and I am getting the warning "Attempt to present on whose view is not in the window hierarchy!" After I close and relaunch, the views appear fine. I believe it has something to do with the rootViewController but not sure... Thanks in advance for any help or direction!
App delegate
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var ref:FIRDatabaseReference?
var databaseHandle:FIRDatabaseHandle?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = MainNavigationController()
FIRApp.configure()
ref = FIRDatabase.database().reference()
return true
}
Navigation controller as rootViewController
class MainNavigationController: UINavigationController {
var segmentedController: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
let vc1 = TravelersFeedVC()
let vc2 = ProfileVC()
if isLoggedIn() {
// assume user is logged in
let homeController = HomeController()
viewControllers = [homeController]
homeController.firstViewController = vc1
homeController.secondViewController = vc2
} else {
perform(#selector(showLoginController), with: nil, afterDelay: 0.01)
}
}
fileprivate func isLoggedIn() -> Bool {
return UserDefaults.standard.isLoggedIn()
}
func showLoginController() {
let loginController = LoginController()
present(loginController, animated: true, completion: {
// perhaps do something here later
})
}
}
// log in function called
func finishLoggingIn() {
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
guard let mainNavigationController = rootViewController as? MainNavigationController else { return }
let vc1 = TravelersFeedVC()
let vc2 = ProfileVC()
if isLoggedIn() {
// assume user is logged in
let homeController = HomeController()
mainNavigationController.viewControllers = [HomeController()]
homeController.firstViewController = vc1
homeController.secondViewController = vc2
} else {
perform(#selector(showLoginController), with: nil, afterDelay: 0.01)
}
UserDefaults.standard.setIsLoggedIn(value: true)
dismiss(animated: true, completion: nil)
}
Ok, so my last answer was wrong (i deleted it), the things is that in app there is a keywindow, which is your navcontroller, and you cannot present anything on this one untill it will load its subviews (even if it hasn't any), and that would happend on viewdidappear, so you should put your code from viewdidload there.