Save data and images to FileManager in Xcode | Continued Learning #26

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everyone i'm nick this is swiffle thinking and in this video we're going to look at the file manager now if you've been following my courses we have already learned user defaults and core data and those are great places for saving and persisting data but there's actually another place that we can save data on ios devices and that's of course the file manager now this file manager works exactly like the file manager on your computer on your mac when you go to save a document you usually open up a folder where you want to save it and then you click save and the file manager on the iphone works the exact same way we first find a folder where we want to save our documents and then we can save it and of course we can get it when we need it now the big difference here of course is that we are not actively double clicking and opening folders on our mac instead we need to programmatically tell the code exactly where to save a file exactly where to fetch and find a file this becomes especially handy in ios development when we're trying to save file types that are actually not accepted in core data so for example we can't save an image directly to core data but we can save it directly to the file manager so this file manager is a great place to put images videos audio files we can store json data we can store pretty much any document we want in this file manager and as i'll explain in this video although it's great we should use this sparingly because this is saving data to the user's device so we don't want to just start saving everything in the world to this device and another thing we need to take into account is that because we're saving on someone else's device we of course as the developer can actually open up their device and see all the folders and all the files that they have saved instead we need to be proactive and make sure we are monitoring and tracking everywhere that we are saving our data on their device because if we need to delete data or change data it's on us to know exactly where it is saved so in this video we're going to take an image we're going to save it in the file manager we're going to then fetch it from the file manager and then most importantly we're going to learn how to manage and delete from the file manager because as the developer it becomes your job to manage all the data for your application that is on the user's phone and if you're still confused at the end of this video do not worry because we're going to start using the file manager a bunch in the next couple videos and also in the next playlist when we build a full app and actually use this to save some documents all right i am back in our xcode project of course let's right click on the navigator create a new file and of course it will be a swift ui view let's call this file manager bootcamp click create once you're inside click resume on the canvas i'm going to leave the navigator open real quick because we are going to use it and again file manager is not technically a swift ui component but it is used in swift ui apps and it's used in all ios apps so obviously very important to go over and we're going to start off by bringing an image into our project so i actually have an image on my desktop right here called steve dash jobs and you can use any image that you have on your computer basically just take an image and drag it into the assets.xc assets folder so again that's this assets folder over here on the left in the navigator and just drag an image inside and if you're confused on this i did a video in the swift ui bootcamp on how to add images to your project and all you have to do is drag and drop images right here and just to point out that we can put images into this assets folder obviously like we just did but this should be used fairly sparingly because uh when you put images here they are actually embedded into your app binary so when you go to ship your app to the app store the data for these images is included in your app's file size so if you have a ton of images here it will make your app file size very big and you can even get rejected from the app store if your binary is too big so when you put images here it should basically be for images that are always going to be needed in your app and probably smaller images such as icons and logos that are core to your app but if it's something that you might want to download from the internet like a user profile or an instagram post that should be downloaded and not embedded right here all right and once you have this image here just take a note of exactly what the image title is so mine is steve jobs because we're going to need to type that out in a second so let's jump back into the file manager boot camp view i'm going to close the navigator and let's create a very simple screen i'm going to add a navigation view open the brackets i'm going to add an image and open the parenthesis and use the name here and this will be steve jobs it should be the exact name of your image let's make it resizable let's make it scaled to fill let's give it a frame with a width of 200 a height of 200 and we don't need the alignment and then maybe let's call uh clip clipped and a corner radius of 10. let's click resume see what it looks like all right i have my image on the screen let's finish up this view here by i'm going to hold the command button and click on the image and i'm going to embed it in a v stack you could also just type out the v stack here i'm going to put a spacer at the bottom so that the image is pushed to the top and on the v stack let's add a dot navigation title and let's just say file manager all right i'm going to take this image and actually put it into a view model so let's create a view model we're going to create a class i'll call it file manager view model and it will conform to observable object let's open the brackets let's initialize one in our view so we'll use an at state object var vm and we'll set it equal to a file manager view model open close the parentheses and inside this file manager view model we are actually going to have an at published var we'll call it image and it will be of type ui image and we'll make it optional and we'll set it equal to nil to start on this image in our view let's actually reference this image here and i'm using a ui image because i find that when we are moving images around like different classes and different structs it is just easier to use a ui image and if you try to pass in an actual ui component that is of type image it'll be much harder to work with so i definitely recommend using ui images in the background when you're dealing with the data and the nice thing is that this image actually has a initializer that takes a ui image so let's use this ui image initializer and of course we want to reference this one which is optional so let's start by saying if let image equals vm.image open the brackets so if we do have an image here then we'll add it onto our screen we'll paste this inside and we'll put our image here again i did a whole video on if let statements in the swift ui bootcamp if you're confused go check that out and then come on back if i click resume we should not see the image anymore because it's set to nil right now and we're going to create an init in this view model and when we initialize this v model we're going to call a function that's called func get image from assets folder open close parentheses open the brackets let's call it from the init and all we're going to do in this function real quick is set image equal to uh ui image open the parentheses we'll use the named completion and of course it will be steve dash jobs i'm going to use this string a bunch so let's actually make it a variable let's say let image name of type string and we'll set it equal to steve jobs i'll pass in this image name down here and if i click resume we should see our image back on the screen now so that's working and so again the reason we have this image right now is because we embedded it into the assets.xcs folder right we put it right here directly but when you're actually using your app most of the images in your app user profiles instagram posts those are going to have to be downloaded from the internet you're not going to have them directly so when you download them from the internet if you want to save them to your device you can save them in the file manager so what we're going to do is take this image that we already have and just save it to the file manager so although we're not downloading images in this video the part where we're actually saving to the file manager will be the exact same so let's create a button underneath this image here we'll create a button open the parentheses let's use the action and label i will leave the action blank for a second and the label will say save to i'll call it fm for file manager let's give it a foreground color of white let's give it a background color of color.blue let's give it some padding before the background and i'm going to add some extra padding dot horizontal after let's give it a corner radius of 10 and let's change the the font to be headline all right very simple button we have here and when we click this button obviously we want to save it to the file manager all right so what we're going to do is scroll on up here and outside of the view model i'm going to create another class we're going to call this class local local file manager and i'll open the brackets here and the reason i'm separating this is because in your apps the view model should be specific to specific views so this view model is for this view that we see here whereas this file manager is going to be generic for anything that wants to access the file manager so if i have maybe three different screens in my app and all three of those screens are saving to the file manager i'll actually have three views one for each screen i'll have three view models one for each screen but i'll only have one file manager class here and this will be the same file manager class that i use for all of the screens so that's why we're separating the view model from the manager here and because this is generic uh i'm comfortable making it a singleton so let's call static let instance equals local file manager let's create a func called save image and into this function we're going to have to pass in an image of course which will be of type uiimage and we'll we'll need a name that we want to save it as so add name of type string we'll go ahead and open the brackets and when you save images to the file manager you can actually save a ui image instead we save the data for the image so what we're going to do is call image dot jpeg data and then here we can change the compression quality if we want to compress it to a lower quality than it currently is i could put this as maybe 0.5 for 50 percent of the current quality but i'm going to leave it as 1.0 to just be 100 of the current quality now i'm using jpeg data because my image i know is dot jpg but if you add a dot png image you would probably want to use the image.png data all right so getting rid of that so let's actually say let data equals image.jpg data and if i hold the option button and click on this data we see that it's optional and that's just saying that there's a chance that we can't get the data from this image now we know this is a valid image and this will succeed but let's just be safe coders and wrap this in a guard let statement so let's add a guard here we'll say guard let data equals this else open the brackets and we will return and in this else we'll just print out error getting data this obviously shouldn't happen but we're just being safe coders and all we need to do to save it is called data dot write and we don't need the options right now you can play with that on your own time but we're just going to write to a url and let's click enter here and we now need a url which is basically where do we want to save this data and this is where we need to find a place in the file manager that we want to save this image so let's comment this out quick and let's create the url let's say here let directory equals file manager dot default dot urls4 i'm going to look for the one here with the for and in and i will make this full screen quickly because we actually don't need the canvas for this part and when we call this we can then add a search path directory here and if we press the period here we can see that there are a whole bunch of different directories we can choose from you can think of these directories kind of just as folders and it's where do we want to save all of this data and every user's iphone is going to have all these directories the library directory the movies directory the music these are the same directories that you see on your mac like if you went into the finder and you looked at the the root folders it would look the exact same here we have the downloads directory but i actually went and googled it for you guys and i came to this website here which i will post in the caption below and this is coming directly from apple of course it's on the apple developer website and this is talking about storing data on a user's ios device into the icloud and i guess that's if uh they have the icloud backing up their device which i assume most people do but anyway if we scroll down here we can see pretty straightforward from apple and number one here says that only documents and other data that is user generated or can not otherwise be recreated by your application should be stored in the documents directory so this will be stuff that is very core to your app but again cannot be recreated by your app so maybe if someone was signing in and they went through some specific onboarding process that created a document and it was a one-time thing then it would be a good place to store that in the documents directory but if it was something like a profile picture that they could download again and again and again obviously that can be recreated so we shouldn't put that in the documents directory but the documents directory is fairly common so to use that all we have to do is type in documents directory and if we click this in basically i use the user domain mask every time and i believe this is basically relating to if you have multiple users on a machine so if i click on this local domain mask it will say install items to everyone on this machine that would be access to my account on my computer as well as any other accounts on my computer but for like an iphone app the only thing that's really relevant is the current user so the user domain mask is probably a good bet to put for this and that's all we need to get into the documents directory but before we move forward let's look at a couple other directories that are important so let's say let directory to equals file manager.default.urls and let's jump back to this website and number two is the data that can be downloaded again or regenerated should be stored in the library slash caches directory so examples of files should be downloadable content such as used by magazines newspapers map applications so basically anything that a user can download downloadable content should be put into this caches directory and we can do that by calling here dot catches directory this is not rocket science we'll call that user domain mask and that's how we access the caches we're going to do one more let's say let directory 3 equals file manager.default.urls let's jump back in here and and number three is very simply data that is used only temporarily so the least important here should be stored in the temp folder and we can go back into the code and this one's actually a little different sorry i didn't mean to type out these urls here and to access the temp directory we just called dot temporary directory and just like that these are definitely the three most common directories and possibly the only directories i've ever used in the file manager so these three should cover all most scenarios that you guys are going to have and before we move forward i'm going to just print out each of these we'll call directory and let's just print all three here two and three and i want to print all three of these out and i want to call this save image from our code so we need so we need to reference this instance and then this save image so going down to our file manager here let's create a let manager equals the local file manager dot instance that's our singleton and then let's create a function here we'll say func save image open close parentheses open the brackets and in this save image we want to call the manager and then call the save image function inside the manager so we'll say manager.save image we need to pass in an image so this is our image which is optional so let's first unwrap it to make sure there is an image we'll say guard let image equals image else return we'll pass in this new image to this and the name of course will be our image name which we have up here let's then call the save image from our button so down on our button we'll say vm dot save image all right i'm going to take this file manager bootcamp view i'm going to copy it i'm going to open up the navigator and go into our app.swift file this is where your app main call is where your app starts i'm going to make the first screen in our app this file manager bootcamp i'm going to ch i'm going to click on the iphone 12 here and let's run our app on a simulator and we're running on the simulator because i want to get those printouts that we just went through and close this quick all right and when my screen loads i can see the image and i have this saved to fm and when we click this it should run this save image function and we should get these three printouts so looking down here we'd ignore some of this swift ui print here this is all just garbage honestly and hopefully when i click this button we get our three print outs and let's take a quick look at them it looks kind of like gibberish but these are url paths and they work just like the folders on your computer so when you go to save something on your computer you open up maybe the library folder and then inside that there's a developer folder and then you save this is the same thing so each of these backslashes represents a folder a folder and then eventually in here is going to be your device this is probably our device id and then after that is the documents folder so the documents directory opens up this documents folder and then we can save our image inside here the caches directory opens up the library and then the caches folder and the temporary directory opens up this temp folder so not too much going on here i know this looks kind of like gibberish but i just wanted to point out that we are saving things in different locations and these paths are important because any time we want to access the document at this path whether we want to save it or delete it or update it or fetch it we need to use the exact path so it's very important that we are monitoring what paths we're using and obviously inside this documents folder we want to add an extension specifically for this image so for our purposes we're going to use the caches directory for the rest of this tutorial so i'm going to actually delete these print statements and let's comment out the first one and the third one let's just call this directory and the last thing i do need to point out is that this call right here it actually returns us an array so if we look at this print out here it is an array and there's only one item in the array so although it's an array we only want to use these first item here we want to use this data in here we don't actually want the array so we're just going to call dot first at the end of it this is going to give us that first item which is this whole path here and then we're going to add our extension for this image we're going to say let path equals directory dot appending path component and the path component is going to be a string and we're going to use the name that we passed into this save image so let's use the backslash open close parenthesis we'll pass in the name and just like it is on my desktop it is stevejobs.jpg i'm going to save it as dot jpg here and real quick i'm actually going to just print out the directory here and let's print out the path i'm going to run the simulator one more time just so we can see explicitly where we are saving this image let's click our button and we can see here that the directory is the library backslash caches and then we're going to actually save this image as stevejobs.jpg and that is within the catches folder and as you can see this is optional so we want to unwrap it to make sure we actually do get a url here all right so i'm going to delete this right two and this print statement here and we're going to add a guard statement we're going to say guard let's say let path equals i'm going to basically redo all this let's say file manager i'm just going to put each segment on a different line so let's say dot default dot urls for the caches directory dot user domain mask dot first and that is optional and then we'll say dot appending path component and here i will just copy and paste this string paste it in here and i'll say else and then we can just print out error getting path and of course return so this is kind of just like a simpler way to write this directory and this path here so i'm going to actually just delete this section up here we have our path and now we can just call data dot right to and we have our path which is a url and this call can throw so we need to put it in a do catch statement and we'll say catch let error open the brackets we'll put our calls in here so we'll say try to write the data and if it's successful let's just print out here success saving and if it's an error let's print out here uh error saving and then let's backslash open close parentheses and print out the error again i've covered these guard statements and these do statements a bunch of times in my courses so i'm assuming that you already understand all of that and let's run the simulator one more time and see if we get this success saving printout click save to fm and success saving all right so we are successfully saving this to the file manager and that's step one and step two now of course is getting it back from the file manager so let's create a another function down here we'll say funk we'll say get image we'll open close parenthesis and open the brackets and when we go to get an image we're going to need the either identifier or the name of that image so let's pass in the name of type string and when we go to get the image we need the exact path where it is saved so this path that we created when we save the image we need to get the exact same path when we go to get the image so because we're using the same logic here this path i'm going to make this its own function so down here we'll say funk get path for image we'll add a parameter called name of type string and then this will return a url and we'll make it optional and we'll open the brackets we're making an optional in case for some reason we can't get the path so i'm going to cut this guard statement that we have up here with the file manager catches directory i'm going to paste it in here and if we get this error getting path we're going to return nil so that's when we can't get the url but if we can we get past this guard statement we're going to return the path all right and then back up in our code we no longer have this path reference so let's actually create another one in this guard let statement we're going to say guard let data equals this comma let path equals we're going to call our new function get path for image the name of course will be the name that we're passing in here let's just put this else back up here so now we again have a reference to our path and this should go away there should go away let's clean this up now our save image function is condensed our get path for image is reusable and when we go to get an image we can then use this again so we'll say guard let path equals get path for image and we'll pass in the name and the second thing i like to do is after we get this path i want to make sure a file actually exists at this path so it's a handy trick we can call file manager dot default dot exists file exists at path and here we need the string version of this path so when we call get image for path it actually returns a url and on that we can call dot path and this path returns us a string here so now we can just take this path and pass it in here and of course we'll say else open the brackets we'll print out error getting path and we will return so if we can get this path n file exists then we can continue down here and we can get our image and of course we're going to want our function to return a ui image and of course there's a chance here if we get this error we don't actually get an image so we'll make it optional in that scenario and in that scenario we will return nil but if we are successful we will return a ui image open the parenthesis and there's actually completion here that's contents of file and we can actually pass in a path which is our path all right we now have our function to get an image from the file manager so scrolling down into our view model here we have a function to get the image from the assets folder let's create a funk get image from file manager open close parentheses open the brackets and here we're going to set image equals to the manager dot then we're gonna look for our new function get image and of course we can pass in our image name all right so before we use this i'm gonna run the app one more time and you guys should as well what i'm going to do is basically right now we're getting this image from the assets folder right so this is coming from the assets folder where we started the video and when i click save to file manager it is saved to the file manager on the device it's a folder that is in the device it's not in our app code but it's actually in the device here so it's on this simulator and now if i comment this line out and instead i call get image from file manager if i run this app again it should get that image and it's getting it from the file manager this time so this time this image is coming from the device not from the assets folder and i can prove that because if we open up the navigator and we go to the assets.exe assets folder and i delete this image from our project completely so our project has nothing in the assets folder and i rerun the app we can see that we still have access to the image and that's because this image was saved to the device not in our app and this is great for persisting images in your app if you download some really important content you can save it to the file manager and it will save and persist and again this doesn't have to be four images it can be pretty much any any kind of document you want to save but it is very common to use with images videos maybe sometimes some json data or even audio files so this is coming from the file manager not from the assets folder i'm going to put my image back in the assets folder just for some testing again in a second but i just wanted to prove that to you guys so let's jump back into the code now we now have the ability to get the image from the file manager but i'm just going to put in the get image from assets folder for now so we learned how to save images to the file manager and we learned how to get images from the file manager the next thing we need to cover is how to delete images on the file manager and we're doing that because it's often overlooked but when you save things to the file manager they're saving to that person's device forever you should assume that if you save it to the manager it's going to be there until you explicitly delete it so if you're saving hundreds of images they're all getting saved and you don't want to take up too much space on that user's device so it's very important to monitor and delete old items from the file manager we're going to do that now let's go back up to our file manager class here save image get image and under here let's create a func delete image again we're going to need the name of type string we'll open the brackets and we're going to take this guard statement from the get image and we're going to copy it and just paste it in our delete image and we're not returning anything right now but and if it exists we're going to create a do catch statement and we're going to try to call file manager file manager dot default dot remove item at and this remove item at needs a url so so we will pass in our path and we have we had converted this path to a string up here but let's actually keep it as a url and then for this file exists we're going to need to convert it to a dot path down here all right in the catch statement we'll say let error and then we will print error deleting image and then backslash open close parentheses and pass in the error if we get to here let's just print out successfully deleted all right pretty simple function let's call this from our code so going down i'm going to create a func here funk delete image open close parenthesis open the brackets we're going to call manager.deleteimage and of course we can pass in our image name we need to call this from our view so going down i'm going to create a h stack i'm going to copy i'm going to cut this button here paste it into the h stack and paste another one below it and the second button i'm just going to call vm dot delete image and we're gonna say delete from fm let's make the background color dot red let's run this app one more time see if we are successfully deleting all right if i delete from fm we have successfully deleted so we know it is working and you'll notice here that if i click delete from fm again we're going to say it's going to say error getting path and that's because in our code we added some safe checks so that when we go to delete it we are checking that the file exists and since we had already deleted it it no longer exists so of course we're practicing safe code and we're printing out error getting path here all right so this is the basics of file manager basically we can save data to files so that it persists let's take this a couple steps further before we wrap up i want to put some data onto the screen here so we can see if it's actually saving or deleting because in your app you probably want to give some user feedback like if it actually saved or if it actually deleted so going down to the file manager view model i'm going to create a at published var let's call this uh info message of type string and we'll set it equal to a blank string let's take that info message and let's just put underneath this h stack i'm going to add a text with the vm dot info message and let's just give it a font of large title let's give it a dot font weight of semi-bold and maybe a foreground color of purple why not all right so now back up in our code and when i call this save image i wanted to return i wanted to actually tell me if this save image was successful so i'm going to go to this function here the manager.save image which is back up here and now let's actually just have this return a string and so if we fail this path here let's return error getting data if we come here and we are successful let's return success saving if we get this error let's return the error saving we do the same thing for the delete image let's return a string if we get here let's return error getting path if we're successful we'll return successfully deleted and finally if there's an error we will return error deleting image going back down we should now we get a return we get that return string when we call this so we'll just set uh info message equal to the result of that and here we'll set info message equal to the result of that i run the app one more time and interesting enough here i actually don't have the image pulling through and that's because in my init i have get image from file manager and in the last and about a minute ago we deleted the image from the file manager so obviously it is totally gone now so i'm going to change my knit back to get it from the assets folder let's let's not get it from the file manager real quick so we'll get it from the assets folder and now we can save to fm and we get the success saving and of course we can delete it if i try to delete it after it's already deleted we should get our error message let's try to save it again and if i try to save it while it's saved it will still say success saving and that's because you can overwrite documents on top of documents just like if you clicked save as and you saved something on top of something else it would just overwrite it so that's what we did here we just overwrote it by clicking save again so this is working perfectly and the last thing i want to introduce you guys to is creating your own folders because as i mentioned before you are in charge of what you save to this file manager so if you're saving a ton of stuff to the file manager you want to make sure that you have all the references and you know exactly where all that data is so that you can access it or delete it later because if i saved images to some random path and then i have no idea what that path is i'm not going to know how to either get that image i'm not going to know how to delete that image i need this exact path but what is handy sometimes is creating a folder specific to your app or specific to this part of your app where you can save everything inside so this way if you ever get lost you could just access that folder directly so what i'm going to actually do is create a another function here we're going to say funk create folder if needed open close parenthesis open the brackets and we're going to create an init so every time i create this local file manager which is only once because it's right here in our singleton when this is created i'm going to call create folder if needed and in this function we're just going to create a folder specific to our app so if i was creating an app here i would use i would obviously use this caches directory but within the caches directory i would create another folder specific to my app that i might call like my app underscore profile pictures or my app underscore content so this way if i ever needed to i could just delete that folder within the caches directory so going back up to create folder if needed we're going to say guard let path equals file manager dot default dot urls for the caches directories what we're using for the user domain mask dot first and that's optional and here we're going to add the folder and we'll say dot appending path component and then here i'm going to add the name of my folder so i'm going to call my folder my app underscore images you can call it whatever you want obviously you need to track what the name of your folder is but basically anytime i save an image from my app i'll put it into this folder and i might create several folders inside a single app but by making these folders it just makes it easier to manage where all my documents are saved so on this we'll then call dot path and we'll say else and here we can just return if we can't get this path finally we want to check that this path this folder doesn't already exist because we're creating the folder if needed but it's already existing we don't need it so we'll say if file manager dot default dot exists file exists at path and of course we can pass in the path and we want to check if it doesn't exist so we'll use the exclamation point so if it does not exist open the brackets if it doesn't exist we then want to create this folder we do that with a do catch statement and we say file manager file manager dot default dot create directory we're creating a directory because it's a folder and not a file of course we're going to use the at path the path will be the path and this is almost always true it basically means if we had like a folder inside folder so if our so if we appended a path component and then we appended another one and then another one and to get to that third one we needed to create two or three folders this would then create all those folders and then for the attributes this is optional so i'm just going to put it as nil and of course if there's an error we'll say let error and we will print here error creating folder i'll print out the error and here i'll also print out success creating folder all right so when we run our app now this init should be called and this folder should be created on the first time that we run it so we should get this success creating folder and the only thing we need to change now is the path for our image if i jump to the path for our image that we made earlier we actually now need to first go into the new folder and then add this and then add our custom path component so here i'm going to add a dot appending path component and the folder is going to be the exact same name that we just had so this will be the folder name i'm actually going to make it a constant up here let's say let folder name equals i'll set it equal to this and then i will just make this the folder name and down in our get path i will also set this as the folder name so now when we save our image when we're getting our image it should be within that folder and just to see that quickly when we go to save it where are we at the save image uh right before this return let's just print the path that we're saving to all right let's run the app quick as soon as it loads i should get a print out it looks like it happened way at the top here success creating folder so as soon as it loaded it created this new folder which is which printed out here it created this new directory for us so when we save it success saving and i should get a print out of where it's saved to so here i get my printout and it's the caches folder that we saw and then it's a folder that we just created my app underscore images and then my images within this folder so this is a little bit better this is a little more practical for your apps so we're not just saving things everywhere we're saving them structured so if we ever get lost or anything we can always just delete this folder entirely because we know because we know all the stuff that we've saved is within this folder and last but not least let's just learn how to delete a folder so in here let's create a func delete folder open close parenthesis open the brackets and i'm going to actually just copy the same path for creating the folder i'm going to paste it in our delete folder here so we have a path just to the folder name not to a specific image and in here we can just very simply called a do catch statement and we can call file manager dot default dot remove item at and i just realized here that we can actually use the string version or the url version so i'm sorry i didn't bring that up when we deleted an image i guess when i deleted an image oh yeah we did this funkypath.path thing here we could actually have just kept this as the path this wouldn't have to be this way and then we could have just done remove item at path then we can pass it in doesn't really matter if you're using the url or the string it's the same stuff and up here we will call dot remove item at path and we'll pass in our path here i'll just print success deleting folder we'll say let error print error deleting folder then we'll print out the error let's just call this delete folder from our code quick so going back down when we delete the image let's also call manager.delete folder just to see if it works let's click play and you'll notice that when i when i ran at this time we didn't get that printout at the top for creating a folder and that's because the folder already exists we created it last time we ran our app and that's why we called that function create folder if needed because if it already exists we don't run this code twice so being good coders here it exists we can save to the folder and if i delete we should now also have our delete success deleting folder so that's all it takes to delete the entire folder and if i rerun the app now it should have to create the folder again because it was deleted so success creating folder all right guys i know this was a little confusing but essentially all we're doing is working with the file manager to save items to fetch items and then to delete items if we need to the important things to note here is that the file manager works just like the finder on your computer you first find a directory you find the folders where you want to save it and then you save it with a name so in our case we went to the caches directory we saved it in a folder called my app underscore images and then we saved it with a name was custom to the actual name of the image that we were trying to save so we went into the folder and then we added our custom name and now you guys know how to save images delete images and get images of course so if you had an app where you had maybe an image that comes up very often in your app like it's on the home screen a user sees it many many times instead of trying to download that every time the user opens the app you might want to download that once and save it to the file manager but of course there are tons of images that are not going to be reused a ton of times so like if you went on instagram and you went to someone's profile chances are most of the time you're not going to see that image that that user posted that one time so you wouldn't want to save everything to the file manager it should be for things that the user is going to need or reuse multiple times and again as we learned we can always use the caches directory or even the temp directory for stuff that is even more temporary and if you're still confused do not worry because i'm going to use this in the next couple videos so that we can get more comfortable working with the file manager and when we should actually use it and in the next playlist when we actually create a full app we're going to use this efficiently so you will get some hands-on experience on using the file manager this unfortunately was a long video again and i am sorry for that i'm going to try to make some shorter videos coming up but this is just something that i think would be kind of hard to explain in detail without actually walking through all these steps so hope you guys enjoyed this thank you for watching as always i'm nick this is swiffle thinking and i'll see you in the next video
Info
Channel: Swiftful Thinking
Views: 4,034
Rating: undefined out of 5
Keywords: Swift FileManager, SwiftUI FileManager, how to use file manager SwiftUI, how to use file manager in Swift, Swift how to use file manager, SwiftUI how to use file manager, what is file manager swift, Swift save to file manager, Swift save files, Swift save files locally, Swift FileManager not working, Swift FileManager directories, delete from filemanager, save to filemanager, How to use FileManager, How to use FileManager Swift, How to save files Swift, save files in Swift
Id: Yiq-hdhLzVM
Channel Id: undefined
Length: 50min 39sec (3039 seconds)
Published: Thu Apr 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.