I moved some of my apps settings form inside the app to the Settings menu
Everything works fine on the simulator (as usual:D) but when I try to deploy to a device it can't find the settings values in my NSUserDefaults.
In the Settings menu I can see my app and it's settings, but when I run this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults synchronize];
NSString *loc=[defaults objectForKey:#"currency"];
NSLog(#"start");
NSLog(#"--- %#",loc);
[defaults setObject:loc forKey:#"localisation"];
I get :
2012-09-17 15:51:08.240 MyApp[259:707] start
2012-09-17 15:51:08.243 MyApp[259:707] --- (null)
Any Ideas ?
NSUserDefaults has a method registerDefaults:.
Related
Some of the variables are accesses from within Info.plist file like $(VARIABLE):
Is it possible to do the same in Settings.bundle?
I need to display there my current ios app version:
You cannot dynamically change the Settings.bundle. Settings.bundle always use static plist.
But you can change it using NSUserDefaults
//Set app version
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:[[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleShortVersionString"] forKey:#"appVersion"];
[defaults synchronize];
You can put above code to:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
I have an existing app in ios now I want to integrate some code to check whether the app is first install or updated or same version. but the problem is that if an old user update the app the first launch is working while the app is updated.Can anybody tell me how to handle it ?
You could save a version number to NSUserDefaults, and update it accordingly.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *currentAppVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleShortVersionString"];
NSString *previousVersion = [defaults objectForKey:#"appVersion"];
if (!previousVersion) {
// first launch
// ...
[defaults setObject:currentAppVersion forKey:#"appVersion"];
[defaults synchronize];
} else if ([previousVersion isEqualToString:currentAppVersion]) {
// same version
} else {
// other version
// ...
[defaults setObject:currentAppVersion forKey:#"appVersion"];
[defaults synchronize];
}
return YES;
}
I'd like to prominently show a 'help' button when my meteor app is opened for the first time (on iOS). What's the best way to achieve this? That is, how can I easily confirm my meteor app is opened for the first time, after installing the app on iOS.
set a NSUserDefaults parameter called helpShown, check setting to know which display controller to display. set to true when help is clicked / dismissed
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// set here a bool value
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"showHelp"];
//...............
return YES;
}
//somewhere you add your help button
//......
//remove/hide help button
- (void)helpFinishAction:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:#"showHelp"]) {
[defaults setBool:NO forKey:#"showHelp"];
NSLog(#"remove/hide you help button here");
//remove/hide you help button here
}
}
What is the standard way to preload an app with core data on delivert, for example i want to deliver my app with an example document which is to be available through core data after install.
So how do i package some initial data with an Core Data iOS App?
You can detect the first launch and then add the Data:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedOnce"])
{
}
else
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:YES forKey:#"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
//Add your data here in the Core Data Database.
}
return YES;
}
How can I change to another view when the app is opened again.
I was thinking of using the method ' - (void)applicationWillEnterForeground:(UIApplication *)application ' in my app delegate and performing a segue maybe? How would I go about doing this correctly?
Thanks guys!
u can use NSUserDeafult to add the status of application when its go in foreground and when application opened again means in "application didFinishLaunchingWithOptions" just check that NSUserDeafult's value and change ur view.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:#"first" forKey:#"state"];
[prefs synchronize];
use this code when application go in foreground
and when its come back just
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *state = [prefs stringForKey:#"state"];
now according to state do whatever u want