0

At the moment I'm storing both the username and password for communication with the server (through Alamofire) in the iOS keychain. However, every now and then the keychain returns nil when trying to retrieve these items. Therefore, storing the username and password in the keychain is not a reliable option. Is there a better way to communicate with the server? Maybe a session cookie or something?

Tometoyou
  • 7,792
  • 12
  • 62
  • 108
  • 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) – Alessandro Ornano May 16 '16 at 17:06
  • @AlessandroOrnano I am already storing the username/password in the keychain but the problem is it's unreliable (even using Apple's sample code), so I'm looking for something better. – Tometoyou May 16 '16 at 17:14
  • You must reformulate your question with some code because, like this, you can find your answers (Is there a better way to communicate with the server? Maybe..) all in this link man – Alessandro Ornano May 16 '16 at 17:20
  • Does this occur when the app goes into the background or goes to sleep? – Ruchira Randana May 16 '16 at 17:31
  • @RuchiraRandana Nope I'll be using it and it'll randomly return nil. I've tried several keychain wrappers (including Apple's own) as well as different devices, and it still occurs. – Tometoyou May 16 '16 at 17:42
  • Can you try this and see whether it solves the issue? http://stackoverflow.com/questions/10536859/ios-keychain-not-retrieving-values-from-background/10583042#10583042 – Ruchira Randana May 16 '16 at 17:43

1 Answers1

0

Do you have freedom to modify the server-side code? If so, you could check out JSON Web Tokens: https://jwt.io/

If not, instead of the keychain we could use NSUserDefaults (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/). But remember that this is an interface to store user preferences, it is insecure to store passwords in it. That's why you should consider using tokens.

See this related question: NSUserDefaults or keychain is better to save username and password in iPhone app

Community
  • 1
  • 1
Caio Tomazelli
  • 513
  • 5
  • 14