简体   繁体   中英

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 registered on developer.apple.com and iCloud Documents capability enabled in xcode:

iCloud容器

  • NSUbiquitousContainers dictionary added to Info.plist:

iCloud信息

  • Archive build created and uploaded to App Store, build released to testers via Test Flight (Note: app has already gone through review process and been released to App Store).
  • Note, Cordova config.xml does not contain BackupWebStorage setting so cloud backup should be enabled (not sure exactly whether this affects document sync).
  • User saves backup file, which is saved to app's Documents folder (Note: the app uses the cordova-plugin-file plugin to save the file to the location denoted by the plugin variable cordova.file.documentsDirectory ):

IEXPLORER

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

存储和iCloud使用

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

iCloud云端硬碟

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?

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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM