Can't access any data from my `CKContainer` from a app extension - CloudKit, Swift

Viewed 485

I would like to access some data from CloudKit through an app extension (more specifically a watch app).

In my main app I simply create my container like this: CKContainer(identifier: "myContainerID") and then call any methods on the private database

In my app extension I create a container with the exact same code but when I try to call a method on the private database I get this error:

<CKError 0x600003953c90: "Permission Failure" (10/2007); server message = "Invalid bundle ID for container"; uuid = 76F8A8B0-0B53-4D23-A7EB-D9982C7CE0BB; container ID = "myContainerID">

I've also doubled checked that I've got all the correct iCloud entitlements.

I've also seen this question but I'm already using the solution: how to make watchkit extension app and my iphone app share the same icloud databases

Thanks for any help!

2 Answers

I've spent the whole day today trying to fix, and finally succeeded. Here is what I found: the last name in the cloud container ID must match product target ID and application name (value of CFBundleName in your plist file). This is how it's set in info.plist by default:

<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string> 

where $(PRODUCT_NAME) is the target that I've mentioned above. See the picture on the bottom. It's very important to note as well that fixing the mismatch by changing the existing target is not going to work. What you'll need to do is to create a new container with an ID where the last name matches your product name, e.g. if your product name is abc, the new container ID should look like:

iCloud.com.company.abc

After the change is made, you'll need to add the new container to your APP ID, and re-create profiles, because they will be invalidated. After new profiles are created, just download them and import in Xcode/Signing and Capabilities.

Note that official Apple docs do say that using an app name is recommended, but they've never said that it's mandatory and the only way to make it working.

enter image description here

Apple seems to have fixed it on their side!

Thanks for the help anyway :)

Related