I want to save contact directly to an iOS device's Address Book programmatically.
How can I do it?
Add a new field: Click the Add Field pop-up menu at the bottom of the contact card, then choose a type of field. Add another address: Click Add New Address below the address fields.
Tap the Phone app to open it and tap the Contacts icon or launch the Contacts app from the home screen. Browse your contacts or enter a name in the search bar at the top of the screen.
Select the iPhone contact you wish to edit and click Edit Contact. Alternatively, right-click on a contact and choose Edit or double-click on the contact entry. You can now edit all contact details such as phone numbers, emails, address, etc. If you want to add photos, it is twice easier to do so on a PC.
Here is a small example :
CFErrorRef error = NULL;
NSLog(@"%@", [self description]);
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef newPerson = ABPersonCreate();
ABRecordSetValue(newPerson, kABPersonFirstNameProperty, people.firstname, &error);
ABRecordSetValue(newPerson, kABPersonLastNameProperty, people.lastname, &error);
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiPhone, people.phone, kABPersonPhoneMainLabel, NULL);
ABMultiValueAddValueAndLabel(multiPhone, people.other, kABOtherLabel, NULL);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
CFRelease(multiPhone);
// ...
// Set other properties
// ...
ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
ABAddressBookSave(iPhoneAddressBook, &error);
CFRelease(newPerson);
CFRelease(iPhoneAddressBook);
if (error != NULL)
{
CFStringRef errorDesc = CFErrorCopyDescription(error);
NSLog(@"Contact not saved: %@", errorDesc);
CFRelease(errorDesc);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With