ios - SecurityApplicationGroupIdentifier 的应用组返回 ni

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

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

编辑:我能够通过编辑我的调试权利以包括应用程序组来解决 nil 问题。我一直在关注这个post并且能够成功地从我的 NSPersistentContainer 迁移我的数据,但是当我尝试在用户打开 iCloud 时使用 NSPersistentCloudKitContainer 时,同样的方法不起作用。它仍然能够迁移数据,但不允许我在设备之间同步我的数据(几乎只是让它成为一个常规的 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,只需确保用于调试的权利(您可以在目标的build设置中找到它)具有应用程序组你做的。有时不添加。

关于ios - SecurityApplicationGroupIdentifier 的应用组返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61845865/

相关文章:

python - 生成最多 2 个重复的 3 个数字的列表

scala - 通过方法类型参数分配给类型成员的值打破了类型等价

c - 使用 for 循环初始化一个数组,并将最终数组元素的值作为条件(在 C 中)

python - 将 Selenium 与 PyCharm CE 一起使用时的弃用问题

swift - 在 Vapor 中使用原始 sql 返回条目总数

php - 如何解决 "The process has been signaled with sig

typescript - 如何使用可选的唯一字符串定义数组 typescript 接口(interf

swiftui - 我怎样才能在 SwiftUI 中实现类似于 radiogroup 的一种可能选择

react-native - React 导航 5(导航到另一个堆栈屏幕)

forms - 如何判断表单字段在 vue.js 中是否无效