Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Login / Sign Out Implementation in Swift

Tags:

ios

swift

iphone

I've been attempting to implement a Login / Logout Flow for an iOS app in swift. Here's my storyboard -

Main Storyboard

In the main view controller (which is the blue screen), I have the following code implemented to detect that if the user is already signed in, then to automatically take them to the table view controller -

override func viewDidAppear(animated: Bool) {

    if PFUser.currentUser() != nil {

       self.performSegueWithIdentifier("test", sender: self)

    }

The issue is, that when i Sign In or Login through either one of the green screens, the Table View navigation bar appears different. The 'Sign Out' button appears properly when the user opens the app and is already logged in, however, logging in or signing in through the green screens, the navigation bar contains a '< Back' button.

Can someone explain how a login / logout flow needs to be implemented in storyboard and programatically in Swift. I've seen some Objective-C examples out there, but can't seem to find one in Swift. If anyone has a good example, it would be really helpful.

like image 744
SB2015 Avatar asked Aug 17 '15 23:08

SB2015


2 Answers

Swift 4 In LoginViewController's login button action

@IBAction func abtn_login(_ sender: Any) {

        let appDel:AppDelegate = UIApplication.shared.delegate as! AppDelegate
        let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        let centerVC = mainStoryBoard.instantiateViewController(withIdentifier: "InitialScreenTabBarController") as! InitialScreenTabBarController

        // setting the login status to true
        UserDefaults.standard.set(true, forKey: "isUserLoggedIn")
        UserDefaults.standard.synchronize()
        appDel.window!.rootViewController = centerVC
        appDel.window!.makeKeyAndVisible()


}

validate login according to your scenario.

In View Controller having the logout button.

@IBAction func abtn_logout(_ sender: Any) {

    UserDefaults.standard.set(false, forKey: "isUserLoggedIn")
    UserDefaults.standard.synchronize()

    let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController

    let appDel:AppDelegate = UIApplication.shared.delegate as! AppDelegate

    appDel.window?.rootViewController = loginVC

}

In AppDelegate, didFinishLaunchingWithOptions

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

    if(userLoginStatus)
    {
        let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let centerVC = mainStoryBoard.instantiateViewController(withIdentifier: "InitialScreenTabBarController") as! InitialScreenTabBarController
        window!.rootViewController = centerVC
        window!.makeKeyAndVisible()
    }
like image 185
Abhooshan Bhattarai Avatar answered Oct 05 '22 09:10

Abhooshan Bhattarai


The < Back button is appearing because you are doing a push segue from the login view controllers to the tab bar controller. A better flow for the app would be to have the tab bar controller be your initial view controller. Then, in its viewDidAppear method, check that the user is logged in. If the user isn't logged in, segue modally WITHOUT animation to your login view controller. This will all happen without the user noticing and will allow the storyboard setup you want

like image 20
keithbhunter Avatar answered Oct 05 '22 09:10

keithbhunter