1

Coming from Android development I use SharedPreferences to store username and password to login to a server using OutputStreamWriter and HttpURLConnection. Now in iOS I am using NSMutableURLRequest to send the username and password. Is there anything in iOS to store the username and password like SharedPreferences in iOS? Take in mind that this is sensitive data so it needs to be secure.

jscs
  • 63,694
  • 13
  • 151
  • 195
Waqleh
  • 9,741
  • 8
  • 65
  • 103

4 Answers4

5

Yes, there is. Usernames and Passwords should be saved using Apples Keychain.Keychain is encrypted by default. https://developer.apple.com/library/ios/documentation/Security/Conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html

Also, take a look at this tutorial on how to use Keychain in iOS. http://maniacdev.com/2011/07/tutorial-how-to-use-the-ios-keychain-to-store-names-and-passwords

To save other user preferences, use NSUserDefaults.

Karim
  • 736
  • 3
  • 14
4

The counterpart of Android SharedPreferences is the IOS NSUserDefaults. In your case i suggest you to use Keychain thats secure your data

If you are moving to IOS development I strongly suggest to use cocoapods

Using it, i've found a framework that make easy work with keychain SSKeychain

bsorrentino
  • 1,413
  • 11
  • 19
1
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:self.numbers forKey:@"userName"];
[defaults synchronize];

U can use NSUserDefault to store the username password in iOS as like android SharedPreference. If you want more security for userName Password encrypt and then store it.

NSString *userName=[defaults ObjectforKey:@"userName"];

this will retrive the value.

Store value in NsUserDefault will accessible by your application only but not in case of jail broken ios device

CoolMonster
  • 2,258
  • 25
  • 50
  • I don't think it's appropriate to store usernames and passwords in NSUserDefaults since this isn't encrypted by default. Using keychain is more appropriate as this is encrypted by default, and is recommended (should be required) by Apple. – Karim Jan 17 '14 at 02:04
  • @KarimLanguedoc ye keychain is more secure than NSUserDefault. But the question is he want to know **equivalent of android SharedPreference in iOs** that's why I answered like that. – CoolMonster Jan 17 '14 at 02:14
0

You don't store them in the shared preferences, instead use the keychain, which is encrypted

KeychainItemWrapper* keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"KeychainTest" accessGroup:nil];
[keychain setObject:(__bridge id)kSecAttrAccessibleWhenUnlocked forKey:(__bridge id)kSecAttrAccessible];

NSLog(@"%@, %@", [keychain objectForKey:(__bridge id)kSecAttrAccount], [keychain objectForKey:(__bridge id)kSecValueData]);

[keychain setObject:@"example@email.com" forKey:(__bridge id)kSecAttrAccount];
[keychain setObject:@"MySuperSecretPassword" forKey:(__bridge id)kSecValueData];

NSLog(@"%@, %@", [keychain objectForKey:(__bridge id)kSecAttrAccount], [keychain objectForKey:(__bridge id)kSecValueData]);

https://developer.apple.com/library/ios/samplecode/GenericKeychain/Introduction/Intro.html (The KeychainItemWrapper needs -fno-objc-arc as it is not built for ARC)

http://b2cloud.com.au/how-to-guides/using-the-keychain-to-store-passwords-on-ios

SomeGuy
  • 9,670
  • 3
  • 32
  • 35
  • this is giving me the following error: Implicit conversion of C pointer type 'CFTypeRef' (aka 'const void *') to Objective-C pointer type 'id' requires a bridged cast – Waqleh Jan 17 '14 at 01:59
  • This happens because you are probably using "ARC". Are you? If so, you can set a flag to ignore ARC for that specific file. Select your project, Select your target, select Build Phases and expand the "Compile Sources" drop down. Look for KeychainItemWrapper (or whatever you call your keychain file) and set this flag. -fno-objc-arc – Karim Jan 17 '14 at 02:09
  • @Waqleh sorry I've updated it now to work with ARC, I've simply added bridged casts to each of the kSec keys and values. Also you will need to add -fno-objc-arc to your compiler flags for the KeychainItemWrapper.m file, see here http://stackoverflow.com/questions/6646052/how-can-i-disable-arc-for-a-single-file-in-a-project – SomeGuy Jan 18 '14 at 02:24