Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new contact to iOS Contacts (Address Book)?

I want to save contact directly to an iOS device's Address Book programmatically.

How can I do it?

like image 585
Chatar Veer Suthar Avatar asked Mar 22 '11 10:03

Chatar Veer Suthar


People also ask

How do I add an address to Apple Contacts?

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.

Where do I find my address book on my iPhone?

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.

How do I edit my address book on iPhone?

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.


1 Answers

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);        
}
like image 167
j_freyre Avatar answered Sep 20 '22 18:09

j_freyre