iCloud 2: Setting up an application for iCloud

This is part 2 in my iCloud tutorial series – the previous article is iCloud 1: An introduction to iCloud.

In this installment I’ll discuss how to set up an application to use iCloud. In this post I discuss the steps you need to do to set up iCloud support for your application (I do create an application in this posting, it just doesn’t do any of the “iCloud goodness” yet).

The things you need to do for setting up an application to use iCloud are:

  • Setting up an app ID in the Apple Provisioning Portal and configuring it to support iCloud;
  • Setting up the iCloud entitlements in your application;
  • Obtaining a provisioning profile for the application.

What is confusing is that these steps take place in different locations – the first is done using the provisioning portal on the developer web site using a web browser, the other is done in XCode (Note: I am using XCode 4.2.1 for this tutorial – you need to be using XCode 4.2 or later due to the need to support iOS 5 for iCloud).

In this tutorial I am going to focus on iOS devices.  You can build and test a lot of your application there, but Apple currently does not allow the simulator to support iCloud.  This means the only way you can test iCloud functionality is to run it on a real device, which means the device needs to be registered with the provisioning portal and enabled for development use.  I will not go over how to use the provisioning portal – if you need help on this the best place to go is the Apple iOS Dev Center at http://developer.apple.com/ios.

For this tutorial I will use an application that I have developed calle NotePlus. This is a simple note taking application that also allows you to record an audio file to go along with each note.  I have made the source code for the initial version of NotePlus available here (Note that this is a ZIP archive that contains all the source code for this application).

NotePlus is a universal application.  I’m doing this because the easiest to see if iCloud is working by testing with two devices, and I assume that most people will either have an iPad and an iPhone or iPod Touch tied to the same Apple ID or they’ll have an iPhone and an iPod touch tied to the same Apple ID.

The importance of the bundle identifier

Make sure that you note the value of the Bundle Identifier for NotePlus – this is the suffix for the App ID we use to identify this appellation (Note that my bundle identifier is com.beret.NotePlus – you will use a different bundle identifier based on your personal or company Apple Developer Portal setup). This is what we will use to enable iCloud for this application in the provisioning portal.

Setting up NotePlus for iCloud in the Provisioning Portal

iCloud is enabled on an app-by-app basis in the provisioning portal.  The current default value is that it is not configured, which makes sense when you think about it. I will configure the NotePlus application in the provisioning portal and configure iCloud during the process.

The steps are:

  • Go to the App ID section of the provisioning portal and click on the “New App ID” button;
  • Fill out the form for the new App ID, ensuring that you put the correct bundle identifier in the right place on the form, then click the “Submit” button.
New Application ID

New App ID form for the NotePlus app

  • Once you click submit you will see the App ID has been added to the list of App ID’s that you have created.  It should look like something in the file below.  You are not done, however.  If you look at the column for iCloud you will see that there is a yellow ball with the text “Configurable” next to it. Click on the blue Configure link to the right of the App ID entry so that we can configure this.
NotePlus App ID created in the provisioning profile

NotePlus App ID in the provisioning profile

  • When the configuration dialog opens in the browser, click on the “Enable for iCloud” checkbox (Note that you will get a dialog box that will inform you that all provisioning profiles that support this application will support iCloud).  Once you click through the information dialog click on the “Done” button.
Configure the NotePlus app ID for iCloud

Configuring the NotePlus App ID for iCloud

  • Now you are back at the list of App ID’s in the provisioning profile.  Note now that the NotePlus application has a green ball and the text “Enabled” in the iCloud column. Our job is done here, so we can go back to XCode and do the second part.
NotePlus is now iCloud enabled

NotePlus is now iCloud enabled

Getting a Provisioning Profile for NotePlus

Now that you have set up NotePlus for iCloud, you need to get a provisioning profile to use.  You can either generate one using XCode (in the Organizer) or you can create a new profile in the Provisioning Portal.  All this is done using the standard techniques for obtaining a provisioning profile.

Setting up entitlements for NotePlus

Once you have completed the above we need to set up the entitlements for NotePlus to allow it to use iCloud. Apple uses this as part of mapping the application to the iCloud storage, and it also allows the developer to manage the type of data that the developer wants to synchronize.  The developer can specify one or more directories/folders to enable for iCloud storage and also enable key-value storage.

Why would you want to manage this from an application standpoint?

  • iCloud storage is not an infinite resource.  Users get 5 GB of storage for free but this storage is shared between all their applications (excluding storage for items bought through the iTunes music storage).  This means that your application and other applications are trying to carve out a piece of this storage space for themselves.  Since this is obviously not coordinated, at some point in the future the user may run out of space in the cloud.  The options the user has are to either buy more storage space from Apple or to free up some space.  As an application developer you need to be mindful of what you store in iCloud.
  • iCloud does allow you to share data between applications (just not between users). You can specify the bundle ID’s for other applications you wish to use to share data.

Once you’ve unpacked the NotePlus application open it in XCode.  Once it has loaded select “NotePlus” in the “Targets” portion of the middle area of the screen.  You should see something like the following:

XCode project summary

NotePlus target summary information in XCode

Be sure to change the value of the “Identifier” field in the right-most section from com.beret.NotePlus to whatever bundle ID you have defined for your application in the provisioning portal.

Now scroll down the right-most section to the “Entitlements” section.  This is where you enable your entitlements.  You should see the following on your screen:

The Entitlements section for the NotePlus application before enabling iCloud

Now click on the checkbox at the top of the Entitlements section (the one labelled Enable Entitlements).  You should now see the following (only with your bundle ID in the various sections instead of com.beret.NotePlus – you did change it, didn’t you?):

After enabling the Entitlements for NotePlus

Notice that the act of checking the Enable Entitlement checkbox fills in the information for iCloud, including the iCloud key-value store information and the iCloud containers.  You can use the iCloud containers to add access to shared containers (where you can share data with other applications).  For this tutorial series I’m going to keep it simple and just use the defaults.

At this point, we’ve done all the infrastructure setup to enable NotePlus to use iCloud.  Of course, just enabling it doesn’t make data sharing happen – we need to add logic to the code to actually access the iCloud data.  That will happen in the next several tutorial postings.  ‘Til then…

Next: Data and Metadata

One thought on “iCloud 2: Setting up an application for iCloud

  1. Pingback: iCloud 3: Data and Metadata, and an introduction to NotePlus | Musings and Opinions

Leave a Reply

Your email address will not be published. Required fields are marked *