How do you get an alert to pop up ONLY the first time someone clicks on something?

Discussion in 'iOS Development' started by bamhm182, May 27, 2009.

  1. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    I've got something like this right now:

    Code:
    - (void)viewDidLoad
    {
    	UIAlertView *alert = [[UIAlertView alloc]
    						  initWithTitle:@"Welcome to Game Mode 1!"
    						  message:@"This is what you do in this mode."
    						  delegate:self
    						  cancelButtonTitle:@"Alrighty"
    						  otherButtonTitles:nil];
    	[alert show];
    	[alert release];
    }
    
    I want it to only show up the first time someone opens Game Mode 1 after installing it to their device, how can I do that?
  2. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Look into the NSUserDefaults class, then save a bool YES to say a key "FirstOpened", then at start up see what the bool is if it is NO continue to show it, if it is YES continue to without showing it.
  3. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    Thanks for the tip chicken. I'll look into it.
  4. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Code:
    - (void)showAlertIfNecessary {
            if(![[NSUserDefaults standardUserDefaults] boolForKey:@"FirstLaunchAlert"]) {
                    // Create and show alert
                    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstLaunchAlert"];
                    [[NSUserDefaults standardUserDefaults] synchronize];
            }
    }
    }
  5. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    Thanks johnnyboi. Saved me some time. I appreciate it. Everyone here is so helpful.

    Please Register or Log in to view images



    EDIT: I was looking into the NSUserDefaults, and I noticed that some of them have a + in front of them? What's the difference between a + and a -?

    Example:
    Code:
    - (void)viewDidLoad
    
    vs.
    
    + (void)resetStandardUserDefaults
    
  6. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Your question has already been explained in this forum. ALso, gjb, you don't need to synchronize the defaults right away. They write on a timer, and if your app doesn't make it to that first timer firing before crashing, then you probably want to show the alert again. Remember, this is a platform where we want to save as many cpu cycles as possible, and adding an extra "write this file to disk" operation is a great way to lose quite a few cycles.
  7. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    Sorry, I guess I forgot to search this time. I was exhausted earlier, normally that's the first thing I do.

    Also, I just stuck it in the view did load, seems to work just fine though. I think you just need to put it somewhere that gets loaded instantly, right?

    Now, if I wanted to have an option in the settings menu to reset the messages, would I just import the .h into the options .m, than put something like:

    Code:
    - (IBAction)resetButtonPressed:(id)sender;
    {
    		[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"AlreadyRan"];
    }
    

    EDIT: I tried it, and it turns out that that works. So in theory, if I wanted to set multiple game types, than I should have something like "GameType1AlreadyRan" "GameType2AlreadyRan", etc. Than I could just stick them in the resetButtonPressed action and it'd reset those as well.

    Also, I just took out the #import, I'm guessing that it is best to import as few files as possible, right? Also, to add for a better user experience, I added an alert to let the user know that all the alerts were reset.
  8. jbonedev

    jbonedev New Member

    Joined:
    May 24, 2009
    Messages:
    44
    Likes Received:
    0
    Device:
    iPhone 3G (Black)

    The + indicates a class level method. E.g.
    Code:
    [MyClass myClassMethod];
    
    The - indicates an instance level method.
    Code:
    MyClass* instanceOfMyClass = [[MyClass alloc] init];
    [instanceOfMyClass myInstanceMethod];
    
  9. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Oh my! This thread has helped me so much. My app (thread available in Theme WIP thread) has advanced quite a bit with this! Thanks GJB and bamhm182!
  10. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    No problem, glad I can help.

    Please Register or Log in to view images

    Booleans are VERY useful.

Share This Page