Setting up a CloudKit project with Dev and Prod targets in Xcode

Marcus Smith
Frozen Fire Studios
4 min readJan 26, 2016

Containers in Apple’s CloudKit framework have two environments: Development and Production. The Production environment is the data that users will access with the released app. The Development environment can be used to create or modify your database schema and to test without touching live data or affecting users. By default, Xcode will only access the Development environment. Getting your project set up to hit either of these environments can be done fairly quickly.

Enable CloudKit

Start a new Xcode project or open an existing one and click on the project to open the project panel. Go to the Capabilities tab and switch iCloud to ON and check the CloudKit service. Your app is now set up to use CloudKit!

Make a Prod Target

The next step is to create a new target for hitting CloudKit’s Production environment. To do that, first we’re going to duplicate our current target.

This gives us a new target with the same name but with “ copy” added to the end. In duplicating this target, another Info.plist is also created in the project, named the same as your new target.

I would recommend renaming both the target and the new plist to something more helpful, in my case: CloudKitDemo-Prod and Prod-Info.plist. If you rename the plist, you will need to go back into the General tab (for the new target) and click the “Choose Info.plist File…” button to reconnect this target to your renamed plist.

You will probably want to rename the build scheme for the Prod target to something more helpful as well. Click on the list of schemes and click “Manage Schemes…”

Double click the “[Project Name] copy” scheme and rename it like you did the target.

Change the bundle ID for the prod target to make sure that the two different targets are treated as separate apps on your devices, so that the information from the different environments doesn’t get mixed.

Change the Entitlements for the Prod Target

After you change the bundle ID, go back into the Capabilities tab of the Prod target, and you should have warnings for the iCloud entitlement. If you turn iCloud off and back on, Xcode should fix these issues, but the default container it specifies is now tied to the new bundle ID. We want both of these two targets to point to the same iCloud container, so change “Use default container” to “Specify custom containers.” Check the container ID that matches the original target, and uncheck the one matching your new bundle ID. Now we have two separate apps that are both connected to the same iCloud container.

After doing this, Xcode should have also generated a new entitlements file for your Prod target with a name matching the target. To get the Prod target to connect to the CloudKit Production environment, go into its entitlements file and add the key “com.apple.developer.icloud-container-environment” with a String value of “Production”

The project is now set up to run on CloudKit’s Development or Production environment depending on which scheme you build!

A few things to note:

  • You are not able to access the Production environment from the simulator. To make successful CloudKit calls from the Prod target, you will need to build to an actual device
  • For consistent code that works for both targets, use CKContainer(identifier:) instead of CKContainer.defaultContainer()
  • The Development environment has a just-in-time schema feature, where you can save records from the app without ever needing to do any setup in the CloudKit dashboard. This is not available in Production.
  • To modify your database schema for Production, you must deploy your Development schema

--

--