应用程序组的安全性应用程序组标识符返回 nil



我设置了一个应用程序组以与我的今日扩展一起使用。我在主应用的目标和扩展的目标中添加了应用组。在开发人员门户的应用 ID 配置中,我启用了应用组。但是出于某种原因,每当我调用它时,FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: id)都会返回 nil。我已经三倍检查了forSecurityApplicationGroupIdentifier字符串,知道它是正确的。

有谁知道为什么这不起作用?

编辑:我能够通过编辑我的调试权利以包含应用程序组来解决零问题。我一直在关注这篇文章,并且能够成功地从我的NSPersistentContainer迁移我的数据,但是当我尝试使用NSPersistentCloudKitContainer时,当用户打开iCloud时,这种方法不起作用。它仍然能够迁移数据,但不允许我在设备之间同步数据(几乎只是让它成为一个常规的NSPersistentContainer(。如果我恢复到我这样做的旧方式,我就可以使用 iCloud 同步。

任何人都可以帮助我在使用NSPersistentCloudKitContainer迁移以使用应用程序组时解决此同步问题吗?

核心数据代码:

class CoreDataManager {
static let sharedManager = CoreDataManager()
private init() {}
lazy var persistentContainer: NSPersistentContainer = {
var useCloudSync = UserDefaults.standard.bool(forKey: "useCloudSync")
//Get the correct container
let containerToUse: NSPersistentContainer?
if useCloudSync {
containerToUse = NSPersistentCloudKitContainer(name: "App")
} else {
containerToUse = NSPersistentContainer(name: "App")      
}
guard let container = containerToUse else {
fatalError("Couldn't get a container")
}
//Set the storeDescription
let storeURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.App")!.appendingPathComponent("(container.name).sqlite")
var defaultURL: URL?
if let storeDescription = container.persistentStoreDescriptions.first, let url = storeDescription.url {
defaultURL = FileManager.default.fileExists(atPath: url.path) ? url : nil
}
if defaultURL == nil {
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: storeURL)]
}

let description = container.persistentStoreDescriptions.first else {
fatalError("Hey Listen! ###(#function): Failed to retrieve a persistent store description.")
}
description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
if !useCloudSync {
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
//migrate from old url to use app groups
if let url = defaultURL, url.absoluteString != storeURL.absoluteString {
let coordinator = container.persistentStoreCoordinator
if let oldStore = coordinator.persistentStore(for: url) {
do {
try coordinator.migratePersistentStore(oldStore, to: storeURL, options: nil, withType: NSSQLiteStoreType)
} catch {
print("Hey Listen! Error migrating persistent store")
print(error.localizedDescription)
}
// delete old store
let fileCoordinator = NSFileCoordinator(filePresenter: nil)
fileCoordinator.coordinate(writingItemAt: url, options: .forDeleting, error: nil, byAccessor: { url in
do {
try FileManager.default.removeItem(at: url)
} catch {
print("Hey Listen! Error deleting old persistent store")
print(error.localizedDescription)
}
})
}
}
}
return container
}
}

我以为问题出在商店描述上,但是当我在更改商店描述之前和之后查看商店描述时,其选项仍然打开了iCloud同步。

我过去也有同样的问题。若要将FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: id)修复为 nil,只需确保用于调试的授权(可以在目标的"生成设置"中找到此授权(具有你创建的应用组。有时不会添加它。

当我在闪亮的新 M1 Mac 上编译我的项目(最初是在英特尔 MBP 上编写的,所有文件都是直接复制的(时,我在containerURL遇到了nil问题。解决方案似乎是我需要手动在权利中输入一个新组。

也许文件只是需要刷新....

编辑:它变得奇怪了!我发现containerURL(forSecurityApplicationGroupIdentifier: bundleID)对大写字母有点有趣(!什么时候

bundleID = "com.MyGroup.container"(或者,实际上,如果它都是小写的(,那么它返回一个可用的URL尽管该组是"com.MyGroup.Container"。

但是当

bundleID = "com.MyGroup.Container"(完全按照授权大写(,则返回nil

我现在开始使用:

containerURL(forSecurityApplicationGroupIdentifier: Bundle.main.bundleIdentifier!.lowercased())

最新更新