Oct
06
2009

Add contact to Address Book using iPhone SDK

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...



Below is the code for adding a contact to iPhone’s Address Book using iPhone SDK.


ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate();

ABRecordSetValue(person, kABPersonFirstNameProperty, @"Sudha" , nil);
ABRecordSetValue(person, kABPersonLastNameProperty, @"Reddy", nil);

ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, @"1234567890", kABHomeLabel, nil);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, @"9876543210", kABPersonPhoneMobileLabel, nil);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, @"04022222222", kABWorkLabel, nil);
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);

ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
ABMultiValueAddValueAndLabel(emailMultiValue, @"whatever@gmail.com", kABWorkLabel, nil);
ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects: [NSArray arrayWithObjects:@"1234 Whatever Street", @"Hyderabad", @"AP", @"INDIA", @"500008", nil] forKeys: [NSArray arrayWithObjects: (NSString *)kABPersonAddressStreetKey, (NSString *)kABPersonAddressCityKey, (NSString *)kABPersonAddressStateKey, (NSString *)kABPersonAddressCountryKey, (NSString *)kABPersonAddressZIPKey, nil]];
ABMultiValueRef addressMultiValue = ABMultiValueCreateMutable(kABDictionaryPropertyType);
ABMultiValueAddValueAndLabel(addressMultiValue, (CFDictionaryRef *)dictionary, kABWorkLabel, nil);
ABRecordSetValue(person, kABPersonAddressProperty, addressMultiValue, nil); ABRecordCopyValue(person, kABPersonAddressProperty);

ABAddressBookAddRecord(addressBook, person, nil);
ABAddressBookSave(addressBook, nil);

CFRelease(person);
CFRelease(addressMultiValue);
CFRelease(phoneNumberMultiValue);
CFRelease(emailMultiValue);
[dictionary release];
 

tags: ,
posted in iPhone SDK by Sudha C Reddi

Follow comments via the RSS Feed | Leave a comment | Trackback URL

1 Comment to "Add contact to Address Book using iPhone SDK"

  1. satish wrote:

    good one…i had one question, how will you handle if u try to add a contact but that contact is already present

Leave Your Comment