在我的应用程序中,我想创建一个新的联系人。如果已存在同名联系人,我希望将新联系人链接到旧联系人。
我已经查看了CNContact和CNContactStore引用,但没有找到任何链接联系人的方法。这是可能的吗?如果可能,是如何实现的?
发布于 2016-07-22 17:42:47
在IOS9中,代表同一人的不同帐户中的联系人可能会自动链接在一起。
要实现这一点,您应该确保新插入的联系人的名称与您希望与之统一的联系人的名称相匹配。
下面链接的文档给出了iCloud和Facebook上的"John Appleseed“的例子。
发布于 2017-09-23 21:05:07
下面的代码用于将联系人与contact store.Just中已经存在的联系人合并,这样唯一的值将被替换,而数字、电子邮件、地址等数组将与现有值一起追加。干杯!!
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
picker.dismiss(animated: true, completion: nil)
let identifier = contact.identifier
updateContact(contactIdentifier: identifier)
}
func updateContact(contactIdentifier: String){
let keysToFetch = [CNContactViewController.descriptorForRequiredKeys()]
let contactStore = CNContactStore()
do {
let contactToUpdate = try contactStore.unifiedContact(withIdentifier: contactIdentifier, keysToFetch: keysToFetch).mutableCopy() as! CNMutableContact
if contactToUpdate.familyName.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
contactToUpdate.familyName = "your value"
}
if contactToUpdate.givenName.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
contactToUpdate.givenName = "your value"
}
if contactToUpdate.organizationName.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
contactToUpdate.organizationName = "your value"
}
if contactToUpdate.jobTitle.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
contactToUpdate.jobTitle = "your value"
}
// here the contact used below is the one that you want to merge with
an existing one.
for i in contact.phoneNumbers {
contactToUpdate.phoneNumbers.append(i)
}
for i in contact.emailAddresses {
contactToUpdate.emailAddresses.append(i)
}
for i in contact.postalAddresses {
contactToUpdate.postalAddresses.append(i)
}
let contactsViewController = CNContactViewController(forNewContact: contactToUpdate)
contactsViewController.delegate = self
contactsViewController.title = "Edit contact"
contactsViewController.contactStore = contactStore
let nav = UINavigationController(rootViewController: contactsViewController)
DispatchQueue.main.async {
self.present(nav, animated: true, completion: nil)
}
}
catch {
print(error.localizedDescription)
}
}发布于 2021-02-25 23:07:09
我不能确定这一点,但我怀疑iOS只能跨不同的来源合并。如果您在同一通讯簿中创建两个联系人,它们将不会被合并。
在Contacts库中对此区域的支持很少。您可以使用CNContact.isUnifiedWithContact(withIdentifier:),检测一个联系人是否统一并链接到另一个特定联系人,您可以决定是否返回统一的联系人。
https://stackoverflow.com/questions/30944065
复制相似问题