Search code examples
iosxcodecontainersicloudcloudkit

iOS App Won't use custom iCloud container


So, I'm developing two apps that I want to use the same iCloud container.

I have followed all steps as shown in Xcode and apple's online documentation. I added activated the iCloud functionality to my app, checked the CloudKit checkbox, selected a custom container I wanted to work with (obviously the same for both apps).

The app has changed the iCloud container identifier in the entitlements file, which should indicate everything works fine. However, whenever I use the app and print the name of my container, it shows the name of the default container for each app.

for both apps, the Capabilities screen looks like this: enter image description here

I have created one object, CloudKitController, to manage all cloud traffic. I intialize it overtime the app launches, and set up variables like this:

var container:      CKContainer
var publicDB:       CKDatabase
let privateDB:      CKDatabase
var delegate:       CloudKitControllerDelegate?

override init() {
        container = CKContainer.default()
        print(container.containerIdentifier)
        //this prints the name of the default container for the project
        publicDB = container.publicCloudDatabase
        privateDB = container.privateCloudDatabase
    }

I even went to the developer portal and made sure the container I want to use is the only container both apps are associated with. didn't work either.

My question is: How can I get two apps, obviously from the same developer account and provisioning profile, to work on one iCloud container?


Solution

  • You stated that you wish to work with a specific container with a specific identifier. But the code you have is specifically accessing the app's default container. That's not the one you want.

    Change:

    container = CKContainer.default()
    

    to:

    container = CKContainer(identifier: "your shared container id")
    

    For further details, read the documentation for the CKContainer class.