I’m wanting the user to accept an agreement before the application is launched. So in the appDelegate.m I have the following:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
defaults = [NSUserDefaults standardUserDefaults];
if(![defaults boolForKey:@"warningAccepted"]){
UIAlertView *alert;
alert = [[[UIAlertView alloc] initWithTitle:@"Warning" message:@"Message... To continue, select \"OK\". To close the app, select \"Cancel\"." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil] autorelease];
[alert show];
}
return YES;
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0){
[defaults setBool:YES forKey:@"warningAccepted"];
[defaults synchronize];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[window addSubview:mainNavController.view];
} else {
[window addSubview:tabBarController.view];
}
[window makeKeyAndVisible];
} else {
// Close App, User hit cancel.
UIAlertView *alert;
alert = [[[UIAlertView alloc] initWithTitle:@"App Cannot Continue" message:@"The application cannot run until warning is accepted." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease];
[alert show];
}
}
One problem is that the else never gets called when the user hits cancel. The other problem is that I’m not sure how to stop the app from continuing. From what I have found, Apple does not want you to force the application to close. Is that correct? How should I go about implementing this? Thanks for all of your help.
Yes, Apple will reject your application if you force to exit. I can tell 😉 Another UIAlertView saying that the user cannot continue without accepting should de the trick (Just don’t put any button on it so the user can’t dismiss it)
About the else scope, does it not pass through it, or the UIAlertView doesn’t show ? Check it with a breakpoint. Further more, isn’t when
(buttonIndex == 0)means that the user touched the “Cancel” button