ios - SecurityApplicationGroupIdentifier 的应用组返回 nil

标签 ios swift xcode ios-app-group

我设置了一个应用程序组以与我的 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/

相关文章:

ios - Parse 集成到 Swift 应用程序时出错

ios facebook 登录 - 无法连接到服务器

ios - 用于在 Tableview 中传递数据的 IF 语句 && Swift

swift - 注册 AWSS3 配置造成 5000 多次内存泄漏

ios - iCloud 打开/关闭错误

iphone - 从 ALAssset 检索时的 CGImage 方向问题

ios - Apple Mach - O 链接器错误。没有这样的文件或目录 Swifty Json

android - 适用于 Android/iOS 的 WMS View /库

swift - 从另一个函数中调用一个函数并两次传递相同的参数是不好的做法吗? - swift 3

objective-c - XC测试用例 : ld: symbol(s) not found for architecture x86_64