0

我有一个 iOS 应用程序可以访问联系人存储的默认容器并检索该容器中的组。在我退出安装了该应用程序的 iPhone 8 设备后,然后使用另一个帐户登录。然后注销并使用我的原始帐户登录,该应用程序不再能够从默认容器中检索组。

为什么要这样做,我该如何解决?

以下是相关代码:

cnGroups = try contactStore.groups(matching: CNGroup.predicateForGroupsInContainer(withIdentifier: contactStore.defaultContainerIdentifier()))

print("cnGroups...")
print(cnGroups)

这是调试窗口输出:

cnGroups...
[]

我在任何类之外的 swift 文件中全局初始化了联系人存储:

internal var contactStore = CNContactStore()

这个请求用户访问联系人的代码在 viewDidLoad() 回调方法中。initUTIGroups() 方法是我尝试从默认容器中检索组的地方。

contactStore.requestAccess(for: .contacts) {

    permissionGranted, error in

    if error != nil {

        print(error?.localizedDescription as Any)

    }

    if permissionGranted {

        self.initUTIGroups()

    } else {

        let alertMessage = "\(displayName) will not work unless you allow access to Contacts. Please go to Settings and allow access to Contacts then restart \(displayName)"

        let alert = UIAlertController(title: nil, message: alertMessage, preferredStyle: .alert)

        let actionOK = UIAlertAction(title: "OK", style: .cancel, handler: nil)

        alert.addAction(actionOK)

        self.present(alert, animated: true, completion: nil)

    }

}

我检索了联系人存储中的所有容器,并将它们与默认容器进行了比较。看起来当前的默认容器与我第一次退出 iCloud 之前的默认容器不是同一个容器。我找到了包含组的容器,这些组与我在第一次退出 iCloud 之前从默认容器中检索到的组相同。

是否有我可以编写的代码来防止问题或在问题发生后解决问题?

4

0 回答 0