How to programmatically add a "custom label" to the iOS address book?

When manually adding a contact phone / IMS in the iOS address book, you can add a custom label instead of "Home", "Work", "Other" * (in IMS).

How to create a "custom label" in the address book programmatically?

+6
iphone label addressbook
source share
1 answer

I had the same question. I could not find the answer, so I just tried the method of guessing and checking. The following code seems to work:

CFErrorRef error = NULL; ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate(); ABRecordRef newPerson = ABPersonCreate(); ABRecordSetValue(newPerson, kABPersonFirstNameProperty, @"Jane", &error); ABRecordSetValue(newPerson, kABPersonLastNameProperty, @"Smith", &error); const CFStringRef customLabel = CFSTR( "mylabel" ); //phone ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(multiPhone, @"1-444-444-444", kABPersonPhoneMainLabel, NULL); ABMultiValueAddValueAndLabel(multiPhone, @"1-333-333-333", kABPersonPhoneMobileLabel, NULL); ABMultiValueAddValueAndLabel(multiPhone, @"1-666-666-666", kABOtherLabel, NULL); ABMultiValueAddValueAndLabel(multiPhone, @"1-555-555-555", customLabel, NULL); ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil); CFRelease(multiPhone); ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error); ABAddressBookSave(iPhoneAddressBook, &error); if (error != NULL) { NSLog(@"Error!"); } 

If you check the address book, you will see a phone number with a custom label: mylabel

Thank you: this post

And before: this blog

+11
source share

All Articles