hey guys what's happening my name is cal and in today's swift and xcode tutorial we're going to
be having a look at user defaults and creating a pretty simple settings page for your iOS
application user defaults are a really great way to store a small amount of data that's persistent
rather than using core data and creating a whole entity and a whole bunch of stuff if you just
have like one or two little things you need to save like settings for example like a dark theme
or a light theme then user defaults is a great place to store them but yeah let's get started
creating a new swift and xcode project this is an iOS app that we're building i'm just going to
call it settings user defaults we're going to use storyboard and our programming language of swift
then first thing we're going to do is just get our layout sorted so i'm heading into the main
storyboard i'm going to drag in a label i'm going to center this horizontally in the container
as well as giving it a little bit of space to the top we're going to make the text size a
little bigger so up to 26 and what's going to call this setting so a bit of a title
then i'm going to drag in a horizontal stack view and then i'm just going to set that
below our settings label and then i'm just going to copy and paste down our settings label as well
as adding in a switch so we're just going to have a basic on off switch and then we're just going
to put our settings label inside that stack view so that you can see now that they're both nested
inside that horizontal stack view modifying those constraints as well as changing the label to be
off initially and then i'm going to copy and paste down our settings label once again putting this
just below our switch i'm going to change the text to theme and then i'm going to add in a
little bit of space on our label so just changing the width to 100
next what we're going to do is add in a segment so this is going to be for a theme so we're just
going to give it a little bit of vertical spacing as well and center it horizontally next i'm going
to add one more segment to our segment to control i'm going to call the first one light theme and
the next one's going to be a dark theme and then finally a red theme it doesn't really matter what
themes are i just wanted to make sure that there was three options that you could choose from
cool so that's all looking pretty good what we're going to do now is open up the assistant editor
and create some outlets so i'm just going to drag from the switch i'm calling it on off switch
as well as calling the label and on off label and then i'm going to create an outlet for the segment
just calling it theme segment and then we're going to create an action for both the segment as
well as the switch so just calling the first one segment changed and the next one switch changed
now we can close off the system editor we're going to head back into our view controller and firstly
we're just going to declare our user defaults so i'm just going to say let user defaults equal to
user defaults and standard next inside our switch change function i'm going to say if our on off
switch is on we want to save to our settings so save to our user defaults that the switch is on so
therefore it's now turning off so i'm going to set user default set it to true and then for key we're
going to create a string at the top we're going to call it let on off key and just giving it a
name of on off key it doesn't really matter what the key is name just want to make sure that you
use it in a variable like this so that when you reference it later you can't really make mistakes
rather than just copy and pasting down the string if you declare it as a sort of static variable at
the top and that way the compiler can help you out with minor typos and that sort of thing you
know you're using the right key for the right thing as well we're going to set our switch
text to off or on depending on whether or not the switch is turning off or on and then we do finally
we just need to go into our view did load and we're going to create a function we're going to
call it check switch state when the view loads we need to see whether or not the switch needs to be
toggled to on or off and we're just going to say if user defaults dot bool so get a boolean
for our key our on off key if it's true we want to set our on off switch to true and animate it to be
false and we also want to set our label text to be on and then we just want to do the opposite if our
boolean returns false so let's build and run this now and see if when we set our switch to on or off
and then we close the app and then we reopen it that the state that the switch was in previously
is still there we've successfully saved it to the user defaults and then we've set our switch
accordingly based on the settings cool so next let's get our segment working so i'm going to
declare a couple more strings i'm going to call this next string i'm going to call it theme key
what's actually inside the string doesn't actually matter it just needs to be consistent you need
to be using the same key so i'm going to create a dark theme a light theme as well as our red theme
next we're going to create a function calling it update theme inside here we're going to say let
theme equal to user defaults string for our theme key and then if our theme is equal to our light
theme we just want to set our selected segment index to zero as well as setting our background
color to white we're going to copy that and paste it down two more times and changing those to else
if statements i'm gonna say if our theme is equal to our dark theme selected segment is equal to one
and for red we're going to set it to two as well as just changing the background colors that they
set next inside our segment change method we're going to create a switch we're just going to call
our segment get it selected segment index and then in the case of it being 0 we're going to set our
user defaults to our light theme for our key our theme key our default is going to be our light
theme then we're going to copy and paste down our case 0 and just changing the case to 1 and 2 and
dark and red and then finally we just need to call our update theme function cool and if we run this
you can see that it's working in the same way you can choose the dark and it changes the background
color and when we close and reopen the app it's remembered what the settings were what the user's
preferences were whether or not they wanted to use dark or light theme and yeah so there you have
it that's how you use user defaults in swift and xcode a really great way to build a settings page
i hope you found some use in this tutorial and i'll catch you guys in the next one
you