Well, the answer was simple and yet hard to find. It seems when you convert a Core Data app to work with CloudKit you need to add CloudKit.framework to the Frameworks, Libraries, and Embedded Content section. without it, the app will work when running in debug mode (via Xcode) but once it’s in production, signed and notarized, it won’t. Even if you have the right entitlements, all pointing to production, etc.
翻译一下:
嗯,答案很简单,但很难找到。似乎当您将 Core Data 应用程序转换为与 CloudKit 一起使用时,您需要添加 CloudKit.framework 到 Frameworks, Libraries, and Embedded Content 部分。如果没有它,应用程序将在调试模式下运行时(通过 Xcode)工作,但一旦它投入生产、签名和公证,它就不会工作。即使您拥有正确的权利,所有权利都指向生产等。
选择你的 Targets,第一个 General Tab 里面有个 Frameworks, Libraries, and Embedded Content,点击 ➕ 添加 CloudKit.framework,Embed 选择 Do Not Embed
报错的日志
error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(1242): <NSCloudKitMirroringDelegate: 0x60000377c000>: Failed to set up CloudKit integration for store: <NSSQLCore: 0x13dc04080> (URL: file:///Users/admin/Library/Containers/xxx) <CKError 0x60000064adf0: "Service Unavailable" (6/NSCocoaErrorDomain:4099); "Error connecting to CloudKit daemon. This could happen for many reasons, for example a daemon exit, a device reboot, a race with the connection inactivity monitor, invalid entitlements, and more. Check the logs around this time to investigate the cause of this error."; Retry after 5.0 seconds>
error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate recoverFromError:](2312): <NSCloudKitMirroringDelegate: 0x60000377c000> - Attempting recovery from error: <CKError 0x60000064adf0: "Service Unavailable" (6/NSCocoaErrorDomain:4099); "Error connecting to CloudKit daemon. This could happen for many reasons, for example a daemon exit, a device reboot, a race with the connection inactivity monitor, invalid entitlements, and more. Check the logs around this time to investigate the cause of this error."; Retry after 5.0 seconds>
Error retrieving daemon to get network transfer endpoint: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.apple.cloudd was invalidated: failed at lookup with error 159 - Sandbox restriction." UserInfo={NSDebugDescription=The connection to service named com.apple.cloudd was invalidated: failed at lookup with error 159 - Sandbox restriction.}
Error getting network transfer endpoint: <CKError 0x600000600990: "Service Unavailable" (6/NSCocoaErrorDomain:4099); "Error connecting to CloudKit daemon. This could happen for many reasons, for example a daemon exit, a device reboot, a race with the connection inactivity monitor, invalid entitlements, and more. Check the logs around this time to investigate the cause of this error."; Retry after 5.0 seconds>
error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate resetAfterError:andKeepContainer:](612): <NSCloudKitMirroringDelegate: 0x60000377c000> - resetting internal state after error: <CKError 0x60000064adf0: "Service Unavailable" (6/NSCocoaErrorDomain:4099); "Error connecting to CloudKit daemon. This could happen for many reasons, for example a daemon exit, a device reboot, a race with the connection inactivity monitor, invalid entitlements, and more. Check the logs around this time to investigate the cause of this error."; Retry after 5.0 seconds>
不过他的解决办法不是上面提到的 CloudKit.framework 而是 com.apple.developer.icloud-container-environment 这个属性写的 Development 导致上线后出现了问题,改成 Production 就好了。我感觉这个配置还挺好用的,我就直接固定成 Production 了,这样开发环境也直接连接线上数据库,反正都是连接我个人的 iCloud 不影响其他用户的数据。