2
votes

I'm using iCloud to sync specific documents (UIDocument) between devices on iOS. My implementation is currently working, but I'm having trouble wrapping my head around how updating files works. This is how my implementation currently works:

  1. Construct an instance of my UIDocument subclass and save it locally with:

    [UIDocument saveToURL]

  2. Send the file to iCloud with

    [NSFileManager setUbiquitous]

These steps send my document to iCloud, and I am able to see the file on other devices. My question is how to I update a file? Must I construct a new UIDocument object and save, and setUbiquitous as above? I've tried this with UIDocumentSaveForOverwriting as the save operation in [UIDocument saveToURL], but the setUbiquitous call fails. I've also tried simply saving the local file and watching for metadata query update notifications on other devices, but don't receive any. How can I make an update to a UIDocument and have that change be reflected in iCloud?

1

1 Answers

3
votes

You're only supposed to do the setUbiquitous call once; that just copies the file from your local store into the ubiquity container (which is what iOS uses to sync the file with the cloud). The URL that you use from that point forward to open or to save the file (as in [UIDocument saveToURL]) should be the URL of the file as it exists in the ubiquity container.

You can get the location of the ubiquity container using code like this:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
        myobj.containerURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];

    });

According to the Apple docs, you're supposed to get the location of the URL by using a MetaDataQuery (since, apparently, the URL can changed based on various events). An example of this is in this apple sample.

Good luck. :) I've been struggling with this stuff for a few days...