Better Programming

Advice for programmers.

Follow publication

Checking for the User’s First Launch in Swift

Eric Walker
Better Programming
Published in
5 min readOct 28, 2019
“First” on first launch; “Second+” on every launch after that

Introduction

After you conquer being able to save data, whether using iCloud or playing it safe and using UserDefaults, you need to be able to load that data. While clicking a button can be an easy way to load data, you don’t want to have your user click a button every time they want the most up to date data for your application.

Like I said above, you might assume using a button would be the easiest way to load your users’ data, but being able to detect whether the current session of the app is the first launch or not isn’t as difficult as one might think.

What Are First-Launch Checks and How Are They Useful?

Being able to check to see if the current app session is the first launch or not is pretty important for different types of apps. The most important one would be if your app requires a login. When the user launches your app for the first time, you’d rather have them go to a log-in page instead of the the home page of your app, right? Then you’d need to be able to check if it’s their first time in the app or not.

Another great time to use this is if your app requires a user to choose certain settings on the first launch, then subsequently every launch after that you need to load those settings. The easiest way to implement something like this is to use an old, trusty friend: UserDefaults. Let’s get started.

Requirements

  • Xcode 10
  • Swift 4 (and up)

I’m going to assume you have a little knowledge of how UserDefaults work, and if you don’t, check out my article on how they do. UserDefaults is one of the most useful frameworks out there, in my opinion.

Getting Started

The first thing to do, as always, is to create a new Xcode project.

We’ll select a Single View App for this tutorial (the completed project can be found at the end of this article).

Choose a Single View App for this project

Next, name it whatever you want — for this project, I will be naming it CheckForFirstTimeLaunchDemo.

Naming this CheckForFirstTimeLaunchDemo

Click next, and save it somewhere you’ll remember. Now let’s get some simple code up and running.

The Code

For this article, I chose not to do any UI work. I did this because it technically doesn’t need any — plus it’s for people to better understand you don’t need UI elements, like a button, to be able to load data when your app loads.

First we’ll head into ViewController.swift and set the cursor inside to the ViewDidLoad method. Once you’ve done that, make it look like this:

Line 5: This is just setting the variable defaults to UserDefaults.standard.

Line 10: This is what starts it all. It’s simply an if-statement that checks to see if the key “First Launch” is set to true inside of defaults. Now if it is the first launch, the key “First Launch” won’t be set to anything so the if-statement will output a false reading.

Line 21: If it is false, during the first launch of the app, it’ll come to the else portion of the if-statement. Under this line is where you should put code you want ran only during the first launch of an app. All line 21 does is print “First” to the debug area.

Line 25: Here’s where most of the magic happens: Once the app runs for the first time, “First Launch” isn’t set to anything, so it defaults to false. But after the first launch occurs, we want “First Launch” to be true, signifying the first launch already happened. That way when the if-statement in line 10 gets run again, the else portion won’t run — unless the user deleted the app and reinstalled it.

Line 12: This prints out “Second+” to the debug area, signifying the code under that is what is being ran after the first launch already occurred.

Line 16: This line is exactly the same as line 25 but in a different section of the if-statement. Technically we don’t need this, but it makes me feel better having it.

And that’s it for the code. A little tl;dr for the line explanation would be: Put whatever code you want ran on first launch only between lines 21 and 25. Put anything you want ran on every launch after that between lines 12 and 16, and you should be good to go!

Running the App

All you have to do now is run the scheme that deploys the app to the iPhone simulator (it may take a few minutes if it’s the first time running the app to the simulator).

The first time you boot up the app onto the simulator or real device, the debug area should say “First.”

After that, go into the multi-tasking mode and close out of the application.

After you’ve done that, just run the scheme in Xcode again (that way you get the debug output), and you should now see “Second+” printed.

Running the app twice to make sure everything works

Conclusion

Being able to check to see if the current app’s session is the first time it has been launched or not really isn’t that hard to accomplish. With our friend UserDefaults, you could set up your app to automatically load data every time you open it in just a few short minutes.

Being able to do this adds another layer of usability any user of your app would greatly appreciate. Now they don’t manually have to fetch their updated data. It’ll also go a long way if you’re creating an app that needs a login.

The simpler you can make a user interface, the better.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Eric Walker
Eric Walker

Written by Eric Walker

Studying Mechanical Engineering at The University of Akron. Cancer survivor since 2015.

Responses (1)

Write a response