1

A button triggers an alert; the alert is "one-time", appearing once but never appearing again after user hits "OK".

If the Boolean is 0, the alert is triggered; if it is 1, then the alert is not triggered. If the user hits "OK", the value of the BOOL is set to 1.

Which is the best way to set a one-time alert in Objective-C?

Eric
  • 893
  • 10
  • 25
  • 2
    The technique you have described seems OK. What are you specifically having a problem with? Can you show any code for your attempt? – Paulw11 Oct 30 '15 at 21:03

1 Answers1

2

I would use NSUserDefaults to store the boolean flag you're talking about. Like so:

static NSString * const AlertHasBeenShownUserDefaultsKey = @"AlertHasBeenShownUserDefaultsKey";

-(void)showAlert {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if (![userDefaults boolForKey:AlertHasBeenShownUserDefaultsKey]) {
        //Show an alert

        [userDefaults setBool:YES forKey:AlertHasBeenShownUserDefaultsKey];
    }
}

NSUserDefaults will keep the bool value across launches. The value will be reset if the user reinstalls the app though.

FreeNickname
  • 7,398
  • 2
  • 30
  • 60
  • Thanks! Do I write [self showAlert]; in viewDidLoad? Where do I put the static constant? – Eric Oct 30 '15 at 21:08
  • @Eric, it is just an example code. You can call it wherever it is appropriate in your case. If you want to show the alert as soon as some `ViewController` appears on screen, you could call this method (or just use the code from this method) inside `viewDidAppear`, for instance. I suggest `viewDidAppear`, because it is called, when the ViewController is already visible, not just loaded. It can be called multiple times though. But our flag in `NSUserDefaults` will take care of showing an alert just once. – FreeNickname Oct 30 '15 at 21:13
  • You're right. I figured out where to call the alert. Do you know where I would write the static constant in a ViewController.m/.h file? Should I add the static constant to the class or just declare the variable under @implementation? – Eric Oct 30 '15 at 21:18
  • @Eric, in fact, you could just use `@"AlertHasBeenShownUserDefaultsKey"` everywhere instead of the static constant. Constants are useful because they have a meaningful name and because they save you from typos. So it is just for the sake of good code. `static` means that it is visible only inside the file it is declared in (take a look [here](http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program)). So, you declare it in the `*.m` file. I like to keep my constants right after `@implementation`, but it is not a requirement (the link above should clarify the question). – FreeNickname Oct 30 '15 at 21:25
  • @Eric, also, you could declare it as a static constant inside the function, since it is not used anywhere else. In fact, it might be a better idea, than my original code, because this way this constant won't get in your way in other parts of this file. I declared it as a global constant, because usually such `UserDefaults` keys are used in several methods, so I'm used to declaring them as global. – FreeNickname Oct 30 '15 at 21:28
  • I think that declaring it as a local variable is the cleanest. I will use the code only for a single alert that will appear once after a user clicks it but never again (unless, of course, the user re-installs the app). – Eric Oct 30 '15 at 21:48
  • Do you know if the NSUserDefaults work in the simulator? – Eric Oct 30 '15 at 21:56
  • @Eric, yes, it (they?) does (do?). – FreeNickname Oct 30 '15 at 22:03
  • I tested it in a device. I had to dispatch the alert on the main thread but it worked perfectly for what I wanted. Can you upvote the question? – Eric Oct 30 '15 at 22:25
  • @Eric, sorry, I only upvote questions that helped me. – FreeNickname Oct 31 '15 at 09:21