IOS9如何找到两个CNContact之间的差异?
我试图通过IOS9上的新框架联系人和ContactsUI来操纵联系人。我需要找出最终用户更改其信息(如电话号码或电子邮件)后的确切差异,也就是说,找出最终用户在其指定联系人的通讯簿上做了什么 如果联系人对每种物品(电话/电子邮件)都有不同的标签,这会更容易。但是,如果手机或电子邮件有相同的标签,例如: 约翰: 电话: 住址:123-456 办公室:223-456 办公室:456-334 电邮: 电邮:124@email.com 电邮:345@google.com 更改后,新信息可以是: 厕所: 电话: 住址:123-456 办公室:223-889(变更项目) 办公室:456-377(变更项目) 电邮: 电邮:1241111@email.com(更改项目) 电邮:3451111@google.com(更改项目) 使用ContactsUI,我可以捕获更改前和更改后的两个联系人对象 问题是,如何找到变化IOS9如何找到两个CNContact之间的差异?,ios,swift,contacts,Ios,Swift,Contacts,我试图通过IOS9上的新框架联系人和ContactsUI来操纵联系人。我需要找出最终用户更改其信息(如电话号码或电子邮件)后的确切差异,也就是说,找出最终用户在其指定联系人的通讯簿上做了什么 如果联系人对每种物品(电话/电子邮件)都有不同的标签,这会更容易。但是,如果手机或电子邮件有相同的标签,例如: 约翰: 电话: 住址:123-456 办公室:223-456 办公室:456-334 电邮: 电邮:124@email.com 电邮:345@google.com 更改后,新信息可以是: 厕所:
提前感谢。假设您知道如何遍历一系列新的
电话号码
和电子邮件
和旧邮件。您可以比较新旧联系人对象之间的标签和值对,并确定是否存在新的标签和值对
一些未经测试的代码:
func findOutContactDifference() {
// Assume you already have the two contact objects
let oldContact : CNContact = ...
let newContact : CNContact = ...
// Get list of phoneNumbers out
let oldPhoneNumbers = oldContact.phoneNumbers
let newPhoneNumbers = newContact.phoneNumbers
// Iterate through the new phoneNumbers array
for aNewPhoneNumber in newPhoneNumbers { // Type of aNewPhoneNumber will be CNLabeledValue
let newPhoneLabel = aNewPhoneNumber.label // This will be a string
let newPhoneNumber = aNewPhoneNumber.value as! CNPhoneNumber // You have to cast as specified in the document
let thisPhoneNumberAlreadyExisted = phoneNumberExisted(newPhoneLabel, newNumber : newPhoneNumber, oldPhoneNumbers : oldPhoneNumbers)
if (!thisPhoneNumberAlreadyExisted) {
// This is a new phone number!!
let brandNewNumber = newPhoneNumber
}
}
// Do the similar thing for email
// Beaware that cast for email.value will just be String
// ...
}
/// Will return whether the input pair already existed in the old list
func phoneNumberExisted(newLabel : String, newNumber : CNPhoneNumber, oldPhoneNumbers : [CNLabeledValue]) -> Bool {
for anOldPhoneNumber in oldPhoneNumbers {
let oldLabel = anOldPhoneNumber.label
let oldNumber = anOldPhoneNumber.value as! CNPhoneNumber
if oldLabel == newLabel && oldNumber == newLabel {
return true // Found a matched pair
}
}
return false // Nothing matched
}
==========
CNLabeledValue
value
属性的参考
联系人属性值,如电话号码的CNPhoneNumber、电子邮件地址的NSString等。有关有效值,请参见CNContact属性,这些属性是标记值对象的数组
编辑
CNLabeledValue
标识符的参考
属性
在重新获取的联系人中搜索以前已知的标记值对象时,建议使用标识符。该标识符可以在应用程序启动之间保持
查看属性并进行比较?您好zcui93,谢谢您的回复,但在某些情况下,标签总是相同的,例如从outlook.com帐户导入的联系人。因此,标签+数字对不能很好地工作。比较所有组合将确保您不会错过任何匹配。我们使用配对的原因是标签本身不起作用。你可以举一些你认为不起作用的例子。在问题中给出的示例中,“home:123-456”将被标识为未更改,而所有其他项将被标识为已更改。您是对的,使用您的代码我们可以找到更改的项。但对于进一步的信息,例如,如果我需要跟踪更改,从哪个数字更改为另一个数字,则获取它要困难得多,因为有重复的标签。我说的对吗?使用IOS,人们可以添加新电话号码、删除或更改电话标签。我几乎疯狂地捕捉这些变化。。。有什么想法吗?也许我将不得不制作我自己的UI…您将通过比较对来捕获所有这些更改。怎么了/