iPhone Strange Settings Problem - iphone

I am having unexpected results with my application. I am using a settings bundle, and I want to the default switch to turn on. The switches are off when I start my application. But the sound and the shake are working. I just want the switch to be one when my application loads, and the sound and the shake to be enabled.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Set the application defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:#"YES" forKey:#"enableSound"];
NSDictionary *appDefaults2 = [NSDictionary dictionaryWithObject:#"YES" forKey:#"enableShake"];
[defaults registerDefaults:appDefaults];
[defaults registerDefaults:appDefaults2];
[defaults synchronize];
return YES;
}
Here is part of the code for the sound button:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
enabledSound = [defaults boolForKey:#"enableSound"];
Here is part of the code where I dectect the shake:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
enabledSound = [defaults boolForKey:#"enableSound"];
enabledShake = [defaults boolForKey:#"enableShake"];

It turns out in the settings bundle, that the Value for OFF and the Value for ON should be removed, and the application worked.

Related

iphone xcode 5 app iOS

How I can save value in TextField for showing after closing and opening app?
self.suma.text = [NSString stringWithFormat:#"%d", [self.a1.text intValue]+[self.a2.text intValue]];
I want to do it with "suma".
See documentation for NSUserDefaults.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Write
[defaults setObject:self.suma.text forKey:#"suma"];
// Read
self.suma.text = [defaults stringForKey:#"suma"];

Saving data in app delegate

I have a couple of arrays i wish to save when the application terminates. I implemented this using NSUserDefaults within app delegate. Can anyone take a look at my code, and see whats wrong? It doesn't work whatsoever.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
workouts = [NSMutableArray arrayWithObjects:nil];
menu = [NSMutableArray arrayWithObjects:#"Home",nil];
workoutNames = [NSMutableArray arrayWithObjects:nil];
routinesMade = [NSMutableArray arrayWithObjects:nil];
test = [NSMutableArray arrayWithObjects:nil];
defaults = [NSUserDefaults standardUserDefaults];
self.workouts = [defaults objectForKey:#"workouts"];
self.menu = [defaults objectForKey:#"menu"];
self.workoutNames = [defaults objectForKey:#"workoutNames"];
self.routinesMade = [defaults objectForKey:#"routinesMade"];
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application
{
defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:workouts forKey:#"workouts"];
[defaults setObject:menu forKey:#"menu"];
[defaults setObject:workoutNames forKey:#"workoutNames"];
[defaults setObject:routinesMade forKey:#"routinesMade"];
[defaults synchronize];
}
Btw, i declared defaults in the header file. Thanks guys!
I think I know what the problem is. Your code is inside the applicationWillTerminate: method. Unless you explicitly set your application not to run in background (by setting the 'Application does not run in background' key), it is almost certain that this method will never be called because by the time it gets terminated by the system, it will already have been suspended.
In this case consider saving the information you need in the applicationDidEnterBackground: method.
Hope this helps!
workouts and self.workouts are one property am i correct?
so you create array
workouts = [NSMutableArray arrayWithObjects:nil];
then override it with null because [defaults objectForKey:#"workouts"] contains null

How to remove user defaults [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I reset the NSUserDefaults data in the iPhone simulator?
In my project i am saving some values in NSUserdefaults with different keys.And i have reset button in my app when i click that button values stored in user defaults should remove.Is there any way to delete those values without keys,and is addSuitedNamed: and removeSuitedName can use in this scenario.
NSUserDefaults * myNSUserDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [myNSUserDefaults dictionaryRepresentation];
for (id key in dict) {
//heck the keys if u need
[myNSUserDefaults removeObjectForKey:key];
}
[myNSUserDefaults synchronize];
or
[NSUserDefaults resetStandardUserDefaults];
[NSUserDefaults standardUserDefaults];
With a selector being called on a button click you can achieve the same using
- (IBAction)btnResetUserDefaultsPressed:(id)sender {
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary* dictUserDefaults = [userDefaults dictionaryRepresentation];
for (id akey in dictUserDefaults) {
[userDefaults removeObjectForKey:akey];
}
[userDefaults synchronize];
}
For the second part of your question asked , You would certainly find this useful.
You can reset the values of NSUserDefault using resetStandardUserDefaults.
Check this code:
[NSUserDefaults resetStandardUserDefaults];
[NSUserDefaults standardUserDefaults];
Also you can:
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
Or you can use:
NSUSerDefaults *default = [NSUserDefaults standardUserDefaults];
NSDictionary *dictionary = [default dictionaryRepresentation];
for (NSString *key in [dictionary allKeys])
{
[default removeObjectForKey:key];
}
[default synchronize];

retrieve values from NSUserDefault in iPhone after setting again

In App delegate, I have following code:
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
NSString *alrmTime = #"10:00 AM";
[pref setObject:alrmTime forKey:#"alarmTime"];
[prefs synchronize];
From here I am getting from App delegate User Daeault in Controller A using code
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *tempAlarmTime = [defaults stringForKey:#"alarmTime"];
cell.textLabel.text = [NSString stringWithFormat:#"Remind At %#", tempAlarmTime];
Now, I need to set this userdefault in Controler B , For this m using this:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:mTimeIntervalSTR forKey:#"alarmTime"];
[prefs synchronize];
Now when I need to get this new value in Controller A its coming Null. Why this is happening, and how will I get new set value?
in Controler B use the following to change the userdefault value
NSString *timeString =[[[NSUserDefaults standardUserDefaults] objectForKey:#"alarmTime
"]mutableCopy];
timeString = mTimeIntervalSTR;
[[NSUserDefaults standardUserDefaults]setObject:timeString
forKey:#"alarmTime "];
[[NSUserDefaults standardUserDefaults]synchronize];
After adding a value, call [[NSUserDefaults standardUserDefaults] synchronize];.

How to keep my data in a array-iphone sdk

I'm trying to create an application, and in that I'm receiving some contents from net and loading into and array, if I quit and run the app again i'm losing the data. How can I store the data in the app itself. Should I use some kind of database? If so which one and how can I implement that? I need to store only some string variables.
EDIT:
My app is tabbar based app, and have two tabs. Loading the arrays in one tab and loading that array in a table in the other VC. once I load the array and move to the second tab, the table is loaded with the array. If i go back and add few more values and then if i check, the added datas is not displayed in the table. And also, when I quit the app the table lose all the values, I need to keep the values in the table, even after i quit the app. How can I do that?
here is my code:
in viewdidload:
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
[simplePlistDb setBool:YES forKey:#"isItWorking"];
[simplePlistDb setObject:alertList forKey:#"myArray"];
[simplePlistDb synchronize];
in cellforrowatindexpath:-
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
BOOL flag = [simplePlistDb boolForKey:#"isItWorking"];
if (flag)
{
NSArray *myArray = [simplePlistDb objectForKey:#"myArray"];
for (NSString *str in myArray){
NSLog(#"Str:%#", str);
[loadArray addObject:str];
}
cell.textLabel.text = [loadArray objectAtIndex:indexPath.row];
}
Thank you.
You will want to look into Core Data if you want to save a decent amount of data to the iPhone. However, if not, you can just load the items in the array into a plist and load them from there at launch.
Plist Writing: How to create a new custom property list in iPhone Applications
CoreData: http://www.raywenderlich.com/934/core-data-tutorial-getting-started
Edit--->
As Nekto said, you can also use NSUserDefaults, but I advise mainly using NSUserDefaults for simple NSStrings and integers.
I think for your purposes NSUserDefaults will be enough : NSUserDefaults Class Reference.
Examples:
// save
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
[simplePlistDb setBool:YES forKey:#"isItWorking"];
[simplePlistDb setObject:[NSArray arrayWithObjects:#"very", #"cool", nil]];
[simplePlistDb synchronize];
// restore
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
BOOL flag = [simplePlistDb boolForKey:#"isItWorking"];
if (flag)
{
NSArray *myArray = [simplePlistDb objectForKey:#"myArray"];
for (NSString *str in myArray)
NSLog(#"%#", str);
}
You can use NSUserDefaults in order to store your data till the time your app is installed in your device.
How to write in NSuserDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"yourObject1" forKey:#"correspopndingKey1"];
[defaults setObject:#"yourObject2" forKey:#"correspopndingKey2"];
[defaults synchronize];
How to read from NSuserDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *temp1 = [defaults objectForKey:#"correspopndingKey1"];
NSString *temp2 = [defaults objectForKey:#"correspopndingKey2"];
You can store NSArray in a similar way.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:objArray forKey:#"correspopndingKey"];
[defaults synchronize];