NSUserDefaults, credentials removal on uncheck iPhone - iphone

I am using below code for for saving the username password, credentials saved properly but the issue is when I again run the application, it shows the credentials in textfields but checkbox remains empty (i am using images to show the box checked and unchecked).
My questions are:
1. How can i save the state of checkbox if it is checked or unchecked and
2. If it is unchecked how can i remove the credentials from nsuserdefaults.
- (IBAction)checkboxButton:(id)sender{
if (checkboxSelected == 0){
[checkboxButton setSelected:YES];
NSString *user = [userNameField text];
NSString *passwd = [passwordField text];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:user forKey:#"Username"];
[defaults setObject:passwd forKey:#"Password"];
[defaults synchronize];
NSLog(#"Data Saved");
checkboxSelected = 1;
} else {
[checkboxButton setSelected:NO];
checkboxSelected = 0;
//Saving Username Password in file
}
}
thank you ... :)

Well you can save the state of the checkbox in the NSUserDefaults.
To read do the following.
BOOL isChecked = [[NSUserDefaults standardUserDefaults] boolForKey:#"loginCheckBox"];
It will return NO (false) if it is not set.
To set the state:
[[NSUserDefaults standardUserDefaults] setBool:checkboxButton.selected forKey:#"loginCheckBox"];
To remove the values from the NSUserDefaults just set the properties to nil:
[defaults setObject:nil forKey:#"Username"];
[defaults setObject:nil forKey:#"Password"];
Also I can strongly advice you to store the password in the keychain.
since all the data saved in NSUserDefaults is stored plain text.
You can use the easy to SSKeyChain for accessing keychain api more easily.

for saving checkmark state you have to maintain a variable which has to be saved and read again to set the last state of checkmark and for removing values from NSUserDefaults : you can use:
removeObjectForKey method

Related

How can I create session?

I need to session. I couldn't find any thing about this issue. Therefore I used appdelegate global variable, but app global variable not save value long time.
I mean, my aim is login page. I don't want user to sign in many time in my app. How can I realize this approach?
save the data in NSUserDefaults,
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:username forKey:#"username"];
[defaults synchronize];
NSString *username=[defaults objectForKey:#"username"] ;
You can use NSUserDefaults for this purpose.
Here's an example for you. Good luck!
- (void) createSession:(NSObject *anyObject) {
//write an object to NSUserDefaults with the key of "session"
[[NSUserDefaults standardUserDefaults] setObject:anyObject forKey:#"session"];
//persist the value in the store
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSObject*) getSession {
//get an object from NSUserDefaults with the key of "session"
return [[NSUserDefaults] standardUserDefaults] objectForKey:#"session"];
}
- (void) exampleUsage {
NSObject* mySessionObject = [self getSession];
if (mySessionObject != nil) {
//i have a session
} else {
//i do NOT have a session
}
}
You could check with NSUserDefaults
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
if(![defaults boolForKey:#"login"])
{
/// view to login or signup what ever
}
else
{
/// user already logged in
}
You must use NSUserDefault To Login Window.
NSUserDefaults def=[NSUserDefaults standardUserDefaults];
[def setObject:delegate.txtfield1.text forKey:#"UserName"];
[def setObject:delegate.txtfield2.text forKey:#"Password"];
[def synchronize];
If you want to retrive the NSUSERDefault data used this code.
NSString *UserName=[defaults objectForKey:#"UserName"] ;
NSString *Password=[defaults objectForKey:#"Password"] ;
And For Checking the status
if(![def boolForKey:#"UserName"])
{
}
else
{
}
try this one might be helpful to you......

Password Stored in the memory

During the login request, password get stored. After this request I'm setting the password string with an empty character.
Does it really store in memory, if it does how would I check? And how do I wipe that off.
I'm already using ARC in my App.
//Stroe user information
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:#"Moses" forKey:#"username"];
[prefs setInteger:42 forKey:#"age"];
[prefs synchronize];
//Getting the stored information
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *username = [prefs stringForKey:#"username"];
NSInteger age = [prefs integerForKey:#"age"];
Storing a password in NSUserDefaults is insecure, since anyone who get access to the iPhone, can view the password.
you can check the string is empty..via
if([yourString isEqualToString:#""]){
NSLog(#"empty string");
}

NSUserDefaults - storing and retrieving data

I have some data that's been stored using NSUserDefaults in one view and then being displayed in another view. The issue I'm having is that when the user changes the data and then returns to the view where the data is displayed (in a UILabel), the data that was first saved is displayed instead of the newer saved text.
I think I need to do something with viewDidAppear perhaps, so that every time the view appears the newest saved data is displayed.
here's the code that Im displaying the NSUserDefaults stored info on a UILabel:
NSString *aValue = [[NSUserDefaults standardUserDefaults] objectForKey:#"myTextFieldKey"];
NSLog(#"Value from standardUserDefaults: %#", aValue);
NSLog(#"Label: %#", myLabel);
myLabel.text = aValue;
if someone could point me in the right direction that would be great,
thanks
Put this text in
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear: animated];
NSString *aValue = [[NSUserDefaults standardUserDefaults] objectForKey:#"myTextFieldKey"];
NSLog(#"Value from standardUserDefaults: %#", aValue);
NSLog(#"Label: %#", myLabel);
myLabel.text = aValue;
}
And in your "edit" view in - viewWillDisappear: save changes in NSUserDefaults
When saving data to NSUserDefaults, it doesnt immediately write to the Persistent Storage. When you save data in NSUserDefaults, make sure you call:
[[NSUserDefaults standardUserDefaults] synchronize];
This way, the value(s) saved will immediately be written to Storage and each subsequent read from the UserDefaults will yield the updated value.
Do not forget the use synchronize when you set some value
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:YOUR_VALUE forKey:#"KEY_NAME"];
[defaults synchronize];
Now you can place your code in viewWillAppear method to retrieve the value from defaults, this will help you fetch the currentsaved value for your desired key.
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString* strValue = [defaults objectForKey:#"KEY_NAME"];
myLabel.text = strValue != nil ? strValue : #"No Value";
Hope it helps
In Swift we can do it following way:
To save value:
let defaults = UserDefaults.standard
defaults.set(YOUR_VALUE, forKey: "YOUR_KEY_NAME")
To get value:
let defaults = UserDefaults.standard
let data = defaults.objectForKey("YOUR_KEY_NAME")
For more details visit:
https://www.hackingwithswift.com/example-code/system/how-to-save-user-settings-using-userdefaults

Stay Signed in option for iPhone app

I need to implement a "Stay Signed in" or "Remember Me" check box to my iPhone app. How do I implement this? How do I store the user id?
Help is highly appreciated,
Thanks,
VKS
#VKS
1
-(void)autologin:(id)sender
{
ischecked =!ischecked;
UIButton *check = (UIButton*)sender;
if(ischecked == NO)
[check setImage:[UIImage imageNamed:#"checkbox1.png"] forState:UIControlStateNormal];
else
[check setImage:[UIImage imageNamed:#"checkbox-pressed.png"] forState:UIControlStateNormal];
}
2
if(ischecked == YES)
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setObject:mainDelegate.uName forKey:#"username"];
[standardUserDefaults setObject:mainDelegate.uPassword forKey:#"password"];
[standardUserDefaults synchronize];
}
and for retrieving the password/username
3
if(isChecked == YES)
{
NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
NSString *pass = [standard objectforkey:#"password"];
NSString *user = [standard objectforkey:#"username"];
}
if ([pass == nil] || [user == nil])
{
// Show the alert view that user is not valid.
}
else
{
// User is valid.
}
Since NSUserDefaults is not secure, i would recommend using iOS keychain to save password persistently.
look here: http://iosdevelopertips.com/core-services/using-keychain-to-store-username-and-password.html
Check whether check box is checked and if checked then
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setObject:mainDelegate.uName forKey:#"username"];
[standardUserDefaults setObject:mainDelegate.uPassword forKey:#"password"];
[standardUserDefaults synchronize];
This will save your variables and while login check for the saved NSUserDefaults.
if (standardUserDefaults) {
//Here check for the values if there are any values then use the same and login
}
Check any NSUserDefaults tutorials on google, you will get the sample code.

Saving and Retrieving UILabel value with NSUserDefaults

I trying to save UILabel value to NSUserDefaults. I did IBAction with this code:
-(IBAction)saveData:(id)sender {
NSString *resultString = label.text;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:resultString forKey:#"result"];
[prefs synchronize];
}
Then, I connect it to button with Touch Up Inside.
What log shows after I pressed the button:
result = 0;
When I pressed a second time, then it works.
result = "28.34";
What I'm doing wrong and how can I retrieve a result?
EDIT
With this code I display result in log. I put it to same action.
NSLog(#"%#", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
Your code looks correct, so the issue is probably not the function you posted but rather the code that contains your NSLog. You might be logging the value in a way that misses the first time it gets set.
Your log statement should be
NSLog(#"%#",[[NSUserDefaults
standardUserDefaults]
objectForKey:#"result"]);
This should be after you set the object in NSUserDefaults.
Add this to your applicationDidFinishLaunching in appDelegate or in init method in viewController(you have to create one) :
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if(prefs == nil)
{
[prefs setObject:#"anything" forKey:#"result"];
[prefs synchronize];
}