1

In iOS it is possible to create custom labels for phone numbers and email addresses. Is there a way to remove those created labels programatically (either with CNContacts or ABAddressBook)? In other words: I don't want to delete the custom label from a contact, I want to delete the "custom label" from the system so it doesn't show up at all when someone brings up the available available list.

Attached iOS 9 source code that creates a contact in the phone book with custom labels on the email field.

func createContact() {

    let contactStore = CNContactStore()
    let newContact = CNMutableContact()

    newContact.givenName = "Chris"
    newContact.familyName = "Last"

    let homeEmail = CNLabeledValue(label: "RandomLabel", value: "IGotAnEmail@Address.com")
    newContact.emailAddresses = [homeEmail]

    do {
        let saveRequest = CNSaveRequest()
        saveRequest.addContact(newContact, toContainerWithIdentifier: nil)
        try contactStore.executeSaveRequest(saveRequest)
    }
    catch {
        NSLog("Save failed")
    }
}
hashier
  • 4,670
  • 1
  • 28
  • 41

1 Answers1

0

Contact Framework + deleteContact

This might help you.

Using this function

EDIT: I'm in a good day:

NSOperationQueue().addOperationWithBlock{[unowned store] in
  let predicate = CNContact.predicateForContactsMatchingName("john")
  let toFetch = [CNContactEmailAddressesKey]

  do{

    let contacts = try store.unifiedContactsMatchingPredicate(predicate,
      keysToFetch: toFetch)

    guard contacts.count > 0 else{
      print("No contacts found")
      return
    }

    //only do this to the first contact matching our criteria
    guard let contact = contacts.first else{
      return
    }

    let req = CNSaveRequest()
    let mutableContact = contact.mutableCopy() as! CNMutableContact
    req.deleteContact(mutableContact)

    do{
      try store.executeSaveRequest(req)
      print("Successfully deleted the user")

    } catch let e{
      print("Error = \(e)")
    }

  } catch let err{
    print(err)
  }
}

EDIT: It seems you can but you need to do batch function like this:

  • fetch contacts using AddressBook/ABAddressBookCopyArrayOfAllPeople
  • For...in into contacts
  • get the ABRecordCopyValue to get the ABMultiValueRef you want
    • kABPersonEmailProperty
    • kABPersonAddressProperty
    • kABPersonPhoneProperty
  • For...in into them
  • Get the current with ABMultiValueCopyLabelAtIndex
  • Compare it to the default labels (note here to get the default one)
  • if no match, delete it

Hope it helps you

EDIT 2: Meh, ABAddressBook is deprecated, you need to do the same with New contact framework ... Have fun !!

jlngdt
  • 1,350
  • 9
  • 14
  • That deletes a contact in the phone book. That's not what I'm looking for. I want to delete a custom label from the system not a contact or change the fields of a user in the phone book. – hashier Oct 05 '15 at 14:21
  • Oh my bad i didn't read correctly. Not sure you can remove it: It looks like even after your uninstall an app that add custom label, they stay here – jlngdt Oct 05 '15 at 14:28
  • Yes, they are in the system. Wish there was a way to remove them. Thanks anyway. – hashier Oct 05 '15 at 14:31
  • I don't want to delete the custom label from the contact. I want to delete the custom label from the system. – hashier Oct 06 '15 at 09:33
  • 1
    yep, but to do this, you HAVE to do it for all the contact. Because custom labels appears if and only if one or more contact use it – jlngdt Oct 06 '15 at 09:34
  • OOOO! THAT is the answer to my question... Remove all the code and just say that in the answer and I'll accept it. The code I can write myself. I just didn't know that they disappear if you remove all custom labels from contacts. (And I'll implement it in ABAddressBook anyway, since CNContacts is iOS 9 only). – hashier Oct 06 '15 at 10:14