Storing and using documents within iCloud

In this recipe, we will learn about the different methods that can be used to store and use documents within iCloud.

Getting ready

In this section, we will learn about the file coordinator and the file presenter classes and how the UIDocument class can be registered to receive updates whenever the iCloud data gets updated.

How to do it...

The following example shows how to use the NSFileCoordinator class:

myDocument = [[myDocument alloc] initWithFileURL:ubiquityURL];
myDocument.delegate = self;
coordinator = [[NSFileCoordinator alloc]    
               initWithFilePresenter:myDocument];    
               [NSFileCoordinator addFilePresenter:myDocument];

The job of a file coordinator is to coordinate the reads and writes performed by your application and the sync daemon on the same document. For example, your application and the daemon may both read the document at the same time, but only one may write to the file at any single time.

Note

For more information on the NSFileCoordinator class, you can refer to the Apple Developer Documentation located at the following link location: https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSFileCoordinator_class/Reference/Reference.html

The following example shows how to move a document from local storage to iCloud:

if (![[NSFileManager defaultManager] setUbiquitous:YES
    itemAtURL:localURL destinationURL:ubiquityURL error:&error]){
    NSLog(@"Error making local file ubiquitous. %@",    
           [error localizedFailureReason]);    
    return;    
}

Note

For more information on the NSFilePresenter protocol reference, you can refer to the Apple developer documentation located at the following link location:

https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSFilePresenter_protocol/Reference/Reference.html

As you can see from the following screenshot, it shows the process when changes are made on one device, and having those changes stored locally before being pushed back out to the iCloud service, using a local daemon process. We will learn about the NSFileCoordinator class and storing documents in the cloud when we look at building our example application for this chapter.

How to do it...

Note

Whenever your application stores documents to iCloud, it must specify one or more containers in which those documents contents will be stored, by including the com.apple.developer.ubiquity-container-identifiers key value entry within your application's entitlements file.

How it works...

In this recipe, we learned how to establish a UIDocument document as a file presenter so that you can register the class to receive updates whenever its cloud data gets updated by telling NSFileCoordinator to add the document as presenter. This means that a presenter class is one that takes a strong interest in knowing when outside changes happen to a given file.

Whenever you register for changes, you start by creating a document and a coordinator and then initialize the coordinator with the document as its performer, as shown in the first code snippet. Handling it this way, means that, the new document or presenter using the NSFileCoordinator class allows it to receive alerts about these changes and allows you to update your applications UI to handle when these changes occur.

Next, we use the NSFileManager class that allows you to move local files to and from the cloud using the setUbiquitous:itemAtURL:destinationURL:error: method. This method does nothing more, except moves the file safely from your sandbox into the central iCloud folder and back.

The method takes three arguments. The first establishes the direction of movement. YES brings items from the sandbox to the cloud. The second argument must always be the source URL, and the third its destination. If all three arguments are placed in the wrong order, the method will fail. The local sandbox URL takes on the following form as can be seen in the following URL:

file://localhost/private/var/mobile/Library/Mobile%20Documents/TEAMID~com~geniesoftstudios~iCloudExample/Document.doc;

See also

  • The Working with the iCloud storage APIs recipe
  • The Requesting entitlements for iCloud storage recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset