How to get and store Username and password when app is launched for the first time?
I have a tabbar app and I want to add login window t开发者_StackOverflow中文版hat will show just for the first time the app is launched. and want username and password to be hard coded in the app. can anyone help me with this please.
You probably want to use KeyChain to store the information. Keychain is a secure way to store passwords since the storage values are encrypted.
Apple also has a sample project, GenericKeychain
:
This sample shows how to add, query for, remove, and update a keychain item of generic class type. Also demonstrates the use of shared keychain items. All classes exhibit very similar behavior so the included examples will scale to the other classes of Keychain Item: Internet Password, Certificate, Key, and Identity.
Another example of how to implement this using SFHFKeychainUtils is available here.
if your wanting to save the user name and password in a secure way, store that in the devices key chain - apple provide a wrapper class for this to make it easier to use. you can also have a value stored in the device NSUserDefaults that can be set when the user has ran the app for the first time and this value can be evaulated on subsequent logins to see if it is required to show the login page or go directly to the relevant view
keychain stuff here -ME
NSUserDefault stuff here - ME
I like SFHFKeychainUtils, but if you want it to be removed upon deletion (I see this in another comment), you'll need to store it with the app in the documents directory, preferably with NSUserDefaults:
[[NSUserDefaults standardUserDefaults] setObject:@"myuser" forKey:@"username"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSUserDefaults standardUserDefaults] stringForKey:@"username"];
This does not manage security but will delete along with the app. You can check for the username and if it does not exist or if login fails upon use, bring up your login view as a modal dialog.
Consider using the keychain instead. Even though the password will stay when the app is deleted, it will be encrypted.
精彩评论