如何访问与我认为自定义标签关系联系人数据相关的只读和/或可变联系人记录?
例如我有一个女婿,丈夫或儿子与联系人相关的自定义标签我是否需要CNLabeledValue CNLabelContactRelationChild?我需要阅读这些内容或从联系人数据中获取这些自定义标签?
答案 0 :(得分:2)
以下是我为了为联系人分配关系并能够使用关系
获取它而设法做的事情var myNewContact = CNMutableContact()
let myRelation = CNContactRelation(name: "mommy")
let myMom = CNLabeledValue(label: CNLabelContactRelationMother, value: myRelation)
myNewContact.contactRelations.append(myMom)
// add additional info to your contact such as name, email, family
// save your contact
let keysToFetch = [CNContactGivenNameKey, CNContactRelationsKey, CNContactEmailAddressesKey]
let text = "mommy"
let request = CNContactFetchRequest(keysToFetch: keysToFetch)
do {
try store.enumerateContactsWithFetchRequest(request) {
contact, stop in
for var i = 0; i < contact.contactRelations.count; i++ {
if (contact.contactRelations[i].valueForKey("value")?.valueForKey("name")!)! as? String == text
{
print(contact.givenName)
print(contact.identifier)
}
}
}
} catch let err{
print(err)
}
}