Save data and images to FileManager in Xcode | Continued Learning #26
Vložit
- čas přidán 26. 08. 2024
- In this video we switch gears and look at saving documents to the FileManager. This is a handy resource for any iOS developer where we can save documents (files, data, images, videos, etc.) directly to a user's device. As the developer, it becomes your responsibility to manage the data that you save to someone's device and therefore we need to know how to save data, fetch data, and also delete data!
Learn more:
iOS Storage Guidelines: developer.appl...
File System Basics: developer.appl...
Next video: • Save and cache images ...
Last video: • Publishers and Subscri...
🤙 WELCOME BACK 🤙
WEBSITE: www.swiftful-t...
DISCORD: / discord
GITHUB: github.com/Swi...
SAY THANKS: www.buymeacoff...
I'm amazed at how simple it looks when clearly explained. You have a real talent for teaching.
Thanks for the comment Patrick! I'm very happy to help.
@@SwiftfulThinking he used to be a bully, and force me to eat dirt. now i eat bugs becuase its just a habit from him being a big ol meanie.
Hey Nick, Just want to thank you for all the content you make and the hard work you put in this videos. I am really thankful for that! The way how you teach is just amazing!
Thanks Melvin!
You're a great teacher, thank you for the content.
You should be the one writing Apples documentation. I feel their docs are the worst. And you are the best ate making it simple and clear! Thanks
Amazing as always. I love how detailed and slow you explain these complicated things. It really helps a lot.
I really appreciate the "best practices" approach you take in citing Apple guidelines. Another excellent video!
Awesome, better and better 👍
Thank you as always Александр :)
You tought me SwiftUI from nothing to a pro thanks so much bro
Many thanks for your explanation in an organized and easy-to-follow manner. I'm enjoying your series of video tutorials "SwiftUI Continued Learning."
Thanks for watching Kenji :)
simple, logical, easy to understand and follow. thank you for making this. How can you make the folder available to sync to the iCloud?
Thank you! This is such a helpful tutorial. Super clear.
great vid as always
I noticed that the saved JPEG is much larger than the original JPEG. It would appear that the compressionQuality using 1.0 is not the same as the original quality. BTW - you can see the file saved by the simulator simply by opening the path on your Mac.
I remember learning SwiftUI with you, which was fun, even though I was forced to learn UIKit. I am happy to understand that I still remember SwiftUI, so you did a great job, I guess.
Haha! That's good to hear. SwiftUI > UIKit though 🥲
@@SwiftfulThinking not always, UIKit words with maps better.
@@user-yd9xy3rb4x relax SwiftUI is the best UI tool never seen even tho it still new .
simply genius
You are like my Swift Manual. Thanks again
Uff, long video, BUT very useful as always and FM is clear for me now. Thanks Nick.
Hi Nick,
Some of the topics in this set of videos go over my head - firmly my fault (I'm a bit think sometimes, and getting on in years).
Anyway, I think you have a talent for training people, and I think you have a winning formula with these individual videos.
Great job Nick.
How can I buy you coffee?...?
Good work Nic. learned a lot. long video doesn't bother us at all because we wanna learn it properly. so itz okay. keep up the good work.❤️
Great explaination.
Love from India
I found the last step when add manager.deleteFolder() in VM's delete func then only can click save 1 time & many times in delete button, but when I click save again will show files not exit, when I study these code, I found should add manager.creatFolder() in VM's saveImage func, then will no longer show error message. if I delete the folder, it will create folder again when I click save button with no error.
Great video! Quick question: Why are you using a singleton vs an environment object on app level for Local File Manager?
Hi Hans. Great question.... We use @EnvironmentObject to put an ObservedObject into the environment so that another View can access it. In this video, the LocalFileManager is not observed from the View. Instead, the ObservedObject is our ViewModel and then the ViewModel is referencing the LocalFileManager... The real question would be, why did we use a Singleton, in which case I used a Singleton here because this will be a global class that is used across the app. In a more advanced series I would use Dependency Injection instead.
@@SwiftfulThinking got it, thank you!
Great Video! Amazingly explained.
I am using Xcode 14.3 while saving the image after first click it shows the save successfully ,but then it's not showing any message.. if anyone here face same problem.
I have a question?
Question 1: What is the difference between saving Images Externally with the help of Core Data and saving images with the help of File Manager.
Question 2: Images saved with the help of File Manager will sync with CloudKit?
1. CoreData doesn’t support image formats and is usually not used for saving Images / Videos / etc. Apps with CoreData will often save “imageUrlString” to CoreData and then load image from FileManager (or elsewhere).
2. FileManager does not sync across devices. I’m not sure if there’s a CloudKit sync option.
3. Most apps will use an Image SDK rather than manually doing it. I wanted to teach an example here, which is important to know and understand, but I would recommend an SDK such as Kingfisher or SDWebImage to load and save the images. In which case, you won’t be dealing w FileManager or CloudKit anyway🤙
For example, the Home app allows the user to save an image as a background. I would assume this would be using this concept. Also, can I find the image I saved in the simulator in the folder hierarchy?
Yea, you should be able to find the file. If you print out the path where we save the image you'll see the exact location. I think it will be pretty deep into the hierarchy though!
I really enjoyed this video. I am working on an App that uses a JSON file, and would like to create a desktop app that would allow me to create the data for that JSON file that will be used in the first app. This will help me for sure in getting to that point.
Hi ThePhotodiver! I'm glad the video helped. The code should be very similar except instead of using an Image you just save your JSON data! Thanks for watching.
What should I do for it to appear in the gallery?
How would you use this to sync files across devices similar to how Photos app works
This tutorial for the FileManager is specific to the device. If you want to share across devices, you can integrate CloudKit!
May I ask why you deleted the image from the file manager, and then the view did not update immediately but still shows the image.
Interestingly, when the get image function is added, the view is still not updated
Nick, When would you need to use NSFilePresenter / NSFileCoordinator? Phil
Is it possible to sync the data in FileManager across devices with iCloud? How? Thanks!
Hi Carlos! Well the FileManager is specific to the current device, however, we can use CloudKit to sync across the user's iCloud. That's a whole different framework though.
Congratulations, it was a really nice content. do you intend to share the code
Thanks for watching Eren! Yes, the full source code is available on my Github: github.com/swiftfulthinking
@@SwiftfulThinking Thank you for your answer, I hope your content will continue, you are doing very well.
@@SwiftfulThinking Although I know most of the topics you are telling, I'm still watching: D
how using video files from file manager? please help me
Guys I have written the data into a file in documents directory using Swift's standard File manager functions... I am able to view through xcode terminal but while testing in real iOS I don't see any file... Can anyone let me know if it is even possible to view the file???
You can do this if you save it on a simulator on your computer. You're probably better off printing out the path from Xcode and then trying to find it. It's a challenge to get to because it's very deep, but I think this may help: stackoverflow.com/questions/25198840/document-directory-path-of-xcode-device-simulator
thx!!
Thank you!! :)
Hi Nick, thanks for all your awesome videos! Just one question: in this video the func getImage retrieves and returns an UIImage. But how can i retrieve and return an mp3 file? What data type should I use in the return?
For audio and video, you can write the Data to a URL and then return the URL back to the application. We use AVPlayer to play these files, which can be initialized with a URL. You also can use the remote URL if u don’t want to write to the FileManager, but I often do anyway 👍
@@SwiftfulThinking Thanks a lot Nick, it worked! The reason I couldn’t play the remote URL directly is that I developed my whole player interface (buttons, slider, playing time, etc) using AVAudioPlayer (based on your video Continued Learning #9). But later I found out AVAudioPlayer does not play remote files; only AVPlayer plays remote URLs. So I decided to download the mp3 files in order to use my interface, which is working now. (By the way, it would be nice if you could post a video showing how to make an audio player with slider, controls, etc, using AVPlayer - I tried but was not able to do it).
Good!
👏👏👏👏👏 AMAZING
Great job.
Hey Muhammad.. welcome to the channel! Thanks for watching.
Great video, but how can the user see any files created in Files.app, I notice files I create the are not available to the user, even though I can see them thru terminal. Is there a setting to set in Xcode when the app is compiled, to allow these files accessible to the user/device?
The Assets folder is part of the application bundle. It is included in your app when you ship it to the App Store. Therefore, you can read from it but cannot write to it. If you need to write, you should write to a local URL on the device using the FileManager!
@@SwiftfulThinking thanks for the reply. I watched the bootcamp series for the second time also going through the Continued Learning series a couple times and now finally it's getting through to this old timer. I seem to understand the howto for path/folders/files. This video exercise I tagged all the functions and them folded them up, It made it short and concise and I finally visualized it. It was a great video. I noticed once you delete the folder you could no longer save the image, so I added the func createFolderIfNeeded() before, to check if the folder was still available. Works
@@SwiftfulThinking Thanks for replying. It is, not exactly my question. But I found out the answer. I was asking about writing files through FileManager. I wanted the user to see these files that the app creates. I found through another Swift Tuber to set the 'file sharing' options in Info. Everything with that problem is resolved.
37:40: vm = Localmanager.instance() poped up, code changed and I didn't see you covered it? Captain Nick
what a clown i am : I used state var instead of @StateObject and infoMessage just won't show and no bug report from compiler
Whew this was a long video, but i took small bites out of it until completed. 😁
NO I love a detailed video, let it be long if it is.
its kinda funny NICK. how you used to kick me!
not nice dude. we were young, and you used to KICK ME!
not KOOL dude. not KOOL at all.
nahh
This is a really helpful series! Thanks so much!
To get rid of the pesky constraint errors in the output, just add the following to the to the @main first view:
.onAppear {
UserDefaults.standard.setValue(false, forKey: "_UIConstraintBasedLayoutLogUnsatisfiable")
}
You are the best can I have your insta handle please 😍😍
Awesome, better and better 👍
Thanks as always Michel!