Can't set NSUserDefaults value through variable in IOS - ios

In my iOS app when I'm logging in I used set UserID and loginstatus into NSUserDefaults like this.
#implementation Login{
NSInteger UserId;
NSUserDefaults *preferences;
}
- (IBAction)LogInClick:(UIButton *)sender {
[preferences setInteger:UserId forKey:#"UserId"]; //UserId=40;
[preferences setBool:YES forKey:#"loginstatus"];
[preferences synchronize];
NSLog(#"All NSUserDefaults: %#", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
NSString *uID = [preferences stringForKey:#"UserId"];
NSLog(#"UserID: %#",uID);
}
But the values are not setting in the NSUserDefaults. I tried to log all NSUserDefaults value and also the single UserID value but all fields are coming null.
Later I use this code to logout
- (IBAction)LogOutClick:(UIButton *)sender {
[[NSUserDefaults standardUserDefaults] setObject:0 forKey:#"UserId"];
[[NSUserDefaults standardUserDefaults] setObject:NO forKey:#"loginstatus"];
/* [preferences setInteger:0 forKey:#"UserId"];
[preferences setBool:NO forKey:#"loginstatus"];
[preferences synchronize];*/
NSLog(#"All NSUserDefaults: %#", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
}
First when i logged out it all sets to zero and now the values are not setting. Can anybody tell me what the problem is?

That's because NSUserDefaults *preferences; is just a pointer. And it's value is nil, I presume. You should initialize it. For instance:
- (IBAction)LogInClick:(UIButton *)sender {
preferences = [NSUserDefaults standardUserDefaults];
[preferences setInteger:UserId forKey:#"UserId"]; //UserId=40;
[preferences setBool:YES forKey:#"loginstatus"];
[preferences synchronize];
...
May be a more logical place to initialize preferences would be an init method, or something else that you would consider appropriate for this purpose.

You haven't get the standard userdefaults reference. Change this method
- (IBAction)LogInClick:(UIButton *)sender {
preferences = [NSUserDefaults standardUserDefaults];
[preferences setInteger:UserId forKey:#"UserId"]; //UserId=40;
[preferences setBool:YES forKey:#"loginstatus"];
[preferences synchronize];
NSLog(#"All NSUserDefaults: %#", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
NSString *uID = [preferences stringForKey:#"UserId"];
NSLog(#"UserID: %#",uID);
}

Related

iOS: Force view reload after locale change

I have setup my app to support a base internationalization for English and added a local for Spanish. I'm trying to provide an option to force the app to use a locale select by the user via a segmented control.
What I would like to do is have the screen "refresh" or "redraw" with the new selected locale.
Here is my action for the segmented control:
- (IBAction)segmentSwitch:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
NSUserDefaults *prefs;
prefs = [NSUserDefaults standardUserDefaults];
if (selectedSegment == 0) {
[prefs setObject:#"en" forKey:#"language"];
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"en", nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"English");
}
else
{
[prefs setObject:#"es" forKey:#"language"];
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"es", nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"Spanish");
}
[self.view setNeedsDisplay];
}
My hope is that the screen would refresh and display the selected language view. If I close out the app and restart, it does display new view.
Here is my viewWillAppear:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// get the saved language preference
NSUserDefaults *prefs;
prefs = [NSUserDefaults standardUserDefaults];
NSString *stringVal = [prefs stringForKey:#"language"];
// Make sure we have a valid value
if (stringVal != nil) {
// Set the language preference
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:stringVal, nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate
// Turn on the appropriate segment
if ([stringVal isEqual: #"en"]) {
NSLog(#"English");
languageSwitch.selectedSegmentIndex = 0;
} else {
NSLog(#"Spanish");
languageSwitch.selectedSegmentIndex = 1;
}
} else {
// default to English
languageSwitch.selectedSegmentIndex = 0;
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"en", nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate
}
}
I had thought that following would force a screen refresh but it doesn't seem to be working:
[self.view setNeedsDisplay];
Any thoughts or suggestions would be appreciated. Thanks in advanced.

How to save a BOOL in iOS app

I used the following code to save a BOOL but it's not working.
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"save"];
StrongMAN = [[NSUserDefaults standardUserDefaults] boolForKey:#"save"];
Are there other ways to save a BOOL?
You should add the following code
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:#"kLogIn"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"testingBool"];
BOOL test = [[NSUserDefaults standardUserDefaults] boolForKey:#"testingBool"];

How to save the BOOL [duplicate]

I used the following code to save a BOOL but it's not working.
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"save"];
StrongMAN = [[NSUserDefaults standardUserDefaults] boolForKey:#"save"];
Are there other ways to save a BOOL?
You should add the following code
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:#"kLogIn"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"testingBool"];
BOOL test = [[NSUserDefaults standardUserDefaults] boolForKey:#"testingBool"];

Detect if user opened the View for the first Time In iOS

I know how I can detect if the application first time opened using NSUserDefault:
BOOL didRunBefore = [[NSUserDefaults standardUserDefaults] boolForKey:#"didRunBefore"];
if (!didRunBefore) {
//Your Launch Code
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"didRunBefore"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
The Issue: I want an Alert for ever view explaining about what features it contains and only open this alert when the app is first launched?
As an OOP programmer you can make a common public method.
+ (BOOL)checkWhetherRunBefore:(NSString *)key
{
return [[NSUserDefaults standardUserDefaults] boolForKey:key];
}
+ (void)hasRunForMyClass:(NSString *)key
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
And in your ViewController , you can code in the viewWillAppear or viewDidAppear like this :
- (void)viewWillAppear
{
if(![HelpController checkWhetherRunBefore:NSStringFromClass([self class])])
{
//do your thing
[HelpController hasRunForMyClass:NSStringFromClass([self class])]
}
}
You just need to put your code in viewDidLoad
viewDidLoad will run only one time when app load your view. (except low memory)
Next time you load the view again in viewDidLoad, you can check bool didRunBefore
You can use any key you want.
In FirstViewController:
BOOL didRunBefore = [[NSUserDefaults standardUserDefaults] boolForKey:#"FirstViewController_didRunBefore"];
if (!didRunBefore) {
//Your Launch Code
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"FirstViewController_didRunBefore"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
In SecondViewController:
BOOL didRunBefore = [[NSUserDefaults standardUserDefaults] boolForKey:#"SecondViewController_didRunBefore"];
if (!didRunBefore) {
//Your Launch Code
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"SecondViewController_didRunBefore"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
yo have to need to detect application first launch, you can detect t like this
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:#"firstRun"])
[defaults setObject:[NSDate date] forKey:#"firstRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
You can then test for it later...
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults objectForKey:#"firstRun"])
{
// do something or not...
}

ios - setting a standardUserDefaults while running the simulator does not save the data

I am working with the simulator, and I am doing something like this when the app starts to check if it was opened for the first time:
and then checking if that key/value is empty so that this code executes only once:
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if([standardUserDefaults objectForKey:#"first_time_cookie"] == nil)
{
[standardUserDefaults setBool:YES forKey:#"first_time_cookie"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
but every time I run the program, it executes again. Any idea what is going wrong here?
Just a guess: standardUserDefaults is nil?
You can do different!
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], #"first_time_cookie"];
[defaults registerDefaults:dict];
if ([defaults boolForKey:#"first_time_cookie"] == NO){
[defaults setBool:YES forKey:#"first_time_cookie"];
[defaults synchronize];
}
a bool is not an object. This should work
if([standardUserDefaults boolForKey:#"first_time_cookie"] == NO)
{
[standardUserDefaults setBool:YES forKey:#"first_time_cookie"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
try using
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"first_time_cookie"] == false)
and let me know if it works,
gung-ho

Resources