24

I am still very new to iOS development and swift and am not sure if I'm overthinking this. I am currently working on an application that upon opening requires the user to enter their login credentials. This gets tedious and frustrating because every time the app is closed and opened again the user has to sign in. Is there a simple way to make the program remember if a user is already signed in?

I was looking into CoreData but every example involves storing a new object every time and requires a query of some sort to fetch the information. Where as all I really need is a bool isLoggedIn and an int for the stored user ID.


Edit:

NSUserDefaults is exactly what I was looking for.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Nick
  • 263
  • 1
  • 2
  • 5
  • 1
    You may want to look into `NSUserDefaults` and `keyChain` if you need to store a password. – rmp May 04 '15 at 23:44
  • 'NSUserDefaults' are perfect just as others suggested. They are a simple and easy way to set permanent default values for simple data types. 'CoreData' is more complex and used to store more complex types of data but for simple things like integers, booleans, strings, etc. 'NSUserDefaults' are perfect. – Brennan Adler May 05 '15 at 00:45
  • I would use a simple JSON file and use GPG to encode it to some insane level, keychain can be synced to iCloud, i.e, not secure. I don't trust Apple with my passwords, why should you? With keychain, if someone steals/hacks your phone or laptop, you're probably doomed. – μολὼν.λαβέ May 05 '15 at 01:11
  • I would like to see this question reopened. It came up as the top (and only) SO for my search terms. Although the question seems a little broad at first , the two answers show that it attracts narrow on topic answers. The current answers are fine but I think this question would be helped by allowing new answers, too. – Suragch May 21 '16 at 00:13
  • The close reason can be changed to a duplicate of this question: https://stackoverflow.com/questions/31203241/how-to-use-userdefaults-in-swift – Suragch Nov 30 '17 at 02:51

2 Answers2

32

You can use NSUserDefaults to save information and retrieve it next time when the app launches.

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html

For example:

NSUserDefaults.standardUserDefaults().setObject("mynameisben", forKey: "username")

let userName = NSUserDefaults.standardUserDefaults().stringForKey("username")

Update for Swift 3+

UserDefaults.standard.set("mynameisben", forKey: "username")

let userName = UserDefaults.standard.string(forKey: "username")
Quinn
  • 7,072
  • 7
  • 39
  • 61
Ben Lu
  • 2,982
  • 4
  • 31
  • 54
  • NSUserDefaults has a specific method to retrieve a String. It is called NSUserDefaults().stringForKey(). BTW objectForKey() returns an optional you should use if let to unwrap it or use "??" nil coalescing operator to return a default value instead of nil if there is no stored value – Leo Dabus May 05 '15 at 04:59
  • 1
    https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html#//apple_ref/occ/instm/NSUserDefaults/stringForKey: – Leo Dabus May 05 '15 at 05:01
  • 1
    `NSUserDefaults.standardUserDefaults().synchronize()` It's necessary? – eliasRuizHz Jul 14 '15 at 22:25
  • 11
    In Swift 3, `NSUserDefaults` has been renamed to `UserDefaults`, `standard` is a property of that object, and the method signatures are `set(Any?, forKey: string)` and `.object(forKey: string)`, as well as retrieval methods for other types. – trey-jones Jan 24 '17 at 15:01
  • If a user has two app installations will there be two databases where values are stored separate? – lolelo May 03 '21 at 10:06
11

Use NSUserDefaults

Let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject("User", forKey: "userName")

Check if User name exists when application is started in your AppDelegate.swift

didFinishLaunchingWithOptions

If username exists, skip login page

To check if NSUserDefaults is nil

if (defaults.objectForKey(userName) != nil) { 
// Skip Login 
}
else {// Show login 
} 
Exceptions
  • 1,174
  • 2
  • 9
  • 28
  • How exactly do you "skip login"? is this as simple as making the authenticated view the root controller? and then displaying the login on top if no one has been authenticated (as determined by NSUserDefaults)? – Neurax Mar 24 '16 at 01:26