Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lynkynpark86

macrumors 6502
Original poster
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.
 
The following code goes into your app delegate dot-m file

Code:
//
//  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");
	else
		alert(@"Welcome", @"This app is being run for the first time.");

	[window makeKeyAndVisible];
	
	return YES;
}


- (void)dealloc {
    [window release];
    [super dealloc];
}


@end
 
Why would you use doubleForKey: to read a BOOL?

Code:
BOOL ranBefore = [defaults doubleForKey:  @"ranBefore"];
 
Why would you use doubleForKey: to read a BOOL?

That's a typo.

Here's what I meant.

Instead of:

Code:
	BOOL ranBefore = [defaults doubleForKey:@"ranBefore"];

I meant:

Code:
	BOOL ranBefore = [defaults boolForKey:@"ranBefore"];

It seems to work either way though.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.