Swift Only Load Again if Changed
Checking for the User's First Launch in Swift
Running specific actions at kickoff launch and different ones at every launch after that
Introduction
After yous conquer being able to save data, whether using iCloud
or playing information technology safe and using UserDefaults
, you lot demand to be able to load that data. While clicking a push button tin be an piece of cake manner to load data, y'all don't want to have your user click a button every fourth dimension they desire the well-nigh up to appointment data for your awarding.
Similar I said higher up, you might assume using a button would be the easiest way to load your users' information, just being able to detect whether the current session of the app is the get-go launch or not isn't as hard equally ane might think.
What Are Offset-Launch Checks and How Are They Useful?
Being able to check to encounter if the current app session is the first launch or not is pretty of import for different types of apps. The near important one would be if your app requires a login. When the user launches your app for the kickoff time, you'd rather have them go to a log-in page instead of the the habitation page of your app, right? So you lot'd need to be able to cheque if it's their first time in the app or non.
Another neat fourth dimension to utilize 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 manner to implement something like this is to use an old, trusty friend: UserDefaults
. Let's get started.
Requirements
- Xcode 10
- Swift iv (and up)
I'thousand going to assume you have a little knowledge of how UserDefaults
work, and if you don't, check out my commodity on how they do. UserDefaults
is one of the well-nigh useful frameworks out at that place, in my opinion.
Getting Started
The first thing to do, equally 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 commodity).
Next, name it whatsoever you want — for this project, I will be naming it CheckForFirstTimeLaunchDemo
.
CheckForFirstTimeLaunchDemo
Click side by side, and save it somewhere you'll remember. At present permit's get some simple code up and running.
The Code
For this article, I chose non to do whatever UI work. I did this because it technically doesn't need any — plus it'southward for people to amend understand you don't need UI elements, similar a push button, to be able to load data when your app loads.
Commencement nosotros'll head into ViewController.swift
and set the cursor inside to the ViewDidLoad
method. Once you've done that, brand it await like this:
Line five: This is just setting the variable defaults
to UserDefaults.standard
.
Line 10: This is what starts it all. Information technology'southward but an if-statement
that checks to see if the key "Beginning Launch"
is set to true
inside of defaults
. Now if it is the start launch, the fundamental "First Launch"
won't be set to anything then the if-statement will output a false
reading.
Line 21: If information technology is fake, during the beginning launch of the app, it'll come up to the else
portion of the if-statement
. Nether this line is where you should put code yous want ran only during the first launch of an app. All line 21 does is print "Beginning"
to the debug area.
Line 25: Here's where almost of the magic happens: One time the app runs for the first time, "Outset Launch"
isn't prepare to annihilation, so it defaults to imitation
. But later the offset launch occurs, nosotros want "First Launch" to be true
, signifying the beginning launch already happened. That fashion when the if-statement
in line 10 gets run again, the else
portion won't run — unless the user deleted the app and reinstalled information technology.
Line 12: This prints out "Second+"
to the debug area, signifying the lawmaking nether that is what is being ran after the first launch already occurred.
Line 16: This line is exactly the same as line 25 simply in a different section of the if-statement
. Technically nosotros don't need this, but information technology makes me experience better having it.
And that's information technology for the code. A little tl;dr for the line explanation would be: Put whatever code you lot desire ran on first launch only betwixt lines 21 and 25. Put annihilation you want ran on every launch afterward that between lines 12 and 16, and you should be proficient to become!
Running the App
All you take to practise now is run the scheme that deploys the app to the iPhone simulator (it may take a few minutes if it'southward the get-go fourth dimension running the app to the simulator).
The first time yous boot upwardly the app onto the simulator or existent device, the debug surface area should say "First."
Subsequently that, go into the multi-tasking mode and close out of the application.
After you've done that, only run the scheme in Xcode again (that way you lot become the debug output), and you should now run into "Second+"
printed.
Determination
Being able to check to run into if the current app'southward session is the start time it has been launched or non actually isn't that hard to reach. With our friend UserDefaults
, you could gear up up your app to automatically load information every fourth dimension you open information technology in but a few brusque minutes.
Beingness able to do this adds another layer of usability any user of your app would profoundly appreciate. At present they don't manually have to fetch their updated data. It'll also become a long way if you're creating an app that needs a login.
The simpler y'all tin make a user interface, the better.
Projection Files
Hither's a link to the GitHub project for this tutorial.
Source: https://betterprogramming.pub/checking-for-the-users-first-launch-in-swift-df02a1feb472
Post a Comment for "Swift Only Load Again if Changed"