I would like to add the ability to save a contact (phone number, name, address and email) in the iOS address book by clicking on a button in my application! How can I do this?
Asked
Active
Viewed 1.3k times
3 Answers
15
Its so easy and follows below steps,
First, you have import following frameworks,
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h
Second, write the following code in your button action and modify the code according to your needs,
NSString * addressString1 = [appDelegate getCurrentSummary];
NSString * addressString2 = [appDelegate getCurrentTubeImage];
NSString * cityName = [appDelegate getCurrentcheckValue];
NSString * stateName = [appDelegate getCurrentTubeUrl];
NSString * postal = [appDelegate getCurrentViews];
NSString * emailString = [appDelegate getCurrentLink];
NSString * phoneNumber = [appDelegate getCurrentTitle];
NSString * prefName = [appDelegate getCurrentAuthor];
ABAddressBookRef libroDirec = ABAddressBookCreate();
ABRecordRef persona = ABPersonCreate();
ABRecordSetValue(persona, kABPersonFirstNameProperty, prefName, nil);
ABMutableMultiValueRef multiHome = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
NSString *homeStreetAddress=[addressString1 stringByAppendingString:addressString2];
[addressDictionary setObject:homeStreetAddress forKey:(NSString *) kABPersonAddressStreetKey];
[addressDictionary setObject:cityName forKey:(NSString *)kABPersonAddressCityKey];
[addressDictionary setObject:stateName forKey:(NSString *)kABPersonAddressStateKey];
[addressDictionary setObject:postal forKey:(NSString *)kABPersonAddressZIPKey];
bool didAddHome = ABMultiValueAddValueAndLabel(multiHome, addressDictionary, kABHomeLabel, NULL);
if(didAddHome)
{
ABRecordSetValue(persona, kABPersonAddressProperty, multiHome, NULL);
NSLog(@"Address saved.....");
}
[addressDictionary release];
//##############################################################################
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
bool didAddPhone = ABMultiValueAddValueAndLabel(multiPhone, phoneNumber, kABPersonPhoneMobileLabel, NULL);
if(didAddPhone){
ABRecordSetValue(persona, kABPersonPhoneProperty, multiPhone,nil);
NSLog(@"Phone Number saved......");
}
CFRelease(multiPhone);
//##############################################################################
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
bool didAddEmail = ABMultiValueAddValueAndLabel(emailMultiValue, emailString, kABOtherLabel, NULL);
if(didAddEmail){
ABRecordSetValue(persona, kABPersonEmailProperty, emailMultiValue, nil);
NSLog(@"Email saved......");
}
CFRelease(emailMultiValue);
//##############################################################################
ABAddressBookAddRecord(libroDirec, persona, nil);
CFRelease(persona);
ABAddressBookSave(libroDirec, nil);
CFRelease(libroDirec);
NSString * errorString = [NSString stringWithFormat:@"Information are saved into Contact"];
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"New Contact Info" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
I hope it will help you!

Sivanathan
- 1,213
- 3
- 13
- 25
-
Thank you very much, it work great!!! I wanted ask you just another thing, how can I do to set also the image of the contact??? – Claudio Jul 26 '10 at 13:05
-
1@Claudio follow the link, it has solution http://www.iphonedevsdk.com/forum/iphone-sdk-development/16910-how-add-image-contact.html – Sivanathan Jul 26 '10 at 13:18
14
Here is Code to Add Contact with Person Image in Iphone Address Book
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate();
// Add Person Image
NSData *dataRef = UIImagePNGRepresentation(personImageView.image);
ABPersonSetImageData(person, (CFDataRef)dataRef, nil);
CFErrorRef anError = NULL;
// Full Name, Company Name, Designation Property
ABRecordSetValue(person,kABPersonFirstNameProperty,full_name,&anError);
ABRecordSetValue(person, kABPersonOrganizationProperty, companyLabel, &anError);
ABRecordSetValue(person, kABPersonJobTitleProperty, designation, &anError);
// Multi value Address Property
ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);'
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
[addressDictionary setObject:@"Chicago" forKey:(NSString *)kABPersonAddressCityKey];
[addressDictionary setObject:@"IL" forKey:(NSString *)kABPersonAddressStateKey];
[addressDictionary setObject:@"60654" forKey:(NSString *)kABPersonAddressZIPKey];
ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, (CFStringRef)@"Address", NULL);
ABRecordSetValue(person, kABPersonAddressProperty, multiAddress,&anError);
CFRelease(multiAddress);
ABMutableMultiValueRef websiteMultiValue = ABMultiValueCreateMutable(kABPersonURLProperty);
ABMultiValueAddValueAndLabel(websiteMultiValue, web_URL, (CFStringRef)@"Website", NULL);
ABRecordSetValue(person, kABPersonURLProperty, websiteMultiValue, &anError);
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
ABMultiValueAddValueAndLabel(emailMultiValue, email, (CFStringRef)@"Email", NULL);
ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, &anError);
ABAddressBookAddRecord(addressBook, person, &anError);

Shahab Qureshi
- 952
- 1
- 7
- 19
1
If anyone is looking for the way to add address details (street, city, state, zip) in Swift, it's not well documented on Apple's web site. Here's the way I did it (only address details shown in the example)...
var addressDictionary: [String:String] = [
kABPersonAddressStreetKey as String: Contact.address_1,
kABPersonAddressCityKey as String: Contact.city,
kABPersonAddressStateKey as String: Contact.state,
kABPersonAddressZIPKey as String: Contact.zip
]
let multiWork: ABMutableMultiValue =
ABMultiValueCreateMutable(
ABPropertyType(kABMultiDictionaryPropertyType)).takeRetainedValue()
ABMultiValueAddValueAndLabel(multiWork, addressDictionary, kABWorkLabel, nil)
ABRecordSetValue(person, kABPersonAddressProperty, multiWork, nil)

Justin Domnitz
- 3,217
- 27
- 34