- Setting Up an iCloud-Compatible Project
- How iCloud Works
- iCloud Container Folders
- Recipe: Trying Out iCloud
- Working with UIDocument
- Recipe: Subclassing UIDocument
- Metadata Queries and the Cloud
- Handy Routines
- Recipe: Accessing the Ubiquitous Key-Value Store
- Recipe: UIManagedDocument and Core Data
How iCloud Works
In its simplest form, iCloud is merely a special folder. It lives on the iOS device in /private/var/mobile/Library/Mobile Documents/. You have partial permission to read from and write to this folder. That permission is limited to the container folder or folders declared in your application entitlements. iOS monitors and regularly updates this folder, synchronizing its contents with offsite storage. When you write to this folder, even just by dropping in an image or text file, it automatically joins the iCloud.
Your applications read and write local content from the folder. They do not need to directly access the Internet or check for connectivity. That is all handled for you by iCloud. All you have to deal with is local file URLs.
Updates arrive asynchronously at the device, where they can be handled immediately by open applications or addressed later by suspended ones. You’re responsible for designing your application to handle situations where cloud access is restricted. Apps must implement conflict resolution strategies to deal with staggered updates from multiple parties to the same cloud account.
Imagine this scenario: Mom is flying to Detroit for a meeting, so she’s offline for a while. Dad is at the gym, and Junior is checking in from the local library. Each of them adds new items to a shared To Do list application and may mark some off as “done.” That application must be able to handle these chronologically displaced updates in some smart manner, preferably involving users in its trickier decisions.
The UIDocument class, and its CoreData-enabled child UIManagedDocument, transform this otherwise complicated scenario into a simple notification. Applications are notified when a file’s state changes, allowing you to detect and resolve conflict situations as Dad and Junior’s live updates overlap with each other, and when Mom disembarks from the plane and her changes have to somehow meld with the ones created while she was offline. You might automatically merge all added items and globally apply deletions, or you might lend weight to newer versions, or you might ask the current user what to do. (If you use Core Data with the UIManagedDocument subclass, much of this is automated for you.)
Think about that conflict resolution scenario. Most of your coding responsibilities involve wrestling with how to resolve these conflicts in each of your applications. The rest of iCloud is actually both simple and mechanical, as you’ll see in the following sections.