CloudKit is not accessible from iOS extension targets

Hi!

I'm using CoreData + CloudKit. It works well both on macOS and iOS, however, I can't make it work with extensions (share, action, keyboard).

I get Invalid bundle ID for container error:

<CKSchedulerActivity: 0x3029f4d20; identifier=com.apple.coredata.cloudkit.activity.export.A65D5B7A-18AA-400A-B25F-F042E46646F6, priority=2, container=iCloud.com.org.app.dev:Sandbox, relatedApplications=(
    "com.org.App.dev.App-Keyboard"
), xpcActivityCriteriaOverrides={
    ActivityGroupName = "com.apple.coredata.cloudkit.App Keyboard.A65D5B7A-18AA-400A-B25F-F042E46646F6";
    Delay = 0;
    Priority = Utility;
}>
error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _recoverFromPartialError:forStore:inMonitor:](2812): <NSCloudKitMirroringDelegate: 0x303fd82d0>: Error recovery failed because the following fatal errors were found: {
    "<CKRecordZoneID: 0x300ef9bc0; zoneName=com.apple.coredata.cloudkit.zone, ownerName=__defaultOwner__>" = "<CKError 0x300efa5e0: \"Permission Failure\" (10/2007); server message = \"Invalid bundle ID for container\"; op = xxxxxxx; uuid = zzzzz-xxxxx; container ID = \"iCloud.com.org.app.dev\">";
}

I checked everything 10x: profiles, bundle ids, entitlements, etc. I even removed all local provisioning profiles and recreated them, I also tried setting different CloudKit container, but nothing helps. I tested it on a real device.

My setup:

main app bundle id: com.org.App.dev
keyboard bundle id: com.org.App.dev.App-Keyboard
action extension bundle id: com.org.App.dev.Action-Extension
CloudKit container id: iCloud.com.org.app.dev

I keep the CoreData database in the app group container, but I also tried locally and it doesn't really matter.

This is how I setup my CoreData:

self.persistentContainer = NSPersistentCloudKitContainer(name: "AppCoreModel")

        persistentContainer.persistentStoreDescriptions = [createCloudStoreDescription()]
        persistentContainer.loadPersistentStores { [self] _, error in
            if let error {
                logError("Could not load Core Data store \(error)")
            } else {
                persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
                persistentContainer.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
                logDebug(persistentContainer.persistentStoreDescriptions.first?.url?.absoluteString ?? "")
                logDebug("Core Data store loaded")
            }
        }

    private func createCloudStoreDescription() -> NSPersistentStoreDescription {
        let cloudStoreOptions = NSPersistentCloudKitContainerOptions(
            containerIdentifier: "iCloud.com.org.app.dev"
        )

        cloudStoreOptions.databaseScope = .private

        let documentsUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: AppConstants.appGroupId)!
        let cloudStoreDescription = NSPersistentStoreDescription(
            url: documentsUrl.appendingPathComponent("cloud-database.sqlite")
        )
        cloudStoreDescription.type = NSSQLiteStoreType
        cloudStoreDescription.cloudKitContainerOptions = cloudStoreOptions
        cloudStoreDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
        cloudStoreDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)

        return cloudStoreDescription
    }

Any help would be highly appreciated. It seems like iOS bug, because everything seems to be configured properly. I even checked app identifiers if containers are properly assigned.

Similar issue when using CloudKit directly (unresolved): https://developer.apple.com/forums/thread/665280

Weird, suddenly I stopped receiving this error. Maybe it required a couple of hours to propagate some information, not sure.

Nevertheless, it seems like CloudKit synchronization is not stable on Keyboard Extension. Sometimes it works, but I guess, the only reason why it works is because underneath the main app starts in the background. After a minute or so, the keyboard stops synchronizing changes with Cloud and I have to start the main app to synchronize. Not even restarting the keyboard helps.

Thank you for the follow up. Please reply here if the issue re-emerges.

I saw that you were trying to use NSPersistentCloudKitContainer in an extension, and would like to give you a warning that it may not work that well. This topic is discussed a bit in the following technote:

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

CloudKit is not accessible from iOS extension targets
 
 
Q