Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xcode

ios

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?

like image 970
RaistQian Avatar asked Jun 28 '12 01:06

RaistQian


People also ask

How do I know if an app is running for the first time?

There's no reliable way to detect first run, as the shared preferences way is not always safe, the user can delete the shared preferences data from the settings! a better way is to use the answers here Is there a unique Android device ID? to get the device's unique ID and store it somewhere in your server, so whenever ...

How do I find my first app launch Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken textview, when user open application, it will check whether it is the first time or not.

How do you start an activity only once the app is opened for the first time?

Now in the onResume() method, check if we already have the value of prevStarted in the SharedPreferences. Since it will evaluate to false when the app is launched for the first time, start the MainActivity (Which we want to appear just once) and set the value of prevStarted in the SharedPreferences.


2 Answers

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;
}
like image 58
CReaTuS Avatar answered Oct 06 '22 01:10

CReaTuS


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.

like image 26
DrummerB Avatar answered Oct 06 '22 01:10

DrummerB