与联系人IOS OS X联系框架Swift关联的自定义标签

时间:2016-02-19 03:51:40

标签: ios swift macos contacts cncontact

如何访问与我认为自定义标签关系联系人数据相关的只读和/或可变联系人记录?

例如我有一个女婿,丈夫或儿子与联系人相关的自定义标签我是否需要CNLabeledValue CNLabelContactRelationChild?我需要阅读这些内容或从联系人数据中获取这些自定义标签?

1 个答案:

答案 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)
        }
    }