How do you call a method on only the initial launch of an application? For example, if you wanted to put a tutorial UIAlertView on the very first launch but never again after that?
A:
Use the NSUserDefaults for this. Store a BOOL there saying if its firstlaunch, and set it in applicationDidFinishLaunching. Then then only time that is false is after fresh installs of the app.
Joshua Weinberg
2010-08-09 19:40:15
+2
A:
Easy. When your app launches, check [NSUserDefaults standardUserDefaults]
for the presence of a bool that you put in there. If the boolean isn't there (or it's not YES), then show the alert and save the boolean back into NSUserDefaults
as YES
.
It's all of about 4 lines of code:
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasShownInitialAlert"] == NO) {
UIAlertView * alert = [[UIAlertView alloc] init...];
[alert show];
[alert release];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasShownInitialAlert"];
}
Dave DeLong
2010-08-09 19:41:30