简体   繁体   中英

keep user logged in ios app swift

I have made app which include login and signup functionality. When I run my app at that time login and other functionality everything is going ok but when I stop my app and run it again login page showing again can I make user logged in when again app launch. Below is my code which I have used in my app.

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)

    let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
    let isLoggedIn:Int = prefs.integerForKey("ISLOGGEDIN") as Int
    if (isLoggedIn != 1) {
        self.performSegueWithIdentifier("goto_login", sender: self)
    } else {
        self.usernameLabel.text = prefs.valueForKey("USERNAME") as? String
    }
}


import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // Override point for customization after application launch.

        /*

        var rootViewController = self.window!.rootViewController

        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main" , bundle: nil)

        var isUserLoggedIn:Bool = NSUserDefaults.standardUserDefaults().boolForKey("isUserLoggedIn")
        if(!isUserLoggedIn){
            var loginViewController = mainStoryboard.instantiateViewControllerWithIdentifier("loginview") as! LoginViewController

            window!.rootViewController = loginViewController
            window!.makeKeyAndVisible()

        }else{
            var protectedPage = mainStoryboard.instantiateViewControllerWithIdentifier("profail") as! ProfailViewController
            window!.rootViewController = protectedPage
            window!.makeKeyAndVisible()

        }

        */

        return true
    }

    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {

        return true 
    }
}

Within AppDelegate:-

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    let userLoginStatus = UserDefaults.standard.bool(forKey: "isUserLoggedIn")
    
if(userLoginStatus)
{
    
    let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    .
    .

}

Within LoginViewController(Where you have placed business logic for login validation):-

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

Within OtherViewController(Where IBAction for Logout is kept):-

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

In "didFinishLaunchingWithOptions" method check for user already logged in or not, if yes that set the ProfailViewController to window rootViewController

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let isUserLoggedIn:Bool = NSUserDefaults.standardUserDefaults().boolForKey("isUserLoggedIn")
    if(isUserLoggedIn) {
        let mainStoryboard = UIStoryboard(name: "Main" , bundle: nil)
        let protectedPage = mainStoryboard.instantiateViewControllerWithIdentifier("profail") as! ProfailViewController
        window!.rootViewController = protectedPage
        window!.makeKeyAndVisible()
    }
    return true
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM