iosxcodecordovaicloudcordova-plugin-file

How do I properly enable iCloud document sync for iOS app?


In my cordova iOS app, users can back up their data to a file which is saved in the Documents folder within the app's container, which then should be synced to iCloud. However, though the file is saved it is never synced to iCloud, so clearly I must be missing something. These are the steps I have taken:

iCloud Container

iCloud Info

iExplorer

At this point I would have expected to see xBrowserSync appear in Storage & iCloud usage settings, but I do not:

Storage & iCloud Usage

And neither do I see an xBrowserSync folder appear in the iCloud Drive app:

iCloud Drive

And of course I never see the files synced to my Mac or on icloud.com.

Clearly I'm missing something but I can't see what. I've read many SO posts regarding getting this working and I find Apple's documentation unclear and confusing so I'm kind of stuck at this point. Could someone please point me in the right direction?


Solution

  • After some further reading I believe I know what's missing here. I hadn't realised you need to explicitly tell iOS to upload a file to iCloud Document Storage, which is what will allow files to appear in iCloud Drive on the user's devices.

    So, I need to write some (iOS) code that utilise the FileManager class to:

    1. Call the url(forUbiquityContainerIdentifier:) method in order to get the location of the local iCloud container and initialise it (at which point the app will appear in Storage & iCloud usage settings)
    2. Tell iOS to upload the file to iCloud by calling setUbiquitous(_:itemAt:destinationURL:)

    I'm building a very basic Cordova plugin to do this. I'll update this answer with a link when I'm finished.

    Edit:

    The cordova plugin I developed is called cordova-plugin-icloud-document-storage. It currently only implements uploading documents to iCloud (i.e. setUbiquitous), it doesn't implement the other iCloud functionality of the FileManager class (such as managing cloud updates or version conflicts). If anyone would like to develop this functionality please do submit a pull request.