0

I have a TabBarController inside I am loading two ViewControllers.

import UIKit

class TabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

Images:

enter image description here

class AttendanceViewController: UIViewController,UIApplicationDelegate,
    UICollectionViewDelegateFlowLayout,UINavigationControllerDelegate,
    CLLocationManagerDelegate,UIImagePickerControllerDelegate {

    override func viewWillAppear(_ animated: Bool) {
    if  Auth.auth().currentUser?.uid == nil {
        //Below code is not working
        let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main)
        .instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
        self.navigationController?.pushViewController(vc!, animated: true)
        print("user is not login in redirect to login page")
        return
    }else{
        print("User Successfully loginedin")
    }
}

}

Note : I have two view controller like AttendanceViewController and TaskViewController I want to check if user is not login then I want to redirect to my LoginViewController I tried above code it's was not working

Wings
  • 2,398
  • 23
  • 46
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
  • Are these both VC inside your tabBar? – Wings Nov 27 '18 at 09:21
  • @wings yes bro...`AttendanceViewController` I am cheking if your is not login redirect to `loginViewController` but its not working..it's coming inside `if` loop but `LoginViewController` is not displaying.. – Gowthaman M Nov 27 '18 at 09:23
  • make sure your `AttendanceViewController ` is wrapped into a `UINavigationController`. – Torongo Nov 27 '18 at 09:26
  • @Torongo After adding `UINavigationController` now it working bro..but it's overlapping means loginviewController showing ontop of the TabBarviewController – Gowthaman M Nov 27 '18 at 09:33
  • LoginViewController is inside the tabBar? I suggest that if the user is not logged in instead of pushing the LoginViewController set it as root view, doing that the view flow will start from the loginViewContoller. – FedeH Nov 27 '18 at 09:33
  • @FedeHenze no sir....Inside TabBar I have **Attendance** and **Task** Viewcontroller....`LoginViewController` is separate – Gowthaman M Nov 27 '18 at 09:35
  • @GowthamanM is LoginViewController in another Storyboard? – Robert Dresler Nov 27 '18 at 09:37
  • Save the login state – Wings Nov 27 '18 at 09:38
  • @GowthamanM if LoginVC is not in the TabBarController setting it as a root view controller should work. – FedeH Nov 27 '18 at 09:40
  • @RobertDresler I am new to iOS I am not getting what you are asking..still I created one ViewController from storyboard then I linked my LoginViewControlller. – Gowthaman M Nov 27 '18 at 09:42
  • @GowthamanM see my edited answer – Robert Dresler Nov 27 '18 at 09:43
  • @RobertDresler ya sure give me sometime will check and let u know..Thanks bro – Gowthaman M Nov 27 '18 at 09:45
  • move your login checking code to `TabBarViewController`, and assign `LoginViewController ` to rootWindow (instead of pushing it) – SPatel Nov 27 '18 at 10:03

4 Answers4

1

Present UIViewController

Code you wrote would work if your ViewControllers were embed in UINavigationController, which they're not. So instead of pushing UIViewController from UINavigationController which doesn't exist you can just present your LoginViewController. So replace this:

let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main)
.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
self.navigationController?.pushViewController(vc!, animated: true)

with

let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main)
.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
present(vc!, animated: true, completion: nil)  

Embed in UINavigationController

Alternatively you can embed your UIViewController in UINavigationController and set segue with identifier and then perform this segue to LoginViewController.

enter image description here

enter image description here

enter image description here

7. Perform segue

performSegue(withIdentifier: "YourIdentifier", sender: self)
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
1

Hey I just created an example it will help you:-

I just use userdefaults to save loginState

Here is:-

1). TaskViewController

I just created a signOut button in it

import UIKit

class TaskViewController: UIViewController {


@IBOutlet weak var signOutButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func viewDidLayoutSubviews() {
    signOutButton.layer.cornerRadius = signOutButton.layer.frame.height/2
}



@IBAction func signOutButtonTapped(_ sender: Any) {

    UserDefaults.standard.set(false, forKey: "isUserLoggedIn")  // here I remove the boolean value
    UserDefaults.standard.synchronize()
    let storyBoard = UIStoryboard(name: "Login", bundle: nil)
    let loginVc = storyBoard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
    let rootVc = UINavigationController(rootViewController: loginVc)
    UIApplication.shared.keyWindow?.rootViewController = rootVc

}



}

2). AttendanceViewController - I didn't created anything in it

import UIKit

class AttendanceViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}


}

These both controllers are inside TabBarController which is HomeController

import UIKit

class HomeController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()


}

}

I created it in Main.storyboard file:- enter image description here

And LoginViewController created in login.storyboard file enter image description here

LoginViewController:- here saved the loginState

import UIKit

class LoginViewController: UIViewController {

@IBOutlet weak var loginButton: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func viewDidLayoutSubviews() {
    loginButton.layer.cornerRadius = loginButton.layer.frame.height/2
}


@IBAction func loginButtonTapped(_ sender: Any) {

    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = mainStoryboard.instantiateViewController(withIdentifier: "HomeController") as! HomeController
    UserDefaults.standard.set(true, forKey: "isUserLoggedIn")
    UserDefaults.standard.synchronize()
    UIApplication.shared.keyWindow?.rootViewController = viewController

}

}

And last in Appdelegate.swift check the loginState in didFinishLaunchingWithOptions like this:-

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

    let userLoginStatus = UserDefaults.standard.bool(forKey: "isUserLoggedIn")
    print(userLoginStatus)

    if(userLoginStatus)
    {
        let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
        let centerVC = mainStoryBoard.instantiateViewController(withIdentifier: "HomeController") as! HomeController
        window!.rootViewController = centerVC
        window!.makeKeyAndVisible()
    } else {

        let mainStoryBoard = UIStoryboard(name: "Login", bundle: nil)
        let centerVC = mainStoryBoard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
        window!.rootViewController = centerVC
        window!.makeKeyAndVisible()
    }

    return true
}

Here is the full project - https://www.dropbox.com/s/3d34raovyx6orda/saveLogin.zip?dl=0

Wings
  • 2,398
  • 23
  • 46
0

I think you want to check it in appDelegate. Nowadays I am also working with UITabbarcontroller.I suggest to set a tag(using userDefaults) for checking that the user is logging in or not . And my code is

class AppDelegate: UIResponder, UIApplicationDelegate , UITabBarControllerDelegate , GIDSignInDelegate{

    var window: UIWindow?

    var userCurrentLocation : CLLocation?
    var mapView : GMSMapView?

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

        if UserDefaults.standard.value(forKey: "LoginStatus")as? String == "1"
        {
            let storyBoard = UIStoryboard(name: "Main", bundle: nil)
            let initailVC = storyBoard.instantiateViewController(withIdentifier: "tabBar")
            let navVC = UINavigationController(rootViewController: initailVC)
            self.window?.rootViewController = navVC
        }

        return true
    }

I am using this code to check if the user is logging in it will redirect into TabBarController and if not it will remain in LoginVC.

Angel F Syrus
  • 1,984
  • 8
  • 23
  • 43
0
  • Embed your UITabBarController in a UINavigationController

    UINavigationController(rootViewController: UITabBarController(nibName: "TabBarNibName", bundle: Bundle.main))

  • Just push from the navigation controller of your TabBarController

    tabbarcontroller.navigationController?.pushViewController(vc!, animated: true)

Community
  • 1
  • 1
Torongo
  • 1,021
  • 1
  • 7
  • 14
  • `UITabBarController -> Editor -> Embed In -> Navigation Controller)` options are hidden bro...other ViewController it's showing....only UITabBarController I am not able to set the NavigationController – Gowthaman M Nov 27 '18 at 09:51