How to use @AppStorage in SwiftUI | Bootcamp #52

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what's up everyone i'm nick this is swiffle thinking and i'm getting excited because as we're progressing in this swift ui bootcamp we can now start getting into some of the really cool really exciting things that we can do in swift ui and one of the coolest property wrappers that we haven't touched on until this video is called app storage so if you are an ios developer coming from ui kit you've probably heard of user defaults and app storage is just the swift ui version of user defaults and it's essentially the same thing but a little bit easier to use so in this video we're gonna actually use user defaults and then use app storage so you guys can see the subtle difference between the two and how cool app storage really is and for those of you who have never heard of user default never heard of app storage well you can think of this as basically a little mini database that's in the iphone where you can save information so that if the user closes your app and then reopens your app that information is still safe so this is perfect for situations when you have small pieces of data like your current user's name your current user's id or maybe the last time the user signed in or whether or not they are premium but generally this is not the place where you want to store a whole database i think a lot of beginner developers think oh this is perfect i'm going to save all my information there but really this is just for small pieces of data so with that said this should be a fairly easy video but i'm going to take a couple extra minutes in the video to really just explain to you what is going on because a lot of it is behind the scenes and again app storage is super important if you're using an app where you need to save a user's data whether or not they're signed in signed out you're going to have to use app storage all right everyone i am back in xcode of course let's right click the navigator create a new file it's going to be a swift ui view and let's call this one app storage boot camp go ahead and click create once you're inside click resume on the canvas let's get ready to get coding so let's get rid of this text hello world let's add a v-stack to our screen let's give it some spacing of maybe 20 and we'll open the brackets at the top of the v stack i'm going to have a text and for right now i'm just going to put my name nick and underneath that text we're going to have a button and the button very simply let's open the parentheses let's use the title string protocol approach this title will just do save and i'm going to make it dot uppercase after the end of the string i'll call dot uppercased and then for the action i'm going to hit enter and we're going to leave it blank for now we're not actually going to do anything with this save button just yet so the title the text says nick but let's make that a variable so we've done this a bunch of times in the course at the top we'll do at state var and let's call it current user name it'll be of type string and let's make it an optional string so there's a chance that we don't know the current user's name and if it's not set this will be equal to nil but if it is set it will be a string so let's put that current username in this text current username we're going to run into a quick error and this error is because this current username is optional and to have some text into this text object we need to make sure that we do have a value so if you're following other online courses and they're telling you to explicitly unwrap so if you click this and you click this force unwrap using exclamation point if you're seeing this in other courses i would advise you to get another course because this explicit unwrap is very dangerous because what you're telling xcode when you have this explicit unwrap you're saying that although this variable is optional and there's a chance that it is nil we are a hundred percent positive that right now it has a value that's what this exclamation point means and as we know there is no value right now so if we ran this code with this exclamation point it's going to crash so there are two safe ways to unwrap this optional into a text and the first one if we delete this and we click on that fix again it's giving us this other option with the call us using double question marks if i click that fix we can add two question marks and then give it a value if this is nil so this text is going to have whatever this value is if there is no value in current username so right now let's put add name here so if there is no current username it'll say add name here and if we click resume on the canvas it should say add name here right now beautiful the other safe way of unwrapping this which i showed you a couple of videos ago is by using an if let statement i'm just going over this because this is good practice so we could say if let name equals current user name open brackets and then we will add a text with name so if we can create this name variable with a value will add the text to the screen as we can see if we click resume on the canvas here this is not being created because right now there is no current user name whereas in this version this text is always going to be on the screen but if there is no name we're gonna have a basically a placeholder a default string in that text so again these are the two safe ways to optionally unwrap in swift ui in swift in general and what we're getting at in this video is persistence so when we click this save button i want to update the current username with my name so in the action for this save i'm going to call current username and i'm going to set it equal to nick so i actually want to start testing this on a simulator and not just this preview device so what we're going to do i'm going to open up that project navigator on the left side where we show or hide the navigator and i'm going to go up to the swiftful thinking bootcamp app.swift this is going to be whatever you name this project app dot swift file we did this way back in the first video of this course and i'm basically going to take this current view app storage bootcamp and make sure this is the first view when we run our app so this app storage bootcamp remember the name i'm going to go into that swifferthinkingbootcamp.app.swift file and you'll know if you're in the right file because it will have your at main call and we'll have the window and then this is the first screen in our app so the first screen i want to be the app storage bootcamp screen that's the screen that we just made so if i run my device on the simulator this will be the first screen and now i'm going to go back by pressing this back button back into the file that we just had i'm going to run this app now on our simulator so not on the preview here so we're going to do that by clicking the play button here at the top it's the run button and it's going to run our app swiffle thinking boot camp on an iphone 12 and if you want to change the device you can change your device here but uh let's do an iphone for now so iphone 12 is good and let's click run and it should create a it should pop up a simulator somewhere on your computer so i'm going to hide uh the canvas right now because we're not using the canvas i'm going to hide this navigation bar as well and show you guys the rest of my screen here let's make this bigger and let's look at the app so our app ran and this says add name here as we know because it's this text field and when i click save we should add current user name and it's going to update this variable so when this gets updated we should see the name nick and we should see it twice because now we can see the second text field as well so i'll click save and now i see nick twice this is great this is perfect this is what we've learned so far in this course and let's pretend like the user was in our app they added their name they clicked save and now they close the app so i'm going to close the app on my iphone and if i open up the app one more time now you're going to see that the name did not actually save between sessions it did not persist between app sessions so when i close the app it defaults it back to the starting state and this is where app storage comes in with app storage we can save small pieces of data such as the user's name the user's id into our app so that it saves and persists between sessions so i'm going to first show you guys how to do this using user defaults because it's important that you understand user defaults before you understand app storage and if you learned ios development on ui kit you're already aware and probably an expert with user defaults but let's check it out here so when i click save let's create a variable to hold the word nick so say let name equals nick and we can update the current user's name by calling by adding name here which is just this variable and then i want to save this name into my user defaults so let's call user defaults dot standard dot set and then we can you can see here that there are a bunch of different types that we can set we can set integers doubles booleans we're just going to use the any here because we're going to set a string so let's press enter and then it's asking for a value what value do we want to set or save well the value is going to be nick of course so we'll say name and then 4 key and this is basically just the key that we're going to save this under so the key i'm going to use is name and the key is important because it's going to save this name underneath the key so if we want to fetch this key later if you want to fetch this data we're going to need to know what key to look for so right now the key is just called name and by this one simple line of code we are saving our name into the user defaults and you can just think of the user defaults as a variable or like a dictionary that is behind the scenes and our entire app is going to have access to this user defaults so when we click save we're now saving our name to the user defaults for the key name but if we close our app and then we open it up again we then want to pull that information back from user defaults and set it into our current user's name on this vstack when it appears i'm going to call dot on appear open the brackets and when the screen appears we're going to now get the value from user defaults so we'll say current username this variable up here we're going to set it equal to userdefaults.standard.string4key because we know we're saving a string right this is a type string i'll even add type string here just to be sure and we're going to pull the string for key now be aware here we need to tell it exactly what key where is this saved under and we know it's saved under the key name so here we'll add name so when we click save we're going to save it to user defaults and when we reopen our app when it appears it's then going to look for some value that's saved under this word name and then add that to our current user name so let's check this out quick click run on the simulator here it'll build up our app again add name here obviously we don't have a current user name yet so let's click save and now when we click save it should have updated our current username but also updated the user defaults so if we reload our app now let's close our app open it back up and when it's on up appear it's going to then load the value from user defaults so you can see here now it's saved between sessions we have our current user's name between both sessions so we close our app we open our app we still have the user's name and we can do this for all different types of values as you saw right now we're just using a string with a user's name but we could do booleans we could do integers whatever small pieces of data you want to save in your app you can put them in user defaults now i wanted to show you user defaults because this is the underlying logic behind app storage this is what we've been using in ios development for years before swift ui came out you used user defaults but with swift ui there is a new property wrapper that we can use and it is called app storage so let's create a new variable here we're going to do at app storage and when we open the parentheses on app storage it's going to ask us for a key now we've seen this word key before we saw it down here and we're going to use this same key for this variable up here so we'll do quote name and we're going to make this a variable and we're going to call it current user name just like we did above and of course this will be of type optional string because if this was the first time our user was using this app there would be no value in this app storage in this key so now we're using this app storage version we no longer need this so i'm going to delete this at state current user name entirely so this is now our current user name and the cool thing about app storage is we no longer need to set variables into user defaults we no longer need to fetch variables from user defaults by changing this variable directly it will save so if we change this to a new name it's automatically going to do this line of code and set if we reopen our app it's automatically going to pull the name from the key so we don't have to actually have this on appear at all so what i'm actually going to do now is delete this on appear i'm going to delete this user defaults and just for example purposes let's change the name so here i'm going to put maybe emily and i'm going to click resume or click play on the simulator again and the first time it loads you're going to see that it's going to say nick still and that's because it's saved it pulled from the app storage what we had previously but now when we click save all we need to do is update this variable because this variable is automatically tied to the app storage key of name so all we're going to do is current username we're going to update the current username to emily click save it's emily it's updating on our view i can close my app i can open my app and it still says emily so you should now realize how easy and powerful app storage makes our lives because we don't need to deal with setting saving to user defaults we can just update this variable directly and it's updated on our app and the best part is that this line of code i could then put on any view in my app and they will all have access to that same variable so if i saved it here and i had a different screen on my app but i added this variable on the top of that screen i could still access that current user's name which is awesome alright guys so that was it for this video i originally planned to do a bunch more in this video but this is getting long and what i want to do is going to be a little complex i'm going to make a whole new video next where we really dive into how powerful and how cool app storage is so in this one i hope you got a really brief understanding on how to use app storage we looked at user default which is the underlying technology of app storage and in the next video we're just going to put on display some of the skills that we've learned so far in this course and really see the power of app storage don't forget to hit subscribe if you're enjoying this content as always i'm nick this is swiffle thinking and i'll see you in the next video you
Info
Channel: Swiftful Thinking
Views: 2,917
Rating: undefined out of 5
Keywords: SwiftUI Bootcamp, Learn SwiftUI, SwiftUI App Storage, SwiftUI What is app storage, SwiftUI User Defaults, SwiftUI UserDefaults, SwiftUI @AppStorage, SwiftUI how to use @AppStorage, SwiftUI how to use app storage, @AppStorage, What is @AppStorage, How to use @AppStorage, App Storage SwiftUI, User Defaults SwiftUI, UserDefaults SwiftUI, @AppStorage SwiftUI, @AppStorage not working, @AppStorage not working SwiftUI, SwiftUI persist data, SwiftUI @AppStorage property wrapper
Id: zyuSUrfelw8
Channel Id: undefined
Length: 17min 8sec (1028 seconds)
Published: Fri Mar 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.