Rob
Rob

Reputation: 15982

How to print an alert view only once in my app?

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.

Upvotes: 1

Views: 902

Answers (4)

Ankit Jain
Ankit Jain

Reputation: 1886

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![[defaults valueForKey:@"SHOW_ONLY_ONCE"] isEqualToString:@"1"])
{
    [defaults setValue:@"1" forKey:@"SHOW_ONLY_ONCE"];
    [defaults synchronize];
}

Upvotes: 0

Adrian P
Adrian P

Reputation: 6529

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).

Upvotes: 1

charleyh
charleyh

Reputation: 2053

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.

Upvotes: 4

Manish Agrawal
Manish Agrawal

Reputation: 11026

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.

Upvotes: 1

Related Questions