How to make UIAlertView appear only once, the first time you launch the iPhone application?

I use UIAlertView to make a popup after starting the application. It works fine, but I want the popup to appear the first time the application starts. At the moment, I have a UIAlertView in the AppDelegate class, in the applicationDidFinishLaunching method. Here is my code:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { sleep(4); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Welcome!" message:@"SAMPLE!!!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; 

I am new to application development, so sorry if this is easy.

+6
iphone cocoa-touch xcode uialertview
source share
5 answers

To determine if the application is launching for the first time, you need to have a constant variable to store this information. NSUserDefaults are the best way to maintain these simple configuration values.

For example,

 -(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 named notFirstRun from the configuration. These values ​​are initialized to NO. Therefore, if this value is NO, we execute the if branch and display a warning.

After that, we use [defaults setBool:YES forKey:@"notFirstRun"] to change this boolean to YES, so the if branch will never be executed again (suppose the user does not delete the application).

+10
source share
 - (void) displayWelcomeScreen { NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; NSString *alreadyRun = @"already-run"; if ([prefs boolForKey:alreadyRun]) return; [prefs setBool:YES forKey:alreadyRun]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"…" message:@"…" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert show]; [alert release]; } 
+4
source share

Use NSUserDefault, which is initially set to 0 and which you set to 1 after displaying the warning for the first time.

 //default value is 0 [settings registerDefaults:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:@"HAVE_SHOWN_REMINDER"]]; BOOL haveShownReminder = [settings boolForKey:@"HAVE_SHOWN_REMINDER"]; if (!haveShownReminder) { //show your alert //set it to 1 [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:1] forKey:@"HAVE_SHOWN_REMINDER"]; } 
+3
source share

3 simple lines

 -(BOOL)application:(UIApplication *)applicationBlahBlah { if(![[[NSUserDefaults standardUserDefaults] objectForKey:@"NOT_FIRST_TIME"] boolValue]) { [[[[UIAlertView alloc] initWithTitle:nil message:@"message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] autorelease] show]; [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:@"NOT_FIRST_TIME"]; [[NSUserDefaults standardUserDefaults] synchronize]; } } 
+2
source share

As stated above, the solution should work in most cases, I would also like to inform you about it below.

An Apple document says: the NSUserDefaults class does not currently support settings for each host. You must use the CFPreferences API for this. Ref NSUserDefaults

And a discussion of this here

+1
source share

All Articles