I am following this answer to show a little intro message once the app has been started for the very first time.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
if(![[NSUserDefaults standardUserDefaults] boolForKey:#"hasSeenTutorial"]){
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"hasSeenTutorial"];
[[NSUserDefaults standardUserDefaults] synchronize];
[interface addHelpCards:nil];
NSLog(#"-First Time-");
} else {
NSLog(#"-Not First Time-");
}
...
}
Problem is ... it doesn't work. I am testing this on my device. As soon as I double tap to completely shutdown the app and then restart I get the "First Time" message again. Any idea on what's wrong with this approach? I'd appreciate your help.
if it is not working, I will eat my leg.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// ...
id _object = [[NSUserDefaults standardUserDefaults] valueForKey:#"hasSeenTutorial"];
if (_object == nil) {
[[NSUserDefaults standardUserDefaults] setValue:#(TRUE) forKey:#"hasSeenTutorial"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"-First Time-");
} else {
NSLog(#"-Not First Time-");
}
// ...
}
You're only setting YES for hasSeenTutorial if hasSeenTutorial is already YES (it's in the else). Just move that (and the synchronize) up to the first part of the if
you should be setting the Bool value in the if and not in the else. Right now the bool value will never be set
Related
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
}
}
If user is opening application for the first time after downloading it, then I want to show a view controller with tutorial and then go to main application's View Controller.
But if user is already visited app before, then go to Main view controller.
How to implement that check in AppDelegate?
add in your app delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[NSUserDefaults standardUserDefaults]objectForKey:#"firsttime"]) {
[[NSUserDefaults standardUserDefaults]setObject:#"YES" forKey:#"firsttime"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
}
If condition will check for the key is available in app or not.
If it is not available then it will save it to YES.
You can do as you want.
I would do that in your main view controller in method viewWillAppear. You can use NSUserDefaults. Here's an example:
Swift:
func isAppAlreadyLaunchedOnce() {
let defaults = NSUserDefaults.standardUserDefaults()
if let isAppAlreadyLaunchedOnce = defaults.stringForKey("isAppAlreadyLaunchedOnce"){
println("App already launched")
return true
} else {
defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce")
println("App launched first time")
//Show your tutorial.
return false
}
}
Objective-C:
- (void)isAppAlreadyLaunchedOnce {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *isAppAlreadyLaunchedOnce = [defaults stringForKey:#"isAppAlreadyLaunchedOnce"];
if (isAppAlreadyLaunchedOnce != nil) {
NSLog(#"App already launched");
return true;
} else {
NSLog(#"App launched first time");
[defaults setBool:true forKey:#"isAppAlreadyLaunchedOnce"];
//Show your tutorial.
return false;
}
}
And then run this method in your viewWillAppear:
Swift:
func viewWillAppear(animated: Bool) {
isAppAlreadyLaunchedOnce()
}
Objective-C:
- (void)viewWillAppear:(BOOL)animated {
[self isAppAlreadyLaunchedOnce];
}
NSUserDefaults will be cleared when user will uninstall app.
Add a flag in NSUserDefaults. When the user launches the app for the first time, the flag is not set. After checking the status of the flag, if the flag is not set the flag should be set.
you need to use boolForKey and setBool
if (![[NSUserDefaults standardUserDefaults]boolForKey:#"firsttime"]) {
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:#"firsttime"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
Here I can use NSUserDefaults, but I need to remove this NSUserDefaults.
In my app I'm using NSUserDefaults for getting locations one View to anotherView but here issue when I'm closed to the my app, and again I started still the locations are there, here I used for removing locations in AppDeligate
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults]removeObjectForKey:#"Location"];
}
But still locations is there.
How can I removed this?
Can you suggest me.
change your method
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults]removeObjectForKey:#"Location"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
into
- (void)applicationWillTerminate:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults]removeObjectForKey:#"Location"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
whenever you remove or add values in NSUserDefaults don't forget to call [[NSUserDefaults standardUserDefaults] synchronize];, if your fetching the values on that time no need to add [[NSUserDefaults standardUserDefaults] synchronize];
I want to open a ViewController only once when the app start for the very first time. Below is my code. The problem is when I am writing YES to NSUserdefaults , doing a synchronise , and then closing the app. in Xcode using simulator , the value is not updated to Yes.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"isFirstLaunch"])
{
// app already launched
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO],#"isFirstLaunch", nil]];
[[NSUserDefaults standardUserDefaults]synchronize];
}
else
{
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],#"isFirstLaunch", nil]];
[[NSUserDefaults standardUserDefaults]synchronize];
}
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"isFirstLaunch"])
{
NSLog(#"first time");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LASplashUserSettingViewController *splash = [storyboard instantiateViewControllerWithIdentifier:#"splash"];
[self.window.rootViewController presentViewController:splash animated:NO completion:nil];//
}
}
Why is NSUserDefaults not saving my values?
Have followed above and many other links. what am i missing here?
Thanks,
what am i missing here?
-registerDefaults: doesn't do what you think it does. Use -setBool:forKey: instead.
-registerDefaults: is used to ensure that values exist for certain keys in the defaults system. There's no need to call -synchronize after that method because the values in the registration domain are never saved. You probably meant to use that method to set up the default value YES for isFirstLaunch, but your code checks the value for the key before you register the default value! which defeats the purpose of the method. Proper use would be to call -registerDefaults: first, and then check the value associated with the key, and if the value is YES then call -setBool:forKey: to save the value NO.
A simpler option to detect the first launch would be to invert the question. Skip the -registerDefaults: call altogether and change the key to appHasRunPreviously. If no value is stored for a given key, -boolForKey: will return NO. If that happens, you know that this is the first time the app is running, and you should save YES for that key using -setBool:forKey:.
This is how you use NSUserDefaults in AppDelegate.m
+ (void)initialize
{
// Register defaults for first time
NSMutableDictionary *defs = [NSMutableDictionary dictionary];
[defs setObject:[NSNumber numberWithBool:YES] forKey:#"isFirstLaunch"];
[[NSUserDefaults standardUserDefaults] registerDefaults:defs];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults boolForKey:#"isFirstLaunch"])
{
// Do your stuff here
[defaults setBool:NO forKey:#"isFirstLaunch"];
[defaults synchronize];
}
}
Don't set isFirstLaunch back to YES in the else-condition if you only want the view to be shown once.
You can try this
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:#"AlreadyLaunched"];
[[NSUserDefaults standardUserDefaults]boolForKey:#"AlreadyLaunched"]
I'm trying to make my app launch a different view on the first time it is loaded up. I've got this code at the moment which implements that something should happen when the app is first launched. I've got this code but it lacks the code to open Initialviewviewcontroller. I have no idea how to do this so help would be much appreciated
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL hasRunBefore = [defaults boolForKey:#"FirstRun"];
if (!hasRunBefore) {
[defaults setBool:YES forKey:#"FirstRun"];
[defaults synchronize];
// what goes here??
else
{
NSLog (#"Not the first time this controller has been loaded");
So I should launch a different view controller in the if statement. But what should I put ?
Solution No. 1
I've written a simple snippet for this thing because I use it quite a lot. You can find it here.
Feel free to use it, fork it or modify it!
Solution No. 2
You can do something like this in your AppDelelegate.m
Add this simple method at the bottom:
- (BOOL)hasEverBeenLaunched
{
// A boolean which determines if app has eer been launched
BOOL hasBeenLaunched;
// Testig if application has launched before and if it has to show the home-login screen to login
// to social networks (facebook, Twitter)
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasAlreadyLaunched"]) {
// Setting variable to YES because app has been launched before
hasBeenLaunched = YES;
// NSLog(#"App has been already launched");
} else {
// Setting variable to NO because app hasn't been launched before
hasBeenLaunched = NO;
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasAlreadyLaunched"];
[[NSUserDefaults standardUserDefaults] synchronize];
// NSLog(#"This is the first run ever...");
}
return hasBeenLaunched;
}
After implementation of this method you can use it like that:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Determining Storyboard identifier for first view
NSString *storyboardID = [self hasEverBeenLaunched]? #"MainView" : #"LoginView";
// Setting proper view as a rootViewController
self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:storyboardID];
return YES;
}