Programmatically delete custom phone labels
Asked Answered
M

1

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: "[email protected]")
    newContact.emailAddresses = [homeEmail]

    do {
        let saveRequest = CNSaveRequest()
        saveRequest.addContact(newContact, toContainerWithIdentifier: nil)
        try contactStore.executeSaveRequest(saveRequest)
    }
    catch {
        NSLog("Save failed")
    }
}
Memorable answered 5/10, 2015 at 13:3 Comment(0)
P
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 !!

Pang answered 5/10, 2015 at 13:38 Comment(6)
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.Memorable
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 herePang
Yes, they are in the system. Wish there was a way to remove them. Thanks anyway.Memorable
I don't want to delete the custom label from the contact. I want to delete the custom label from the system.Memorable
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 itPang
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).Memorable

© 2022 - 2024 — McMap. All rights reserved.