CNContact:从 ABAddressBook 迁移:iOSLegacyIdentifier + lastModifcationDate

CNContact :migration from ABAddressBook : iOSLegacyIdentifier + lastModifcationDate

我有一个存储所有本地用户联系人的数据库。 现在我想使用新的框架(联系框架),我的问题是 CNContact 现在有一个新的标识符(不再是自动增量的)称为 "identifier" 并且我无法在我的数据库中处理旧条目可能会更新联系人。

我有两个问题:

  1. 在 xcode 调试器中,我可以在 CNContact 中看到 _iOSLegacyIdentifier(旧的自动增量标识符)作为 属性,我如何在没有私人 API 调用的情况下获取它
  2. 我看不到 "lastModifcationDate" CNContact(在 ABAddressBook 框架中它被称为 kABPersonModificationDateProperty)我如何使用新框架获得它。

谢谢。

[编辑]:我已经为 Apple 开了一张票,这是答案:

There are no plans to address this based on the following:

1) iOSLegacyIdentifier is private API for CNContact. 2) A modification date is not offered on CNContact.

To migrate your DB you can to match contacts by name and disambiguate by manually matching other properties like email addresses or phone numbers.

We are now closing this report.

如您所见,没有真正的解决方案,我们不得不猜测..

1.Doesn不存在。只有私有选择器

[CNContact iOSLegacyIdentifier];

或者你可以得到相同的

[CNContainer CNContainerIOSLegacyIdentifierKey];

请注意,这不是在框架中编译的。使用执行选择器

2.There 在新框架中没有这样的 属性。如果反汇编 Contact 框架,您会发现 uniqueId 仍用于涉及底层核心数据的谓词中。但这对你来说是一项工作,并再次与私人选择者共舞

(怪苹果,不是我没办法)。看一下框架的内部结构。

您可以使用新的 Contact Framework 获取 ContactID (iOSLegacyIdentifier)。我在我的应用程序中使用它来查找特定联系人的 iOSLegacyIdentifier,您可以随意修改。

let predicate = CNContact.predicateForContacts(matchingName: "contactName")
        let toFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactIdentifierKey]

        do{
            let contacts = try self.contactStore.unifiedContacts(matching: predicate, keysToFetch: toFetch as [CNKeyDescriptor])

            for contact in contacts{
                var diccionario:[String : Any] = contact.dictionaryWithValues(forKeys: ["iOSLegacyIdentifier"])
                //With this you can see/obtain iOSLegacyIdentifier
                print(diccionario["iOSLegacyIdentifier"] as! Int)
                return;
            }
        } catch let err{
            print(err)
        }