简体   繁体   中英

Show UIAlertview after 3 launches App

What I'm trying to achieve is to show a UIAlertview when a user opens the app after 3 times. I use the code below in my ViewDidAppear's ViewController, but it shows the UIAlertview everytime when opening the app. Can someone tell me what I'm doing wrong here?

int launches = [[NSUserDefaults standardUserDefaults] integerForKey:@"launchCount"];
if (launches > 3) {
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Alert" 
                                                  message:@"Some message" delegate:nil
                                        cancelButtonTitle:@"OK" otherButtonTitles:nil];
  [alert show];
}
[[NSUserDefaults standardUserDefaults] setInteger:launches+1 forKey:@"launchCount"];

Edit: I'm also getting a NSInteger (aka 'long') to 'int' warning. Could this be the issue why it's not working?

在此处输入图片说明

您应该将该代码移动到-application:didFinishLaunchingWithOptions:应用程序委托中-application:didFinishLaunchingWithOptions:

@AdamPro13 is right that you should move the code but probably to - (void)applicationDidBecomeActive:(UIApplication *)application in your app delegate. The viewDidAppear method can be called several times for each app launch. If you want to show the UIAlertView only once per installation you could either save a BOOL if it has been shown or change the test launches == 4 . Something like

NSInteger launches = [[NSUserDefaults standardUserDefaults] integerForKey:@"launchCount"];
BOOL launchAlertShown = [[NSUserDefaults standardUserDefaults] boolForKey:@"launchAlertShown"];
if (launches > 3 && !launchAlertShown) {
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Alert" 
                                              message:@"Some message" delegate:nil
                                    cancelButtonTitle:@"OK" otherButtonTitles:nil];
  [alert show];
  [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"launchAlertShown"];
}
[[NSUserDefaults standardUserDefaults] setInteger:launches+1 forKey:@"launchCount"];

You have delete the app, not just overwrite with a new build, to delete the contents of [NSUserDefaults standardUserDefaults] . This means that the test launches > 3 will remain true until you have deleted the app from the device or simulator that you are testing on.,

Got it! Thanks to @AdamPro13

NSInteger launches = [[NSUserDefaults standardUserDefaults] integerForKey:@"launchCount"];
    if (launches % 3 == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Alert"
                                                        message:@"Some message" delegate:nil
                                              cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }

    [[NSUserDefaults standardUserDefaults] setInteger:launches+1 forKey:@"launchCount"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM