Is there an Android sharedpreferences equivalent in iOS to save credentials (Username and Password)?

StackOverflow https://stackoverflow.com/questions/21176201

Вопрос

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.

Это было полезно?

Решение

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.

Другие советы

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

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

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top