使用 Xcode 13 (iOS 15 SDK) 构建不再向后兼容 iOS 14.x 目标

Building with Xcode 13 (iOS 15 SDK) is not longer backward compatible with iOS 14.x targets

刚发现使用新的 iOS 15 SDK (Xcode 13 RC) 构建应用不再向后兼容 iOS 14.x 目标时使用CloudKit.

重现步骤:

  1. Xcode 13 RC 中的新项目(检查核心数据 + CloudKit 选项)

  2. 将 iOS 部署目标设置为 14.0

  3. 修改生成的Persistence.swift文件,添加私有CK存储如下:

    让 cloudStoreDescription = NSPersistentStoreDescription(url: url.appendingPathComponent("(privateCloudStoreName).sqlite")) cloudStoreDescription.configuration = "privateCkStoreName" var privateCloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "CKIDENTIFIFER") privateCloudKitContainerOptions.databaseScope = .private cloudStoreDescription.cloudKitContainerOptions = 私有CloudKit容器选项

  4. 运行 在 iPhone 上 iOS 15 -> 将工作

  5. 下载更多模拟器运行时并添加iOS14.5

  6. 运行 on iPhone with iOS 14.5 -> 将失败:

dyld: Library not loaded: /System/Library/Frameworks/_CoreData_CloudKit.framework/_CoreData_CloudKit Referenced from: /Users/andrei/Library/Developer/CoreSimulator/Devices/8554B734-4894-4DD0-A8FA-6C20983F3A49/data/Containers/Bundle/Application/1317D63A-F14B-4EA2-8F18-8EA623D358AB/testapp.app/testapp Reason: image not found

由于下周 iOS 15 将发布给一般 public,我该如何缓解这种情况?

可重现的例子here。 反馈小助手id:FB9630276

解决方案一直在我眼前:

只需删除以下行:

privateCloudKitContainerOptions.databaseScope = .private 

删除该行将具有与默认私有范围相同的效果,并且也将消除导入 CloudKit.

的需要

通过这样做,iOS 14.x

上不再发生崩溃

如果您想使用 .public 范围,根据 Apple Frameworks Enginner,修复如下:

let options = NSPersistentCloudKitContainerOptions(containerIdentifier: id)
options.setValue(1, forKey: "databaseScope") // .public == 1 // options.databaseScope = CKDatabase.Scope.public
description.cloudKitContainerOptions = options