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;
}
Related
I'm coding an iOS app in Objective C. I want the terms and conditions to appear when a user opens the app. I'm going to have a "don't show these again" button, which is all figured out using NSUserDefaults. What I can't figure out is how to make the terms and conditions disappear if the user hits "agree" (termsPressed) but NOT "don't show these again" (neverAgainPressed). I can hide them using button.hidden, but then as soon as the user returns to the main screen of the app the terms and conditions appear again, overlaying the main screen just as they did when the app first launched.
I've tried setting an NSUserDefault when "agree" is pressed, then resetting it when -applicationWillTerminate is called, but it appears that applicationWillTerminate is not called reliably when the app closes if it's closed from the background, and hence the user would never see the terms again even if they hadn't hit "don't show these again". Here's my code:
- (void)viewDidLoad {
// Hide all the terms and conditions elements if either "neverAgain" or "termsPressed" is true
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL neverAgain = [defaults boolForKey:#"neverAgain"];
if (neverAgain == TRUE){
_terms.hidden=YES;
_hideTerms.hidden=YES;
_background.hidden=YES;
_dontShow.hidden=YES;
}
BOOL termsPressed = [defaults boolForKey:#"termsPressed"];
if (termsPressed == TRUE){
_terms.hidden=YES;
_hideTerms.hidden=YES;
_background.hidden=YES;
_dontShow.hidden=YES;
}
NSLog(#"View loaded. termsPressed = %i", termsPressed);
NSLog(#"View loaded. neverAgain = %i", neverAgain);
[super viewDidLoad];
}
- (IBAction)neverAgainPressed:(id)sender {
BOOL neverAgain = TRUE;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:neverAgain forKey:#"neverAgain"];
[defaults synchronize];
NSLog(#"Don't show terms again pressed, neverAgain set to TRUE");
}
- (IBAction)termsPressed:(id)sender {
_terms.hidden = YES;
_hideTerms.hidden = YES;
_background.hidden = YES;
_dontShow.hidden = YES;
BOOL termsPressed = TRUE;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:termsPressed forKey:#"termsPressed"];
[defaults synchronize];
NSLog(#"Terms accepted, termsPressed set to TRUE");
}
- (void)applicationWillTerminate:(UIApplication *)application {
BOOL termsPressed = FALSE;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:termsPressed forKey:#"termsPressed"];
[defaults synchronize];
NSLog(#"Application entering background, termsPressed reset to FALSE");
}
#end
If there was a method that was called every time an app quit, or a version of .hidden that lasted the whole time an app was running, I'd be all set. Let me know if you guys have any ideas.
If you want "don't show these again", why reset? You can show at first launch and after some time (30 days for example):
BOOL ranBefore = [[NSUserDefaults standardUserDefaults] boolForKey:#"kRanBefore"];
NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:#"kLastCloseDate"];
NSTimeInterval timeDiff = [[NSDate date] timeIntervalSinceDate:lastDate];
int days = timeDiff / 86400;
if ((!ranBefore) || (days > 30)) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"kRanBefore"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:#"kLastCloseDate"];
... show something
}
or reser at
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
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
}
}
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"]
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;
}
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;
}