One time UIAlert?
I want to do a UIalert the first time a user opens my app, to tell them about basic functionality, but I don't want it sho开发者_C百科wing up every time they open the app or reload the home screen. I've seen this in other apps but don't know what the best way of going about it would be. If anyone could point me in the right direction on this it would be appreciated.
Stick this into you App Delegate in the - (void)applicationDidFinishLaunching:(UIApplication )application method
BOOL hasRunBefore = [[NSUserDefaults standardUserDefaults] boolForKey:@"FirstRun"];
if (!hasRunBefore) {
UIAlertView *firstRun = [[UIAlertView alloc] initWithTitle:@"Virgin" message:@"Hello World!!" delegate:nil cancelButtonTitle:@"Done" otherButtonTitles:nil];
[firstRun show];
[firstRun release];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstRun"];
}
else if (hasRunBefore) {
//can do some else if run before
}
Basically what this does is check for the key "FirstRun" in NSUserDefaults, similarly this could be modified to be a version number or something, say if you wanted a new alert after each update or something. It then checks if the key does not else (!hasRunBefore) and if it does not exist (so never been run before) it creates a UIAlertView and then sets YES for the key (also creating that ket in process)
Use "[NSUserDefaults standardUserDefaults]" to write a boolean if they've started the app before or not.
I use NSUserDefaults in the viewDidLoad method of my main screen. So every time the app starts and the main screen loads, it checks if its the user's first time.
This is how I do it in my app:
- (void)viewDidLoad {
BOOL tempBOOL = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasSeenOpeningAlert"];
if (!tempBOOL) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Welcome To My App" message:@"This app will ... First you need to ..." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
[super viewDidLoad];
}
and then:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
Edit *editViewController = [[[Edit alloc] initWithNibName:@"Edit" bundle:nil]retain];
[self.navigationController presentModalViewController:editViewController animated:YES];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasSeenOpeningAlert"];
[[NSUserDefaults standardUserDefaults] synchronize];
[editViewController release];
}
精彩评论