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
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
maybe someone could assist me.
When my app is launched for the first time a UIAlertController message appears and asks the user if they Need to GoTo the Settings App. using the following code.
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"prefs:root=Wi-Fi"]];
});
Upon return to the App the same UIAlertController Message appears, using this code in the ViewDidLoad:
-(void)viewDidLoad {
if (launched == NO) {
launched = YES;
defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:launched forKey:#"boolKey"];
[defaults synchronize];
My code for the UIAlertController.
} else if (launched == YES) {
[self doSomeThing];
}
}
It appears my Bool Value is not being saved when the settings in the info.plist Application does not run in background: is set to YES, but if the Application does not run in background: is set to NO the else statement is executed. However this is not good because my app is suspended and when launched again I need the original message to appear and it does not, the app is restored to its last state.
Any Suggestions is greatly appreciated.
JZ
1.- Saving into NSUserDefaults does not save anything to the info.plist. NSUserDefaults has its own plist that gets wiped out if you remove your application.
If you want to prevent to launch the same alertview:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults boolForKey:#"boolKey"]) {
defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"boolKey"];
My code for the UIAlertController.
}
else {
//Whatever you need to do if its not first launch
}
Now next time you hit your viewDidLoad, since the boolForKey:#"boolKey" has a YES, you won't hit that code and the alertView won't get presented.
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
}
}
I want to display a message to users if there is a new version of the app they're using, and to display this message at the app's startup, only once.
I've managed to get the app final version and the bundle's one, and comparing them to display or not an alert view to propose the new app. Now I just want to know how to never display this alert again when it has been done once.
Thanks for your help.
To check if you need to display the UIAlertView, store a variable in NSUserDefaults. For instance, in appDidFinishLaunching you could do:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:#"alertShown"]) {
// display alert...
[defaults setBool:YES forKey:#"alertShown"];
}
In the display alert, just have the code for the alert to pop up.
save the version number in user defaults and check the version with app version, if both are same then continue otherwise show the alert and update the user default variable to latest version. First time leave the user default blank string.
Here is an idea of how to do it.
-(BOOL)application:(UIApplication *)application … {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:#"notFirstRun"]) {
// display alert...
[defaults setBool:YES forKey:#"notFirstRun"];
}
// rest of initialization ...
}
Here, [defaults boolForKey:#"notFirstRun"] reads a boolean value named notFirstRun
from the config. These values are initialized to NO. So if this value is NO, we execute the if branch and display the alert.
After it's done, we use [defaults setBool:YES forKey:#"notFirstRun"] to change this boolean value to YES, so the if branch will never be executed again (assume the user doesn't delete the app).
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![[defaults valueForKey:#"SHOW_ONLY_ONCE"] isEqualToString:#"1"])
{
[defaults setValue:#"1" forKey:#"SHOW_ONLY_ONCE"];
[defaults synchronize];
}
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:.