0

I am trying to add contacts to address book from my app. The code I have works great, but I want the contact name to be the value of a NSString. I have tried to replace @"David" in the code with the name of the string, but it doesn't seem to work.

CFErrorRef error = nil;
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef newPerson = ABPersonCreate();
ABRecordSetValue(newPerson, kABPersonFirstNameProperty,@"David", nil);
ABRecordSetValue(newPerson, kABPersonLastNameProperty,@"Olsen", nil);

//Add my phone number

ABMutableMultiValueRef phoneNumber = ABMultiValueCreateMutable(kABStringPropertyType);
ABMultiValueAddValueAndLabel(phoneNumber,
                             @"095", kABPersonPhoneMainLabel, nil);
ABRecordSetValue(newPerson, kABPersonEmailProperty, phoneNumber, nil);


//Finally saving the contact in the address book
ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
ABAddressBookSave(iPhoneAddressBook, &error);
if (error != NULL)
{
    NSLog(@"Saving contact failed.");
}


NSLog(@"Contact saved.");

}

rmaddy
  • 314,917
  • 42
  • 532
  • 579
ThomasGulli
  • 604
  • 1
  • 7
  • 20
  • 1
    That looks similar to your previous question http://stackoverflow.com/questions/22332545/address-book-ios-contact-name-from-string. If you don't get satisfying answer, please try to *improve* the question and provide more information. Don't just repeating the question. – Martin R Mar 11 '14 at 18:53

1 Answers1

1

You have to use a CFStringRef, so __bridge your NSString

NSString *name = @David";
ABRecordSetValue(newPerson, kABPersonFirstNameProperty,(__bridge  CFStringRef) name, nil);
Armand DOHM
  • 1,121
  • 1
  • 7
  • 9