How to read / write to iCloud?

Hi all,

Quick question: when developing an iOS app, how does one read / write the data in iCloud?

The reason for asking is that I don’t want the user losing their previously saved data when they change / upgrade to a new iOS device.

Any where you can point me? (Not looking for a plugin advertisement.) :crazy_face:

Thanks,

Byron

For “data” do you mean files, or something more akin to settings? Do you want that open-able on a macOS app as well?

1 Like

Hi Jason,

Thanks for the reply. Yes, the ability to write files would be great! :slight_smile:

I am just wanting to make sure that when the user decided to switch to a new iPhone that they don’t lose all of their information they stored on their previous one. There needs to be a way to make sure that users don’t lose their data when they switch from one phone to another.

Byron

Having played with SwiftUI, I can tell you that the two most prevalent ways of retaining data across devices are:

iCloud Key Value Store (NSUbiquitousKeyValueStore): Apple Developer Documentation

as well as iCloud synched Core Data (SQLite):
https://developer.apple.com/documentation/coredata/mirroring_a_core_data_store_with_cloudkit/syncing_a_core_data_store_with_cloudkit

That being said, I am not sure what is natively available to Xojo without a plugin. I am sure iCloud storage is available with third party plugins.

If it were me (and seeing you have a pro license), I would stand up a server with a web service which maintains user data and settings, and have it synch with a local SQLite storage for offline availability. It would be more work than using something like a Core Data iCloud synch, but you would have complete control over how it worked as well as being prepared to offer greater functionality down the road.

I’m not sure how much this helps - but having a real backend to an app I think is really the best way to go. Down the road if you decide to offer an Android version (or other platform), you will already have the functionality instead of being locked into iCloud.

1 Like

I currently use iCloud KVS (NSUBIQUITOUSKEYVALUESTORE). It requires a few declares but it is easy to use.

Let me know if you need a demo project

4 Likes

A while back I had a similar post, but didnt find the demo. If you can provide one, that would be great!

You can check MBS Xojo Plugins with the NSUbiquitousKeyValueStoreMBS class. It allows to synchronize small settings like preferences over iCloud.

And we have classes or CloudKit to basically have a database to store values in iCloud.

1 Like

@Jeremie_L I would love to see a sample project as well. Thanks for offering to shed some light on this. I really need to learn how to research Apple’s docs and implement declares to add iOS functionality.

1 Like

Are there any examples?

1/ Save a file to Cloud (are folders possible?)
2/ List files in the cloud
3/ retrieve a file from the cloud

It will be greet if you have one to show us :wink:

1 Like

I thought we had an example.
But maybe you just look on Apple’s documentation on how to use it and then translate their example to Xojo code.

I confess Apple’s docs are as impenetrable as the MBS cloud plugin documentation to me.
I can’t identify anything useful in either at the moment.
Key Value pairs are too small to be useful to me at 64kb, and I can’t work out how to push , pull , or enquire on a file-by-file basis.
Happy to hold my hand up and look stupid here, but the people who brought us
’ NSUbiquitousKeyValueStoreDidChangeExternallyNotification’
clearly didn’t have clarity and widespread understanding in mind.

1 Like

iCloud files are merely a folder on your Mac in the user Library. You read and write to that folder as you would to any other folder on a hard drive in your Mac.

The location of my iCloud docs is /users/bart/Library/Mobile Documents/com~apple~CloudDocs

MacOS takes care of updating the files actually stored in the cloud, and keeps the files in the cloud synchronized with the files on your Mac.

Here is the command I use to save a data file named [BalCarry] into a subfolder [DataBud] in a folder named [Budget! 2022] in iCloud:

NOTE: my user name is [bart].

+++++++++++++++++++++++

balString = " a whole bunch of data joined into one string "

filPfx = /users/bart/Library/Mobile Documents/com~apple~CloudDocs/Budget! 2022/DataBud/
FilNam = filPfx + “BalCarry”

fldr = new FolderItem(filNam,FolderItem.PathModes.Native, false)

if fldr <> nil then

txtout=TextOutputStream.Create(fldr)
txtout.Write balString
txtout.Close

end if

+++++++++++++++++++++++

2 Likes

Does the file have to be a text file?
Is this limited to 64Kb?

What happens when moving to a new phone if the user has no iCloud active and the files are only stored locally? I guess the only way to retain these data is a local backup on a Mac, played back to the new iPhone during setup.