54

My iOS and Mac apps have been launched to the App Store and all of my users, including myself, are using the CloudKit production database. My question is, is there any way to target the production environment for my development builds? It seems there is no way to target the production environment outside of the Mac App Store, and for iOS, only Ad Hoc distribution can target the production environment.

Eric
  • 1,858
  • 2
  • 16
  • 17
  • If you concern is to debug production environment, then it is not possible, but you can bring back all your record to debug environment see here: http://stackoverflow.com/questions/29274481/any-way-to-debug-app-with-dataset-in-production-environment – János May 13 '15 at 13:11

4 Answers4

120

Add the following entry to your entitlements file and perform a clean build.

This allows you to run your application in Xcode with the CloudKit in the production mode.

Entry to add: com.apple.developer.icloud-container-environment with value: Production. Note: Will not work with simulator

enter image description here

Moris Kramer
  • 1,490
  • 1
  • 12
  • 13
RawMean
  • 8,374
  • 6
  • 55
  • 82
  • worked great, but this still fetched data from both: production and development environment. Why?:) Shouldn't only from production? – Bartłomiej Semańczyk Jan 12 '17 at 09:18
  • 23
    for copy and paste: `com.apple.developer.icloud-container-environment` – Bartłomiej Semańczyk Mar 10 '17 at 20:30
  • 2
    Will it change anything if I forget to remove it when submitting to AppStore? – Bartłomiej Semańczyk Apr 14 '17 at 11:33
  • @Bart I haven't tried submitting for review with this. It may be a no op. – RawMean Apr 14 '17 at 14:46
  • 3
    It passes App Store Review without a problem. – JKaz Sep 20 '17 at 05:00
  • Make sure you do a clean and rebuild – RawMean Jan 06 '18 at 19:02
  • 3
    This still works in Xcode 9, Thanks @RawMean, this saved my day! For those who found it difficult to get to the entitlements file, it's in the project folder. Double click on it will open it in Xcode. – CodeBrew Mar 10 '18 at 22:54
  • This worked with a device! I added the key and value to the entitlements and deployed to production on the CloudKit dashboard. Then I also had to run it on my iPhone for it to fetch and post records. – pcnick13 Jul 31 '19 at 01:33
  • 2
    Works on my simulator! – Phuah Yee Keat Nov 24 '19 at 09:05
  • 1
    I set -environment to Production and APS to production and my xcode attached iPhone and my simulator both accessed the production databases. I set -environment to Development and APS to development and my xcode attached iPhone and my simulator both accessed the development databases. iOS 14.7.1 and Xcode 12.5.1. – KenM Aug 31 '21 at 07:39
  • This no longer works on Xcode 13. It complains about provisioning profiles. – john elemans Nov 23 '22 at 20:59
  • You can do a build and archive. Then Distribute App and select Ad Hoc, save the ipa file as usual. Now use the device manager to install the ipa file to your device. Voila – john elemans Nov 23 '22 at 21:08
  • @johnelemans I just tried it on Xcode 14.1 and it works – RawMean Nov 24 '22 at 00:48
  • 1
    Thank you so much OPs! I am about to launch an iOS app and imagine my deflation when my homemade CloudKit client MacOS app was not downloading my CloudKit final-testing records from the Production Container environment. Your post gave me a big chunk of my weekend back. People helping people online is still awesome to me, even after decades of experiencing it in my case. – Gary93730 Jun 10 '23 at 14:54
3

From the iCloud Design Guide https://developer.apple.com/library/mac/documentation/General/Conceptual/iCloudDesignGuide/DesigningforCloudKit/DesigningforCloudKit.html#//apple_ref/doc/uid/TP40012094-CH9-SW1

During development, Xcode automatically points your app to the development environment. Before you ship your app, configure your app using the distribution workflow. In this workflow, Xcode lets you choose whether you want to target the development or production environment and adds the com.apple.developer.icloud-container-environment entitlement to your app with the value you selected. Prior to shipping, be sure to configure your app for the production environment. Apps that target the development environment will be rejected by the App Store.

Per you comment here are some additional thoughts: Per Apple guideline, you should use the development env as long as you are still developing what is the best Data Schema for your application, and to allow faster development. Then Apple suggests to deploy to production and test on it, before giving to customer. So I think the answer is that it depends on where you are in the product development cycle. If you are in early stages use the dev env. If you have customers in the field then I would use the production env for production as long as it serves my needs. If I am developing new features requiring schema changes you should only develop those in the dev env until you are confident deploying them to production will not break anything for existing users and allow them a smooth transition. Hope this help.

harryhorn
  • 892
  • 6
  • 8
  • 1
    That's from the doc, pretty much what I asked in the original question. What I want to know is what's the common practice for developer who develop for CloudKit across Mac and iOS, does the team always use the CloudKit development environment, unlike all of their users? – Eric May 13 '15 at 05:23
  • added info to my answer. – harryhorn May 13 '15 at 07:22
  • Thanks for the reply, but that's not really convenient. In my case, the scheme is done, I just want to be able to use the production environment for all of my development builds as well, so let's say I setup a new device and download my app from the App Store, it would see the same data. As it stands now, as the developer if I want to run the development build in my daily usage of th app, I have to always run the development build and use a different CloudKit environment from the rest of my users. Not a big deal, just inconvenient. – Eric May 14 '15 at 05:03
  • Thanks, that clears up when development should be used. However, the questions asks whether it's possible to use the production CK environment for Mac app distributed as beta outside of the App Store. In my experience it is not possible. It'd be good to get a confirmation on that. – Eric Aug 30 '15 at 03:16
  • The problem is that TestFlight does not support OS X apps - and so when you wish to test against an iOS app which is in Beta on TestFlight, you need the OS X app in debug mode to be able to connect to production as well. It's a mess of an inconvenience right now as I haven't even been able to get Adhoc Mac apps to build for 'production' since Adhoc copies are essentially development builds on the Mac (there's no such thing as Adhoc). How do you then test!? – strangetimes Sep 12 '15 at 08:16
2

I am not sure whether setting "the com.apple.developer.icloud-container-environment entitlement" to "Production" will work on a device running from Xcode - it doesn't work for me on the simulator.

But you could make yourself an 'internal reviewer' and run a new version using TestFlight. TestFlight uses the Production Environment.

Peter B. Kramer
  • 16,385
  • 1
  • 16
  • 20
  • 6
    Revised - setting "the com.apple.developer.icloud-container-environment" to "Production" in the entitlements file and then running from Xcode on a device (not the simulator) sends me into production. – Peter B. Kramer Sep 02 '15 at 16:13
  • When I do that for a OS X application and run the app from XCode7, the app won't start with a `Terminated due to code signing error` Any ideas on how this may work for OS X apps? – berbie Sep 23 '15 at 08:48
  • It's buggy on the Mac, which is a shame. Please raise a bug with Apple so they know I'm not the only one complaining. – strangetimes Sep 23 '15 at 15:20
  • 1
    @berbie you will need Xcode 8 and macOS Sierra to use `cloud-container-environment` entitlement without code sign error – Harry Ng Sep 23 '16 at 10:28
  • 1
    2019: apparently it also works now on Simulator as of Xcode 11 – Sebastian Dec 14 '19 at 22:20
0

In addition to com.apple.developer.icloud-container-environment. I found that the production cloud did not work to start with. I checked the logs in the dashboard and could see errors. It was only when I deployed the development settings to the production did it work. It looks like the dev one will automatically create the new settings but the production one does not. Perhaps I should be doing more programmatically from the app to set things up but I hope this information helps.