views:

88

answers:

3

How can I make my application display something only when they first launch the application for the first time. Example: They open up my app, an alert comes up, saying something like, "Do you want to play the tutorial?" Then, if they close the app, then re-open it, it won't show up again.

Thanks

A: 

You could store in your property store a boolean value saying whether it's the first time or not, then check that on application start.

John Boker
+1  A: 

More tips on storing data persistently:

Method 1: Use the global user preferences system. You can do this, but it might be considered slightly hacky because it is designed to store user preferences, and I think this is a gray area, since the user doesn't have explicit control here. In any case, check out the docs for NSUserDefaults to find out how to do that.

Method 2: Write to a file whose existence indicates whether or not the tutorial has been viewed. You can easily create a file with an NSData object by calling its writeToFile:atomically: method. Later, you can use the NSFileManager class to check if that file exists or not.

Sample code:

- (NSString*) filename {
  NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                       NSUserDomainMask, YES);
  NSString* documentsDirectory = [paths objectAtIndex:0];
  return [documentsDirectory stringByAppendingPathComponent:@"notFirstTime"];
}

- (void) setNotFirstTime {
  NSData* data = [[[NSData alloc] init] autorelease];
  [data writeToFile:[self filename] atomically:YES];
}

- (BOOL) isNotFirstTime {
  return [[NSFileManager defaultManager] fileExistsAtPath:[self filename]];
}
Tyler
+1  A: 

I'd recommend using NSUserDefaults:

- (void)openOneTime
{
    NSUserDefaults*        defaults = [NSUserDefaults standardUserDefaults];
    static const NSString* kKey = @"One Time Key";
    NSObject*              keyValue = [defaults  objectForKey:kKey];

    if (keyValue == nil)
    {
        [self doMyOneTimeThing]; // pop a dialog, etc...
    }

    // Adds an object for our key which will get found the next time around,
    // bypassing the above code block. The type and value of the object is
    // not important; what matters more is that an object exists for that
    // key at all.

    [defaults setBool:YES forKey:kKey];
}
fbrereto