Keeping application data synchronized across devices is a complex and daunting task. Fortunately, that is exactly why Apple built iCloud. In this Tuts+ Premium series, you will learn how iCloud works and how your applications can seamlessly share data across multiple devices.
Also available in this series:
- Working with iCloud: Introduction
- Working with iCloud: Key-Value Storage
- Working with iCloud: Document Storage
- Working with iCloud: Core Data Integration
One of the most touted features of iOS 5 has been iCloud. Even though iCloud shares a number of similarities with Apple's MobileMe service, iCloud is much more than MobileMe ever was. Like many other companies, Apple has come to realize that the cloud is an indispensable component of the mobile space. Some skeptics see iCloud as Apple's second attempt at building a sync service, but iCloud is a more ambitious undertaking with tremendous potential.
This is the first article in a series about iCloud Storage. Before diving into iCloud Storage, I'd like to take a minute to give a quick overview of what you can expect from this series.
This tuturial will not have any code samples in it. My goal for this tutorial is to get you familiar with iCloud on a conceptual level and to demonstrate how iCloud can assist you as a developer.
In the second installment of this series, I will show you how to enable iCloud Storage for an application. We will also build our first iCloud enabled application by making use of iCloud Key-Value Storage.
The final two installments will focus on iCloud Document Storage. Part three will focus on the new UIDocument class and will discuss how using it can make your life easier. Part four will focus on UIManagedDocument, a concrete subclass of UIDocument integrated with Core Data. Thanks to the close integration of Core Data and iCloud, you will see that your application can benefit tremendously from adopting both Core Data and iCloud in your applications!
What Does iCloud Offer Consumers?
To the consumer, iCloud is a free service that stores the user's data in the cloud, with data being music, photos, email, calendars, contacts, etc. The data stored on iCloud is available to any device set up with the user's iCloud account (e.g. Macbooks, iPhones, iPads). While MobileMe has always had a fairly high price tag, Apple made iCloud free from the start. There is a reason why Apple made this decision. For iCloud to be a success, it is vital that it is used by as many people as possible. Apple has big plans for iCloud, and as time progresses it should become an even more important component of the iOS and OS X operating systems.
How Does iCloud Help Developers?
The component most useful to developers is what Apple refers to as iCloud Storage. This is the part of iCloud that is designed for application data.
Together with iCloud, Apple has introduced a collection of APIs allowing developers to adopt iCloud Storage in their own applications. Apple emphasizes that their own applications, such as the iWork suite of applications, make use of the same APIs to interface with iCloud. The iCloud API is simple, easy to use, and surprisingly flexible. It is clear that Apple has put a lot of thought, effort, and resources into the creation of iCloud with regard to third party integration.
Benefits of iCloud Storage
Keeping application data synchronized across devices is no easy feat (just ask Apple, Dropbox, or Google!). Not only does it require a reliable back-end infrastructure, but one of the most daunting challenges is performing the data synchronization itself. Numerous scenarios can lead to data loss or corruption. Conflict handling, in particular, is one of the most challenging aspects of data synchronization. The more complex the data model, the more sophisticated the conflict algorithm must be to handle all edge cases.
Instead of creating a separate iCloud framework, Apple has enhanced some of its existing frameworks with iCloud APIs.
NSFileManager is a fine example of a class that has gained significant API support for iCloud integration.
One of the more prominent benefits of iCloud is its seamless integration with iOS. Every iOS device has an iCloud daemon running in the background. This daemon monitors changes in iCloud even if your application is not running.
Another iCloud optimization is peer-to-peer syncing. Just like Dropbox, iCloud will sync changes over the local network if it detects that a device with the same iCloud account is on the same local network.
Last, but not least, iCloud was designed by a company with many years of experience in delivering and managing data in the cloud. The ease with which developers can adopt iCloud in their applications shows that Apple has deployed a reliable solution that will benefit thousands of developers. For the vast majority of applications, it will no longer be worthwhile to develop a custom data syncing solution.
Behind the Scenes
If you are thinking about adopting iCloud in your application, I suggest that you take the time to get a good understanding of iCloud's inner workings and be aware of its limitations. iCloud enabled applications have an iCloud sandbox much like the sandbox an application has on iOS. Sandboxing has a number of clear security advantages. However, security comes at a cost. The downside of the iCloud sandbox is that accessing your applications' data from other installed applications is cumbersome.
Developers new to iCloud might think that it is a black box with Apple deciding what happens to your data and how it is managed. This is not true, however. Whenever a document changes on device A, the changes are pushed to iCloud. iCloud then notifies any additional devices, say devices B and C, of these changes. It is up to your application to decide how it will respond when changes are made. The same is true for conflict resolution. As I mentioned earlier, conflict handling is one of the most challenging aspects of data synchronization. iCloud will notify you when changes have taken place and whether conflicts have arisen. It is up to you to implement a conflict algorithm to handle any conflicts that might arise.
It is also worth mentioning that Core Data is closely integrated with iCloud. Since its introduction on the Mac platform many years ago, Core Data has become an amazingly powerful framework. Its integration with iCloud makes it even more appealing. Conflict management, for example, is handled gracefully by Core Data thanks to its tight integration with iCloud.
Adopting iCloud Storage
There are two ways that you can make use of iCloud Storage in an application, (1) iCloud Key-Value Storage and (2) iCloud Document Storage. Both are targeted at specific use cases.
iCloud Key-Value Storage
iCloud Key-Value Storage shares a lot of similarities with NSUserDefaults and it operates in a similar fashion. With iCloud Key-Value Storage, your application can store key-value pairs, just like you can with NSUserDefaults or a simple NSDictionary. The difference is that these key-value pairs exist locally (on the device) and in iCloud. At specific intervals, the local key-value pairs are synchronized with iCloud.
Keeping user settings in sync across devices is a possible use case for iCloud Key-Value Storage. It is perfectly suited for applications handling a limited amount of data. In other words, applications that are not data heavy and are only storing small amounts of information are best suited for Key-Value Storage.
iCloud Key-Value Storage has a number of limitations. The first limitation is that only property list data types (NSString, NSNumber, NSDate, NSData, etc.) can be stored. Custom model objects need to be serialized before they can be stored using iCloud Key-Value Storage.
A second limitation is related to the amount of disk space allocated to Key-Value Storage. Each application can store 1MB or 1,024 key-value pairs. As I mentioned earlier, Key-Value Storage is not an option for data-intensive applications.
The major benefit of Key-Value Storage is ease of use. It is extremely easy to set up and use in your application, requiring a very limited amount of development time.
iCloud Document Storage
As its name implies, this type of storage is aimed at document based applications. iCloud Document Storage can be used to store files and can also cope with packages.
One of the advantages of the iCloud Document Storage model is that the space available to your application is only limited by the capacity of the user's iCloud account.
These two types of iCloud Storage are very different, and it shouldn't be difficult to decide which type is the most appropriate for your application.
Setting Up iCloud
Configuring an application for use with iCloud is easy and requires only two steps. In the second installment of this series, I will talk about the setup process in more detail by walking you through the process step-by-step. In this article, I will provide a brief description of each step.
Step 1: iCloud Provisioning
An application is not by default configured to use iCloud. To enable iCloud, log in to the provisioning portal with your developer account and enable iCloud for your application in the App IDs portal. That is all you need to do to provision iCloud for use in your application.
Step 2: Configure Application Entitlements
The next step is creating the correct entitlements for your application. Every application that wants to make use of iCloud must be signed with iCloud specific entitlements. The entitlements define which applications have access to your application's data and these entitlements therefore provide a layer of security for the user's data.
If you are confused by some of the iCloud concepts that I discussed, then you may want to keep an eye out for the next installment of this Tuts+ Premium series. Next time, I will walk you through the process of setting up an iCloud enabled application, and we will build an application that adopts iCloud Key-Value Storage to keep the user's data synchronized across multiple devices. Stay tuned!
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.Update me weekly
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post