Home > Articles > Programming > General Programming/Other Languages

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

Using iCloud in Your App

At this point, you’re ready to use iCloud in your app. You will see concrete examples of how to do so starting in Part III, “Using the Technologies.” There is one step that you can take now to confirm that your app has been properly set up and that the entitlements and provisioning are correct.

Create a new app or use an existing app that you want to enable for iCloud (starting from a new app is a simpler way in the long run until you’re more comfortable with iCloud). Set up the entitlements and provisioning as described in the previous sections. Add a single line of code to test if iCloud is available:

id currentiCloudToken = [[NSFileManager defaultManager]
                          ubiquityIdentityToken];

On OS X, it should go in applicationDidFinishLaunching:, and on iOS, it should go in application:didFinishLaunchingWithOptions:. In both cases, it normally goes after your other initializations. (Note that this method was added in iOS 6 and OS X Mountain Lion (10.8). You can find older and more complex ways of performing this task on the web.)

The iCloud token that is returned is an opaque object identifying the iCloud account (that means that you can’t see inside it). There are two possibilities when you ask for the token:

  • If it is nil, the user is not signed into an iCloud account.
  • Although you can’t see the account details, you can check if a token is the same as another token using isEqual:. This lets you check to see if the user has changed iCloud accounts.

Note that if a user has been signed into an iCloud account and turns on Airplane mode or turns off networking on a Mac, the token is still returned. You can access the local copy of your ubiquity container’s data. When Airplane mode is turned on again, iCloud will take care of syncing the two stores and will let you know if there is anything for you to do. Because the operating systems manage these disruptions in connectivity, resist the temptation to store extra copies of data locally in the app’s sandbox.

Apple recommends as a best practice that you use either iCloud storage or sandbox storage. Mixing the two provides a suboptimal user experience. Along those lines, ask users if they want to use iCloud the first time they run your app. Unless they reinstall the app, don’t ask them again.

The iWork apps are a good example of how to manage documents in iCloud. Over the last few years, they have moved to an explicit Export command, which, among other things, can let you export the contents of an iCloud document to another format and to a non-iCloud location.

  • + Share This
  • 🔖 Save To Your Account