Preparing an iOS 9 App to use iCloud Storage

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
iOS 9 Directory Handling and File I/O in Swift – A Worked ExampleManaging Files using the iOS 9 UIDocument Class


Purchase the fully updated iOS 10 / Swift 3 / Xcode 8 edition of this book in eBook ($19.99) or Print ($45.99) format.
iOS 10 App Development Essentials Print and eBook (ePub/PDF/Kindle) edition contains over 100 chapters. Learn more...

Buy eBook Buy Print Preview Book


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.

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 from within an iOS application.

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




iCloud Data Storage Services

The current version of the iOS SDK provides support for three types of iCloud based storage, namely iCloud Document Storage, iCloud Key-Value Data Storage and CloudKit 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.

CloudKit data storage provides applications with access to the iCloud servers hosted by Apple and provides an easy to use way to store, manage and retrieve data and other asset types (such as large binary files, videos and images) in a structured way. This provides a way for users to store private data and access it from multiple devices, and also for the developer to provide data that is publicly available to all the users of an application. CloudKit data storage is covered in detail beginning with the chapter entitled An Introduction to CloudKit Data Storage on iOS 9.

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

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 Developer Program chapter of this book.

Purchase the fully updated iOS 10 / Swift 3 / Xcode 8 edition of this book in eBook ($19.99) or Print ($45.99) format.
iOS 10 App Development Essentials Print and eBook (ePub/PDF/Kindle) edition contains over 100 chapters. Learn more...

Buy eBook Buy Print Preview Book


Enabling iCloud Support for an iOS 9 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 41 1, selecting a Development Team to use for the provisioning profile if prompted to do so:


Enabling iCloud support in Xcode 7

Figure 41-1


The iCloud capabilities section provides options to enable key-value storage, iCloud Documents and CloudKit services.

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.icloud-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.

A single iCloud container is added to the entitlements file by default when using the Capabilities panel. Additional containers may be added by selecting the Specify custom containers option and clicking on the ‘+’ button located beneath the Containers list.

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:

let UBIQUITY_CONTAINER_URL = "ABCDEF12345.com.yourdomain.icloudapp"

let 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>/Documents

Summary

iCloud brings cloud based storage and application data synchronization to iOS 9 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.


Purchase the fully updated iOS 10 / Swift 3 / Xcode 8 edition of this book in eBook ($19.99) or Print ($45.99) format.
iOS 10 App Development Essentials Print and eBook (ePub/PDF/Kindle) edition contains over 100 chapters. Learn more...

Buy eBook Buy Print Preview Book



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