Thiha Aung
Thiha Aung

Reputation: 5096

How to store a user's data to the device?

I am creating a sign in view. I have two UITextField's which are for the user's email and password. When user clicks the 'Sign In' button, these two parameters are sent to my API and check to see if the user exists. If this user exists, my API gets the user's email, name, status and token, and return the JSON result to me. The token had expire date. Hence, I need to store the token and email in the device so the user doesn't have to repeatedly sign in.

I am newbie.So,any code help is appreciated. Thank you.

Update

I have imported LockSmith.swift and LockSmithRequest.swift into my project.And I had also added KeyChainService.swift same as matt blog to project.But I am occurring these problem and my xcode is 6.2.

enter image description here

Upvotes: 3

Views: 1854

Answers (3)

Ashley Mills
Ashley Mills

Reputation: 53111

If you're looking for a very simple drop-in Keychain wrapper, you could take a look at this:

https://github.com/ashleymills/keychain.swift

Upvotes: 1

Suragch
Suragch

Reputation: 511706

Keychains should be used to store users' emails and passwords. Here are some posts that talk about doing this in Swift:

Upvotes: 3

Prince Shekhar
Prince Shekhar

Reputation: 119

If you just want to store data within your app, you can use one of the following

  • NSUserDefaults (Suitable for basic small details)
  • CoreData (Suitable for large amounts of dynamically changing data that needs to be queried)
  • Files (Suitable for static content)

In your case, I would suggest using NSUserDefaults.

Assuming you have the information to be stored in the form of NSDictionary. You can follow these steps:

[[NSUserDefaults standardUserDefaults] setObject:[yourDictionary objectForKey:@"token"] forKey:@"token"];
[[NSUserDefaults standardUserDefaults] synchronize];

Later you can retrieve this key like this

[[NSUserDefaults standardUserDefaults] objectForKey:@"token"];

Hope it helps!

Upvotes: 1

Related Questions