I am creating a Disclaimer that will be shown when the user first launches the app. The disclaimer is an alertView with 2 option. If user agrees then firstViewController will display. If he doesn’t he will be redirected to another viewController. But I can’t get the disclaimer to disappear if the user agrees the first time. It is shown every time the app launches. Any help would be appreciated. Thank you in advance..
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![[defaults valueForKey:@"keyDisclaimer"] isEqualToString:@"accepted"]) {
UIAlertView *disclaimer = [[UIAlertView alloc] initWithTitle:@"Read Before use" message:@"By using this app you agree to its terms and conditions.\n\n\n\n\n\n\n\n\n\n\ntext heren\n\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:@"No!" otherButtonTitles:@"Yes Let me In", nil];
[disclaimer show];
}
// Override point for customization after application launch.
return YES;
}
-(void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *buttonString = {[alertView buttonTitleAtIndex:buttonIndex]};
if ([buttonString isEqualToString:@"Yes Let me In"]) {
NSMutableDictionary* defaultValues = [NSMutableDictionary dictionary];
[defaultValues setValue:@"accepted"forKey:@"keyDisclaimer"];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
}
else if ([buttonString isEqualToString:@"No!"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:@"You are not allowed to use this app due to the fact that you did not agree to the terms and Conditions. Please exit this app!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
// [[NSUserDefaults standardUserDefaults] setValue:@"notAccepted" forKey:@"keyDisclaimer"];
}
if ([buttonString isEqualToString:@"OK"]) {
introViewController *intro = [[introViewController alloc] initWithNibName:@"introViewController" bundle:nil];
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_window.rootViewController = intro;
[_window makeKeyAndVisible];
}
}
This will register your defaults if they are not set (for the first time)
Also, seems like you’re forgetting
[defaults synchronize]to commit changes after setting values. If so, you don’t need theregisterDefaultsmethod at all.Like this: