1

How do I access the read-only and/or mutable contact records pertaining to what I believe to be custom labeled relationship contact data?

For instance I have Daughter-In-Law, Husband or Son custom labels associated with a contact Do I need CNLabeledValue CNLabelContactRelationChild? What do I need to read these or get these custom labels from contact data?

00Spitfire
  • 13
  • 5

1 Answers1

2

Here is what I managed to do in order to assign a relation to a contact and be able to fetch it using the relation

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)
        }
    }
nadi9
  • 576
  • 3
  • 11
  • not sure yet, I am hung up on another slew of bugs, I've been reading and this looks real good to me, but I have not had a chance to test this yet. I will try to test soon. Thank you so much for time and help. I really appreciate it, and will let you know after I get to test this part. – 00Spitfire May 27 '16 at 03:57
  • Thank you so so much. This totally works, I just updated it to swift 3 etc. – 00Spitfire Sep 09 '16 at 03:18