Check if user has run the app before? Please help!

Discussion in 'iOS Programming' started by lynkynpark86, May 2, 2010.

  1. lynkynpark86 macrumors 6502

    I am making an app, and I want to show an alert the first time the user runs the app only. If anyone knows how to do that, help would be greatly appreciated. PLEASE, don't say "Check your NSSomething", etc. I am a complete beginner. I just learned how to do a UIAlertView today. Either code, or a link to code, would be very helpful. Thanks.
  2. kaydell.leavitt macrumors regular

    Apr 19, 2010
    The following code goes into your app delegate dot-m file

    //  DidRunBeforeAppDelegate.m
    //  DidRunBefore
    //  Created by kaydell on 5/2/10.
    #import "DidRunBeforeAppDelegate.h"
    @implementation DidRunBeforeAppDelegate
    @synthesize window;
    // the function alert() displays an alert to the user
    void alert(NSString *title, NSString *message) {
    	UIAlertView *alert = [[UIAlertView alloc]
    						  initWithTitle: title
    						  message: message
    						  delegate: nil
    						  cancelButtonTitle: @"OK"
    						  otherButtonTitles: nil
    	[alert autorelease];
    	[alert show];
    // Override point for customization after application launch
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    	// get the standard user default object
    	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    	// read the flag to see if the application was ran before
    	BOOL ranBefore = [defaults doubleForKey:  @"ranBefore"];
    	// set a flag that this application has been run before
    	[defaults setBool:YES forKey:@"ranBefore"]; // set a flag in secondary storage
    	[defaults synchronize]; // save to disk
    	// do processing depending upon whether the app has be run before or not
    	if (ranBefore)
    		alert(@"Welcome Back", @"This app has been run before");
    		alert(@"Welcome", @"This app is being run for the first time.");
    	[window makeKeyAndVisible];
    	return YES;
    - (void)dealloc {
        [window release];
        [super dealloc];
  3. lynkynpark86 thread starter macrumors 6502

    Thank You

    That is perfect! Thank you so much! :D
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Why would you use doubleForKey: to read a BOOL?

    BOOL ranBefore = [defaults doubleForKey:  @"ranBefore"];
  5. kaydell.leavitt macrumors regular

    Apr 19, 2010
    That's a typo.

    Here's what I meant.

    Instead of:

    	BOOL ranBefore = [defaults doubleForKey:@"ranBefore"];
    I meant:

    	BOOL ranBefore = [defaults boolForKey:@"ranBefore"];
    It seems to work either way though.

Share This Page