Preparing an iOS 7 App to use iCloud Storage

From Techotopia
Revision as of 20:11, 27 October 2016 by Neil (Talk | contribs) (Text replacement - "<table border="0" cellspacing="0"> " to "<table border="0" cellspacing="0" width="100%">")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
PreviousTable of ContentsNext
iOS 7 Directory Handling and File I/O – A Worked ExampleManaging Files using the iOS 7 UIDocument Class


Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book


In 2007 Catawba County in western North Carolina used tax incentives to persuade Apple, Inc. to build a data center in the town of Maiden (current population 3269) where Apple subsequently spent $1 billion building a 500,000 sq. foot data center. During construction of the center much speculation circulated in the media as to the purpose of the building. At Apple’s 2011 World Wide Developer Conference in San Francisco all speculation was laid to rest when the iCloud service was announced to the public.

This chapter is intended to provide an overview of iCloud and to walk through the steps involved in preparing an iOS 7 application to utilize the services of iCloud.


Contents


What is iCloud?

From the perspective of the average iPhone or iPad owner, iCloud represents a vast remote storage service onto which device based data may be backed up and music stored for subsequent streaming to multiple iCloud supported platforms and devices. By default each registered user account gets 5GB of storage space for free and the option to purchase more as needed.

From the perspective of the iOS application developer, on the other hand, iCloud represents a set of programming interfaces and SDK classes that facilitate the storage of files and data on iCloud servers hosted at Apple’s data centers (of which the Maiden, NC facility is now just one of many) from within an iOS application.

iCloud Data Storage Services

The current version of the iOS SDK provides support for two types of iCloud based storage, namely iCloud Document Storage and iCloud Key-Value Data Storage. iCloud document storage allows data files and documents on the user’s device to be stored on iCloud. Once stored, these files may be subsequently retrieved from iCloud storage via any supported device or platform using the owner’s iCloud account details.

The iCloud key-value storage service allows small amounts of data packaged in key/value format to be stored in the cloud. This service is intended to provide a way for the same application to synchronize user settings and status when installed on multiple devices. A user might, for example, have the same game application installed on both an iPhone and an iPad. The game application would use iCloud key-value storage to synchronize the player’s current position in the game and the prevailing score, thereby allowing the user to switch between devices and resume the game from the same state.


Preparing an Application to Use iCloud Storage

In order for an application to be able to use iCloud services it must be code signed with an App ID with iCloud support enabled. In addition to enabling iCloud support within the App ID, the application itself must also be configured with specific entitlements to enable one or both of the two iCloud storage methods outlined in the preceding section of this chapter.

Fortunately, both of these tasks can be performed within the Capabilities screen within Xcode 5.

Clearly, iOS developers who are not yet members of the iOS Developer Program will need to enroll before implementing any iCloud functionality. Details on enrolling in this program are outlined in the Joining the Apple iOS Developer Program (iOS 7) chapter of this book.

Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book

Enabling iCloud Support for an iOS 7 Application

In order to enable iCloud support for an application, load the project into Xcode and select the application name target from the top of the project navigator panel. From the resulting project settings panel, select the Capabilities tab and locate and switch on iCloud support as outlined in Figure 34-1, selecting a Development Team to use for the provisioning profile if prompted to do so:


Enabling iCloud capabilities in Xcode 5

Figure 34-1


Enabling iCloud support will have automatically added the iCloud entitlement to the application’s App ID, and also created an entitlements file to the project containing the application’s iCloud container identifiers.

Reviewing the iCloud Entitlements File

Once iCloud capabilities have been enabled for an application within Xcode, a new file will appear in the project named <product name>.entitlements. Any applications that intend to use iCloud storage in any way must obtain entitlements appropriate to the iCloud features to be used. These entitlements are placed into this entitlements file and built into the application at compile time.

If the application is intended to make use of iCloud document storage then the entitlements file must include a request for the com.apple.developer.ubiquity-container-identifiers entitlement. Similarly, if the key-value store is to be used then the com.apple.developer.ubiquity-kvstore-identifier entitlement must be included. Applications that require both forms of iCloud storage must include both entitlements.

The entitlements file is an XML file in which the requests are stored in a key-value format. The keys are the entitlement identifiers outlined above and the values are represented by one or more container identifiers comprised of the developer’s ID and a custom string that uniquely identifies the application (the corresponding application’s App ID is generally recommended, though not mandatory, for this value).

The entitlements file may be created either manually or, as outlined above, automatically from within the Xcode environment. When using the Capabilities settings, the entitlements file will appear in the project navigator panel. Note that by default, Xcode only enables iCloud container storage. If key-value storage is also required make sure that the Use key-value store option is also selected within the Capabilities panel.

A single ubiquity container is added to the entitlements file by default when using the Capabilities panel. Additional containers may be added by clicking on the ‘+’ button located beneath the Ubiquity Containers list.

Manually Creating the Entitlements File

As an alternative to using the Xcode capabilities interface, it is also possible to manually create the entitlements file. An example entitlements file containing entitlements to both document and key-value storage with a single iCloud container might read as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>com.apple.developer.ubiquity-container-identifiers</key>
        <array>
                <string>$(TeamIdentifierPrefix)com.ebookfrenzy.iCloudStore</string>
        </array>
        <key>com.apple.developer.ubiquity-kvstore-identifier</key>
        <string>$(TeamIdentifierPrefix)$(CFBundleIdentifier)</string>
</dict>
</plist>

Create this file using a text editor and save it as <appname>.entitlements (where <appname> is the name of your application), modifying the App ID accordingly for each entitlement request. Once saved, the file may be included into the corresponding Xcode project by opening the project and using the File -> Add Files to “<project name>” menu option or by dragging the file from a Finder window and dropping it onto the Xcode project navigator panel. The contents of the file may be viewed and modified from within Xcode at any time by selecting it in the Xcode project navigator panel so that it appears in the editing panel as illustrated in Figure 34-2:


An iOS 7 iCloud entitlements file

Figure 34-2


To modify a value simply double click on the corresponding field to enter edit mode.

Accessing Multiple Ubiquity Containers

The ubiquity-container-identifiers value is an array that may reference multiple iCloud containers. If an application requires access to more than one ubiquity container it will need to specifically reference the identifier of the required container. This is achieved by specifying the container identifier when constructing URL paths to documents within the iCloud storage. For example, the following code fragment defines a container identifier constant and then uses it to obtain the URL of the container in storage:

#define UBIQUITY_CONTAINER_URL @"ABCDEF12345.com.yourdomain.icloudapp”

ubiquityURL = [[filemgr 
     URLForUbiquityContainerIdentifier:UBIQUITY_CONTAINER_URL]     
     URLByAppendingPathComponent:@"Documents"];

If nil is passed through as an argument in place of the container identifier the method will simply return the URL of the first container in the ubiquity-container-identifiers array of the entitlements file:

ubiquityURL = [[filemgr 
     URLForUbiquityContainerIdentifier:nil]     
     URLByAppendingPathComponent:@"Documents"];

Ubiquity Container URLs

When documents are saved to the cloud they will be placed in sub folders of a folder on iCloud using the following path:

/private/var/mobile/Library/Mobile Documents/<ubiquity container id>/

Summary

iCloud brings cloud based storage and application data synchronization to iOS 7 based applications. Before an application can take advantage of iCloud it must first be provisioned with an iCloud enabled profile and built against an appropriately configured entitlements file.


Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book



PreviousTable of ContentsNext
iOS 7 Directory Handling and File I/O – A Worked ExampleManaging Files using the iOS 7 UIDocument Class