user2632918
user2632918

Reputation:

Open second view

I try to check of a user was successfully logged in. If this be true, then not the login screen shut be loaded. The TerminView would be loaded. enter image description here

Here is the function in the LoginViewController (I wanna use this function also, when the user login successfully after press the "Login"-Button)

func checkIfLoggedIn() -> Void {

    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
    let url = NSURL(fileURLWithPath: path)
    let filePath = url.appendingPathComponent(K.login.file)?.path
    let fileManager = FileManager.default
    if fileManager.fileExists(atPath: filePath!) {
        self.navigationController!.pushViewController(self.storyboard!.instantiateViewController(withIdentifier: "TerminView") as UIViewController, animated: true)

    }
}

In the storyboard I give the TerminViewController also the id: TerminView

Here is the call of this function in the AppDelegate:

func applicationDidFinishLaunching(_ application: UIApplication) {

        LoginViewController().checkIfLoggedIn()
}

Where is my misstake?

Upvotes: 1

Views: 96

Answers (4)

Maddy
Maddy

Reputation: 1658

In Your app delegate you must set the root controller. As per your requirement see below code.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        //Main your storyboard identifier

        let mainStoryBoard = UIStoryboard(name: "Your_Storyboard_identifier", bundle: nil)
        var rootController:UIViewController

        //Retrieve Login Status if user is logged in or not

        let loginStatus: Bool = UserDefaults.standard.bool(forKey: "LoginStatus")

        // if login status is true show TerminViewController else LoginViewController

        if loginStatus == true {
            //Your TerminViewController

            rootController = mainStoryBoard.instantiateViewController(withIdentifier: "TerminViewController")
        }
        else{
            //Your LoginViewController

            rootController = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController")

        }

        //setting root view controller

        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.window?.rootViewController = rootController


        return true

    }

On Login Button click save the user login status in UserDefaults

@IBAction func login(_ sender: Any) {

//Store user login status in userdefaults and check it in app delegate when the user open your app again.            

UserDefaults.standard.set(true, forKey: "LoginStatus")
UserDefaults.standard.synchronize()

//Your Logic here ..
// On user login you might be navigating to TermViewController.
}

Upvotes: 0

Prathamesh
Prathamesh

Reputation: 170

Give the instance of loginViewController to appdelegate use this in appDelegate

let loginVC: LoginViewController?

this in viewDidload of LoginViewController

    let appDele = UIApplication.shared.delegate as! AppDelegate
    appDele.loginVC = self

and this in application did finish launching

func applicationDidFinishLaunching(_ application: UIApplication) {

    if(loginVc != nil){
    loginVC.checkIfLoggedIn()    }}

it is recommended that use userdefaults but if you want to continue with this solution then try this

Upvotes: 0

KKRocks
KKRocks

Reputation: 8322

Try this

You need to change rootViewController with login flag

func isUserLoggedIN() -> Bool {
        var struserID: String? = ""
        if  UserDefaults.standard.object(forKey:"LoginKey") != nil {
            struserID  = UserDefaults.standard.object(forKey:"LoginKey") as! String?
        }
        return struserID!.characters.count > 0 ? true : false
    }

Check as below

if self.isUserLoggedIN() {
     let navLog: UINavigationController? = Constant.StoryBoard.instantiateViewController(withIdentifier: "loginNavBar") as? UINavigationController
     window?.rootViewController = navLog
            }
else{
   // set another ViewController
}

Upvotes: 0

Shashank Dekate
Shashank Dekate

Reputation: 53

If u want to handled this in app delegate class, than based on login status. you need to set root view of the navigation controller. I think for better approach to handle this situation after the first sucessfull login u need to store result in NSUserdefault. Second time check this userdefault and accordingly set root view of your navigation controller.

Upvotes: 0

Related Questions