Home > Articles > Mobile Application Development & Programming

  • Print
  • + Share This
This chapter is from the book

Recipe: Trying Out iCloud

The best way to convince yourself that you have properly entitled and established iCloud is to create a file from code and assure yourself that it propagates across to all devices. That’s exactly what Recipe 18-1 does. It establishes the documents folder discussed earlier, writes a small text file (“Hello World”) to the cloud, and builds a custom URL that you can use to access that new file from outside the cloud.

Requesting this URL requires net access and can take several seconds to retrieve. Please be patient during this time. The URL remains valid for a limited time, so the code also prints out the expiration date for that access period to the console.

After running this code, you’ll want to ensure that cloud files reached their destination on each test device you have signed into the same account. Try downloading the file from the URL and, only after that, delete it using iCloud settings.

Recipe 18-1 Giving iCloud a Spin

+ (NSArray *) contentsOfUbiquityDocumentsFolderForContainer:
    (NSString *) container
    NSURL *targetURL =
        [self ubiquityDocumentsURLForContainer:container];
    if (!targetURL) return nil;
    NSArray *array = [[NSFileManager defaultManager]
        contentsOfDirectoryAtPath:targetURL.path error:nil];
    return array;

+ (NSArray *) contentsOfUbiquityDataFolder
    return [self contentsOfUbiquityDataFolderForContainer:nil];

+ (NSURL *) ubiquityDocumentsFileURL: (NSString *) filename
    forContainer: (NSString *) container
    if (!filename) return nil;
    NSURL *fileURL =
        [[self ubiquityDocumentsURLForContainer:container]
    return fileURL;

+ (NSURL *) ubiquityDocumentsFileURL: (NSString *) filename
    return [self ubiquityDocumentsFileURL:filename
// Log the contents of the ubiquity documents folder
- (void) list: (id) sender
    NSLog(@"Contents of Documents: %@",
        [CloudHelper contentsOfUbiquityDocumentsFolder]);
// Create a new text file
- (void) create: (id) sender
    // Write to default container
    NSError *error;
    NSURL *targetURL = [CloudHelper
    // Write a "Hello World" Text file to the cloud
    NSLog(@"About to write to file.");
    if (![@"Hello from the cloud!" writeToURL:targetURL
        atomically:YES encoding:NSUTF8StringEncoding error:nil])
        NSLog(@"Error writing to %@: %@",
            targetURL, error.localizedFailureReason);
    // Retrieve a URL to share
    NSDate __autoreleasing *date;
    NSURL *url = [[NSFileManager defaultManager]
        expirationDate:&date error:&error];
    if (!url)
        NSLog(@"Error creating publishing URL: %@",
        NSLog(@"iCloud URL: %@", url);
        NSLog(@"Expires: %@", date);
- (void) loadView
    [super loadView];
    self.navigationItem.rightBarButtonItem =
        BARBUTTON(@"List", @selector(list:));
    self.navigationItem.leftBarButtonItem =
        BARBUTTON(@"Create", @selector(create:));

    // Default Ubiquity Container
    NSLog(@"Data: %@", [CloudHelper ubiquityDataURL]);

    // Shared Ubiquity Container
    NSString *sharedIdentifier =
        [CloudHelper containerize:@"com.sadun.SharedStorage"];
    NSLog(@"Shared: %@", [CloudHelper

    // Nonexistent Ubiquity Container. Will error.
    NSString *nonexistentIdentifier =
        [CloudHelper containerize:@"com.sadun.nonexistent"];
    NSLog(@"Nonexistent: %@", [CloudHelper

    BOOL success = [CloudHelper setupUbiquityDocumentsFolder];
    if (success)
        NSLog(@"Default ubiquity Documents folder is ready");
        NSLog(@"Error setting up ubiquitous documents folder");
  • + Share This
  • 🔖 Save To Your Account