UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

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      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.