WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Permanent storage location question

Forums > iOS

At the moment, In my app, I store json files and images as data in the local device's storage, using the document directory. Is this document directory unique to the app (has each app it's own documents directory?) I assume yes.

What I want is that the app saves usage data to the user's personal iCloud somehow and that same data is shared between the user's different devices (ipad, iphone). But i don't want them to be able to access these files directly on iCloud, so somewhere obscured for the user.

Any nudges in the right direction, what do I need to make this work ?

3      

Look into CloudKit.

4      

@Roosterboy Thanks!

To be sure, that's not something like a paid cloud service right? Just storing my app's files on the user's own iCloud drive, right?

3      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Correct. You can store data private to each user or general for all your users. It's essentially an iCloud-based database in which you can store records and assets (like image files).

If you haven't already, and I assume you haven't, check out Project33.

4      

Thanks! I'll have alook at projects33. Last question, I can store json files and images I presume ? Apart from records ina. database like structure?

3      

Images would be stored in CloudKit as CKAssets. I'm not exactly sure how JSON would be stored, but CKRecords are key/value stores just like in JSON so maybe there's some way to map the two. Alternatively, one of the data types allowed in a CKRecord is Data, which you can probably convert your JSON to Data and store it that way.

3      

What about storing it in iCloud Drive in non-public directory? I think that will be much easier

4      

@nemecek-flip Yes! That's exactly what I want! Is that possible, and how? I don't want to use a coredate like structure as in cloudkit. Just read and write Json files with codable ...

3      

I just did this some days ago for my app but with public folder that is visible. But it wasnt visible until I appended "Documents" directory to the path and yet the file writing still worked.. So I guess you can get reference for the container and it should make the files hidden :D

I am actually planning to write a blog post about iCloud Drive so I think I can move it forward to today if you were to have problems with finding materials about it

4      

@nemecek-filip No need to rush it forward to today, but I am really curious how to read and write json and images to iCloud drive directly (no database like environment) to my own apps folder. When you have your post ready just let me know. Thanks in advance (I really can't find anything on this on the web)

4      

@Gakkienl here you go :-) https://dev.to/nemecek_f/ios-saving-files-into-user-s-icloud-drive-using-filemanager-4kpm I went by pull request on GitHub so I hope I did not forget any important steps.

Now saving to iCloud Drive looks pretty easy but when I was looking into it the first time it was super intimidating.

I haven't had the time to delve into the hidden folders. Let me know if you discover something!

4      

If you are using SwiftUI. you might want to look at Coredata and CloudKit.

This is a good video

https://youtu.be/TsfOYHbf4Ew

3      

@emin  

For some reason I now have to try all this out! I don't need it at the moment, but i suspect it's a something that can come up in app development quite easily :)

3      

@nemecek-filip
Super thanks! I'll dive right into it!

3      

I got it working, BUT ...

the icloud folder has a local sort of staging folder on each device from where it gets synced to the cloud. This could take a while, isn't always instantaneous. This folder seems to be well hidden, just browsing my iCloud drive I couldn't find it. Searching for my projects.json helped me locate the folder.

Now the real caveat, My iPhone reads and writes to this folder and the app is working fine. If I open my app on my iPad it doesn't read the projects.json from the cloud drive it seems, just from the local staging area. Also the projects.json doesn't get overwritten. After a while a have projects.json, projects 2.json, projects3. json and so on. The app should always overwrite the file.

Also on iPad, when the local file is synced (and becomes say projects 2.json in the cloud), I no longer have projects in my app, shows an empty screen ....

Seems I need to let go of the iCloud functionailty for now, doesn't work as I expected. More info on the matter is really welcome|

3      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.