0

我正在创建一个将在用户首次启动应用程序时显示的免责声明。免责声明是带有 2 个选项的 alertView。如果用户同意,则将显示 firstViewController。如果他不这样做,他将被重定向到另一个 viewController。但如果用户第一次同意,我无法让免责声明消失。每次应用启动时都会显示。任何帮助,将不胜感激。先感谢您..

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];
   }
 }
4

2 回答 2

2
NSMutableDictionary* defaultValues = [NSMutableDictionary dictionary]; 
[defaultValues setValue:...forKey:...]
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];

如果未设置默认值(第一次),这将注册您的默认值

此外,似乎您[defaults synchronize]在设置值后忘记提交更改。如果是这样,您根本不需要该registerDefaults方法。

像这样:

if ([buttonString isEqualToString:@"Yes Let me In"]) {
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    [defaults setValue:@"accepted"forKey:@"keyDisclaimer"];
    [defaults synchronize];
}
于 2012-10-05T09:29:33.380 回答
0

您想使用类似 stringForKey 或 objectForKey 的东西而不是 valueForKey。

if([[defaults stringForKey:@"keyDisclaimer"] isEqualToString:@"accepted"]) {

过去我在 valueForKey 方面的经历很糟糕,并不总是按我想要的方式工作。这可能是导致您出现一些问题的原因。

于 2012-10-05T10:08:07.747 回答