3

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

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Thiha Aung
  • 5,036
  • 8
  • 36
  • 79
  • possible duplicate of [iOS: How to store username/password within an app?](http://stackoverflow.com/questions/6972092/ios-how-to-store-username-password-within-an-app) – Dima Mar 18 '15 at 05:03
  • 1
    I dont know about Objective C.If u dont mind,can u please share in Swit.And I am newbie,too.Thank You – Thiha Aung Mar 18 '15 at 05:12
  • `Keychain` is better option over `NSUserDefaults`. [Keychain](http://stackoverflow.com/questions/24324285/adding-items-to-and-querying-the-ios-keychain-with-swift). Hope this helps. – Bista Mar 18 '15 at 10:20

3 Answers3

3

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

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • Can i also store the token that are back from API? – Thiha Aung Mar 18 '15 at 08:15
  • In the last link I posted the author has a `saveToken` method. I suggest you try to follow that post as closely as you can. If it doesn't work then come back and update your question. – Suragch Mar 18 '15 at 09:00
  • @Suragh,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."http://puu.sh/gGwNV/fb55409cbf.png" – Thiha Aung Mar 19 '15 at 08:25
  • At the bottom of the blog there is an update that says: "Xcode 6.1: @godzirra let me know on Twitter that doing let kSecClassValue = kSecClass.takeRetainedValue() as NSString no longer works on Seed 2. Instead, you need to do let kSecClassValue = NSString(format: kSecClass). This applies to all of the arguments we provide to the keychain queries listed at the top of the snippet." – Suragch Mar 19 '15 at 13:09
  • I updated your question so that your progress is more visible to other people. You can make further updates to your question yourself as you make more progress. – Suragch Mar 19 '15 at 13:46
1

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!

1

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

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • Thanks for your effort,Ashley.I am really appreciate for your help.It really helpful to newbie in swift like me.But,I will be glad to accept it if your library support for swift 1.2.Thank You,Sir. – Thiha Aung Apr 28 '15 at 03:43
  • @Htarwara6245 The current version does support Swift 1.2 – Ashley Mills Apr 28 '15 at 06:37
  • 1
    ,thank you sir.I gonna use your lib now.If any bug or issue occur,i will write it on github. – Thiha Aung Apr 28 '15 at 07:02