RaistQian
RaistQian

Reputation: 89

How to determine that user runs the app for the first time?

I got a problem of my iOS app recently. In my app, an instruction view will appear at the first time of running, then hide from then on. How can I implement this effect?

Upvotes: 6

Views: 4387

Answers (4)

CReaTuS
CReaTuS

Reputation: 2585

Try to use this function:

- (BOOL) isFirstRun
{
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  
  if ([defaults objectForKey:@"isFirstRun"])
  {
    return NO;
  }

  [defaults setObject:[NSDate date] forKey:@"isFirstRun"];
  [[NSUserDefaults standardUserDefaults] synchronize];  

  return YES;
}

Upvotes: 20

Siam
Siam

Reputation: 109

The only way I think is to store a value into a specified file, when run the app, you should check the value first, and then you can deal with the result whether the app has already been run.

Upvotes: 0

codetiger
codetiger

Reputation: 2779

Store a file and check if the file exsists every time when you start the app. If tr file does not exsists then show intro, and then create the file.

Upvotes: 1

DrummerB
DrummerB

Reputation: 40221

In your app delegate check for a key in the user defaults (your own custom key, something like "AppWasAlreadyStartedPreviously"). If the key doesn't exist yet, it's the first run. You can show your instruction view and add the key to the user defaults. The next time the user starts the app you'll find the key in the user defaults and know that it's not the first run.

See the documentation of NSUserDefaults.

Upvotes: 5

Related Questions