0

When user Sign In, I'm storing some value into userDefaults and checking user at main screen is it already logged in or not . Is it good idea to use userDefaults or there any other Method to check user logged in status.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • I think UserDefault is the best and easiest way to achieve your requirement – Vikky Apr 26 '18 at 05:53
  • 3
    Why do you think there is a problem with this? – Sweeper Apr 26 '18 at 05:54
  • it will slow down my app or not ?, I'm thinking @Sweeper –  Apr 26 '18 at 05:58
  • Well, it depends upon the type of data you are saving in userDefaults, if the data is sensitive(like passwords or hash) then it is recommended that you should not save it in user-defaults, rather you can use Keychain and store it in a encrypted form. For more information you can refer this link https://stackoverflow.com/questions/35900523/how-secure-is-nsuserdefaults-on-ios-8-9 – Vinaykrishnan Apr 26 '18 at 05:59
  • @BaljinderKumar Worry about speed only when there is a problem. Also, are you sure `UserDefaults` is the one that's causing the problem? – Sweeper Apr 26 '18 at 06:00
  • That (slow down) thing might be case if you have large data and each time you require value you get from UserDefault.So when app launches you can fetch all required values from UserDefault and show from there rather than accessing each time from UserDefault – Vikky Apr 26 '18 at 06:02
  • May be you are looking for this https://stackoverflow.com/q/19962276/1597744 – Toseef Khilji Apr 26 '18 at 06:19

5 Answers5

1

Yes, you can save access token or user id in Userdefaults and check whether the user logged in or not.It is used to navigate to Home page or anyother if the user has already logged in.You can write a code for navigation in your Appdelegate.

Naren Krish
  • 259
  • 2
  • 14
0

I would rather suggest Keychain instead of UserDefaults.

A keychain is an encrypted container that holds passwords for multiple applications and secure services. Apple Inc. uses keychains as password management system in Mac OS and iOS.

Having said that, all the sensitive information should go to the Keychain.
As per your point on checking user status each time, it is the right way to go. Mobile apps don't generally show login screen each time unless it is a critical app like that of banking.

Nitish
  • 13,845
  • 28
  • 135
  • 263
  • How I can use this in iOS app –  Apr 26 '18 at 06:01
  • Quite simple. Check this out - https://developer.apple.com/documentation/security/keychain_services/keychain_items/adding_a_password_to_the_keychain – Nitish Apr 26 '18 at 06:02
  • Remember, saving only the status would be ok to be saved in the user defaults (whether the user is loggedin or not). Keychain would be useful when it comes to the need of saving sensitive data, such as the password. – Ahmad F Apr 26 '18 at 06:20
  • @AhmadF : Totally agree – Nitish Apr 26 '18 at 06:20
0

Yes, UserDefaults is the best way to store information that isn't large. You can store your users preferences in UserDefaults.

Generally, in you didFinishLaunching method in AppDelegate, you can do something like this:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if UserDefaults.standard.value(forKey: "userId") != nil {
        /// etc.
    }
return true
}
Akshansh Thakur
  • 5,163
  • 2
  • 21
  • 38
  • "UserDefaults is the best way to store information that isn't large" I don't agree with it, especially when it comes to sensitive data... You could check the other answers about mentioning the Keychain. Also The question is about: Is what you implemented is a good idea, not how to implement it. – Ahmad F Apr 26 '18 at 06:21
  • I also mentioned that User Defaults is there for storing user "Preferences". Nowhere did I say that it should store sensitive data. And just to check whether the user is logged in as asked by op, you can use preference variables. – Akshansh Thakur Apr 26 '18 at 06:33
0
var isUserLoggedIn: Bool {
    return UserDefaults.standard.value(forKey: "userDetail") != nil
}

var currentUser : [String:Any] {
    set{
        guard let unwrappedKey = newValue else{
            return
        }
        UserDefaults.standard.set(unwrappedKey, forKey: "userDetail")
        UserDefaults.standard.synchronize()
    } get {

        guard let userDict =  UserDefaults.standard.object(forKey:"userDetail") else {
            return nil
        }
        return userDict
    }
}

func removeCurrentUser(){
    if UserDefaults.standard.value(forKey: "userDetail") != nil {
        UserDefaults.standard.removeObject(forKey: "userDetail")
        UserDefaults.standard.synchronize()
    }
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if isUserLoggedIn {
  //etc..
}
return true
}

Now you can check user login or not using isUserLoggedIn property. You can also create user model class.

Pratik Sodha
  • 3,679
  • 2
  • 19
  • 38
0

If a piece of data is not sensitive (e.g., default font size), store it in NSUserDefaults.

If it needs to be secure from casual snooping (e.g., user's password), store it in the Keychain.

If it needs to be secure from the user (e.g., registration code), you will need to roll your own encryption, then store the data wherever you like.

In normal iPhones and iPads it's secure to store in UserDeafaults but in jailbroken devices one can get those data easily. That's why it's preferred to store sensitive data into Keychain.

Arnab
  • 4,216
  • 2
  • 28
  • 50