From the accepted answer here :
- (void) addSingleContact:(NSDictionary*)contact {
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied ||
ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusRestricted){
UIAlertView *cantAddContactAlert = [[UIAlertView alloc] initWithTitle:@"Cannot Add Contact"
message:@"You must give the app permission to add the contact first."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[cantAddContactAlert show];
} else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
// create person record
ABRecordRef person = ABPersonCreate();
// set name and other string values
NSString *firstName = [contact objectForKey:@"firstName"];
NSString *lastName = [contact objectForKey:@"lastName"];
NSString *organization = [contact objectForKey:@"company"];
NSString *designation = [contact objectForKey:@"designation"];
NSString *personEmail = [contact objectForKey:@"email"];
NSString *phoneNo = [contact objectForKey:@"phone"];
NSString *mobileNo = [contact objectForKey:@"mobile"];
NSString *webURL = [contact objectForKey:@"website"];
NSString *address = [contact objectForKey:@"address"];
ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFStringRef)organization, NULL);
ABRecordSetValue(person, kABPersonJobTitleProperty, (__bridge CFStringRef)designation, NULL);
if (webURL.length) {
ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef) webURL, kABPersonHomePageLabel, NULL);
ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, nil);
CFRelease(urlMultiValue);
}
if (personEmail.length) {
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) personEmail, kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
CFRelease(emailMultiValue);
}
//Add numbers ()
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
NSArray *venuePhoneNumbers;
if (phoneNo.length) {
venuePhoneNumbers = [phoneNo componentsSeparatedByString:@" or "];
for (NSString *venuePhoneNumberString in venuePhoneNumbers)
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) venuePhoneNumberString, kABPersonPhoneMainLabel, NULL);
}
if (mobileNo.length) {
venuePhoneNumbers = [mobileNo componentsSeparatedByString:@" or "];
for (NSString *venuePhoneNumberString in venuePhoneNumbers)
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) venuePhoneNumberString, kABPersonPhoneMobileLabel, NULL);
}
if (faxNo.length) {
venuePhoneNumbers = [faxNo componentsSeparatedByString:@" or "];
for (NSString *venuePhoneNumberString in venuePhoneNumbers)
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) venuePhoneNumberString, kABPersonPhoneWorkFAXLabel, NULL);
}
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, NULL);
CFRelease(phoneNumberMultiValue);
// add address
ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
if (address) {
addressDictionary[(NSString *) kABPersonAddressStreetKey] = [NSString stringWithFormat:@"%@", address];
}
ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)(firstName) , nil);
ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)(lastName), nil);
ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonAddressProperty, multiAddress, NULL);
CFRelease(multiAddress);
ABAddressBookAddRecord(addressBook, person, &error);
ABRecordRef group = [self getAddressGroup:addressBook];
ABGroupAddMember(group, person, &error); // add the person to the group
ABAddressBookAddRecord(addressBook, group, &error); // add the group
ABAddressBookSave(addressBook, &error);
CFRelease(person); // relase the ABRecordRef variable
CFRelease(group);
CFRelease(addressBook);
} else { //ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined
ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool authorized, CFErrorRef error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (!authorized){
UIAlertView *cantAddContactAlert = [[UIAlertView alloc] initWithTitle:@"Cannot Add Contact"
message:@"You must give the app permission to add the contact first."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[cantAddContactAlert show];
}
});
});
}
}