I am working on an iPhone application which works with the address book contacts. I am trying to get make groups in the contacts but the problem is when I run the application again the group is created again and the new contact is saved to that new created group.
// create address book record
ABAddressBookRef addressBook = ABAddressBookCreate();
// create a person
ABRecordRef person = ABPersonCreate();
// name of the new person
ABRecordSetValue(person, kABPersonFirstNameProperty, [index objectAtIndex:3], nil);
ABRecordSetValue(person, kABPersonLastNameProperty, [index objectAtIndex:0], nil);
//add the new person to the record
ABAddressBookAddRecord(addressBook, person, nil);
ABAddressBookSave(addressBook, &error);
ABAddressBookAddRecord(addressBook, group, &error); // add the group
ABAddressBookSave(addressBook, &error);
ABRecordRef group = ABGroupCreate(); //create a group
ABGroupAddMember(group, person, &error); // add the person to the group
ABAddressBookSave(addressBook, &error);
//save the record
ABAddressBookSave(addressBook, nil);
// relase the ABRecordRef variable
CFRelease(person);
That's the code I have been working on.
ABRecordRef group = ABGroupCreate(); ,
It will create a new group... If you wan to add member in existing group then you should get group by its id.
Updated Code
ABRecordRef group = ABAddressBookGetGroupWithRecordID(addressBookInstance,putYourGroupIdHere);
Thanks,
ABAddressBookRef ab = ABAddressBookCreate();
CFErrorRef error;
MySource *source = [sourcesAndGroups objectAtIndex:0];
ABRecordRef group = [source.groups objectAtIndex:self.IndexValue]; //Get the Group Name
NSString *firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
ABRecordSetValue(group, kABGroupNameProperty,[self nameForGroup:group], &error);
ABAddressBookAddRecord(ab, group, &error);
ABAddressBookSave(ab, &error);
//Create new Person and save to this group
ABRecordRef record = ABPersonCreate();
BOOL isSuccess ;
isSuccess = ABRecordSetValue(record, kABPersonNicknameProperty,lastName, &error);
isSuccess = ABRecordSetValue(record, kABPersonMiddleNameProperty, firstName , &error);
ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
if(ABMultiValueGetCount(phoneNumbers) == 0)
{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"Phone Number" message:#"Please enter Phone number" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[av show];
return;
}
CFRelease(phoneNumbers);
NSString* phoneNumber = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
ABMutableMultiValueRef multi = ABRecordCopyValue(record, kABPersonEmailProperty);
ABMutableMultiValueRef copyOfPhones = ABMultiValueCreateMutable(kABPersonPhoneProperty);
ABMultiValueAddValueAndLabel(copyOfPhones, phoneNumber,kABPersonPhoneMobileLabel,NULL);
isSuccess = ABRecordSetValue(record, kABPersonPhoneProperty, copyOfPhones, &error);
isSuccess = ABRecordSetValue(record, kABPersonEmailProperty, multi, &error);
isSuccess = ABAddressBookAddRecord(ab, record, &error);
isSuccess = ABAddressBookSave(ab, &error);
ABGroupAddMember(group, record, &error);
NSLog(#"is success %d", isSuccess);
ABAddressBookSave(ab, &error);
CFRelease(group);
Related
I am a beginner in iOS development; this code is working in iOS 6 but does not work in iOS 7 ...
My code is below, I want to add contacts into my address book. I am developing an app where I have gone through many links, and I have following code, but now I am stuck...
I have imported:
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
ViewController.m file
ABAddressBookRef ab = ABAddressBookCreate();
// To add a new ab entry with telephone number
ABRecordRef newPerson = ABPersonCreate();
ABRecordSetValue(newPerson, kABPersonFirstNameProperty, (__bridge CFStringRef) nameFirststr, nil);
ABRecordSetValue(newPerson, kABPersonLastNameProperty, (__bridge CFStringRef)#"Jones", nil);
//phone
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFStringRef)myphone,kABPersonPhoneMobileLabel, NULL);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
// Adreess
ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
if (Address)
{
if (Address)
addressDictionary[(NSString *) kABPersonAddressStreetKey] = [NSString stringWithFormat:#"%#\n%#", Address, Address];
else
addressDictionary[(NSString *) kABPersonAddressStreetKey] = Address;
// if (true)
// addressDictionary[(NSString *)kABPersonAddressCityKey] = #"city";
// if (true)
// addressDictionary[(NSString *)kABPersonAddressStateKey] = #"city";
// if (true)
// addressDictionary[(NSString *)kABPersonAddressZIPKey] = #"city";
// if (true)
// addressDictionary[(NSString *)kABPersonAddressCountryKey] = #"city";
}
ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);
ABRecordSetValue(newPerson, kABPersonAddressProperty, multiAddress, nil);
// email
if (emailstr)
{
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) emailstr, kABWorkLabel, NULL);
ABRecordSetValue(newPerson, kABPersonEmailProperty, emailMultiValue, nil);
}
if (Organization)
{
ABRecordSetValue(newPerson, kABPersonOrganizationProperty, (__bridge CFStringRef)Organization, nil);
}
if (title)
{
ABRecordSetValue(newPerson, kABPersonJobTitleProperty, (__bridge CFStringRef)title, nil);
}
if (notes)
{
ABRecordSetValue(newPerson, kABPersonNoteProperty, (__bridge CFStringRef)notes, nil);
}
if (webUrl)
{
ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef) webUrl, kABPersonHomePageLabel, NULL);
ABRecordSetValue(newPerson, kABPersonURLProperty, urlMultiValue, nil);
}
ABAddressBookAddRecord(ab, newPerson, nil);
ABAddressBookSave(ab,NULL);
if (ABAddressBookSave(ab, nil)) {
NSLog(#"\nPerson Saved successfuly");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Your contact sucessfully Add" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok", nil];
[alert show];
} else {
NSLog(#"\n Error Saving person to AddressBook");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Error...!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok", nil];
[alert show];
}
check if addressbook is allowed
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
// First time access has been granted, add the contact
[self addContact];
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
[self addContact];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Access Denied" message:#"Please give access to AddressBook via settings." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}
also replace first line
CFErrorRef * error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
try this code...
ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init];
ABAddressBookRef addressBook = [peoplePicker addressBook];
// create person record
ABRecordRef person = ABPersonCreate();
// set name and other string values
UIImage *personImage = [UIImage imageNamed:#"cinema.png"];
NSData *dataRef = UIImagePNGRepresentation(personImage);
NSString *firstName=#"Raj";
NSString *lastName=#"Patel";
NSString *organization=#"Ilesh Pvt Ltd.";
NSString *jobTitle=#"iPhone App Developer";
NSString *departMent=#"Mobile Division";
NSString *webURL=#"http://www.google.com";
NSString *personEmail=#"goel.anjan#gmail.com";
NSString *phoneNo=#"913654985 or 76876879 or 845676764";
NSString *personNote=#"I am just a kid";
NSString *addressOne=#"Ahmedabad";
NSString *addressTwo=#"Ahmedabad";
NSString *cityName=#"Ahmedabad";
NSString *stateName=#"Gujarat";
NSString *pinCode=#"38008";
NSString *country=#"India";
CFErrorRef cfError=nil;
ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFStringRef)organization, NULL);
if (firstName) {
ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)(firstName) , nil);
}
if (lastName) {
ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)(lastName) , nil);
}
if (jobTitle) {
ABRecordSetValue(person, kABPersonJobTitleProperty,(__bridge CFTypeRef)(jobTitle), nil);
}
if (departMent) {
ABRecordSetValue(person, kABPersonDepartmentProperty,(__bridge CFTypeRef)(departMent), nil);
}
if (personNote) {
ABRecordSetValue(person, kABPersonNoteProperty, (__bridge CFTypeRef)(personNote), nil);
}
if (dataRef) {
ABPersonSetImageData(person, (__bridge CFDataRef)dataRef,&cfError);
}
if (webURL)
{
ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef) webURL, kABPersonHomePageLabel, NULL);
ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, nil);
CFRelease(urlMultiValue);
}
if (personEmail)
{
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) personEmail, kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
CFRelease(emailMultiValue);
}
if (phoneNo)
{
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
NSArray *venuePhoneNumbers = [phoneNo componentsSeparatedByString:#" or "];
for (NSString *venuePhoneNumberString in venuePhoneNumbers)
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) venuePhoneNumberString, kABPersonPhoneMainLabel, NULL);
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
CFRelease(phoneNumberMultiValue);
}
// add address
ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
if (addressOne)
{
if (addressTwo)
addressDictionary[(NSString *) kABPersonAddressStreetKey] = [NSString stringWithFormat:#"%#\n%#", addressOne, addressTwo];
else
addressDictionary[(NSString *) kABPersonAddressStreetKey] = addressOne;
}
if (cityName)
addressDictionary[(NSString *)kABPersonAddressCityKey] = cityName;
if (stateName)
addressDictionary[(NSString *)kABPersonAddressStateKey] = stateName;
if (pinCode)
addressDictionary[(NSString *)kABPersonAddressZIPKey] = pinCode;
if (country)
addressDictionary[(NSString *)kABPersonAddressCountryKey] = country;
ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonAddressProperty, multiAddress, NULL);
CFRelease(multiAddress);
//Add person Object to addressbook Object.
ABAddressBookAddRecord(addressBook, person, &cfError);
if (ABAddressBookSave(addressBook, nil)) {
NSLog(#"\nPerson Saved successfuly");
} else {
NSLog(#"\n Error Saving person to AddressBook");
}
modified code:
CFErrorRef * error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
// First time access has been granted, add the contact
[self addContact];
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
[self addContact];
}
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
// First time access has been granted, add the contact
[self addContact];
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
[self addContact];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Access Denied" message:#"Please give access to AddressBook via settings." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}
I want to add contacts into my address book. I am developing an app where I have gone through many links, and I have following code, but now I am stuck.
I have imported:
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h
in viewcontroller.m:
-(IBAction)addToAddressbook:(id)sender{
ABUnknownPersonViewController *unknownPersonViewController = [[ABUnknownPersonViewController alloc] init];
unknownPersonViewController.displayedPerson = (ABRecordRef)[self buildContactDetails];
unknownPersonViewController.allowsAddingToAddressBook = YES;
[self.navigationController pushViewController:unknownPersonViewController animated:YES];
[unknownPersonViewController release];
}
- (ABRecordRef)buildContactDetails {
NSLog(#"building contact details");
ABRecordRef person = ABPersonCreate();
CFErrorRef error = NULL;
// firstname
ABRecordSetValue(person, kABPersonFirstNameProperty, #"Don Juan", NULL);
// email
ABMutableMultiValueRef email = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(email, #"expert.in#computer.com", CFSTR("email"), NULL);
ABRecordSetValue(person, kABPersonEmailProperty, email, &error);
CFRelease(email);
// Start of Address
ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];
[addressDict setObject:#"The awesome road numba 1" forKey:(NSString *)kABPersonAddressStreetKey];
[addressDict setObject:#"0568" forKey:(NSString *)kABPersonAddressZIPKey];
[addressDict setObject:#"Oslo" forKey:(NSString *)kABPersonAddressCityKey];
ABMultiValueAddValueAndLabel(address, addressDict, kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonAddressProperty, address, &error);
[addressDict release];
CFRelease(address);
// End of Address
if (error != NULL)
NSLog(#"Error: %#", error);
[(id)person autorelease];
return person;
}
In UI I have an IBAction button connected to addToAddressbook, but on click, nothing is happening - so what else to do I need to do in UI or in the code?
After looking at your code i can see that you are missing the ABAddressBookSave statement. After setting the values of
// firstname
ABRecordSetValue(person, kABPersonFirstNameProperty, #"Don Juan", NULL);
// email
ABRecordSetValue(person, kABPersonEmailProperty, email, &error);
// Address
ABRecordSetValue(person, kABPersonAddressProperty, address, &error);
You should call ABAddressBookSave which is as follows.
ABAddressBookSave(ABAddressBookRef addressBook, CFErrorRef* error);
I want to create a new contact in the addressbook.
The problem appears when you want to store more URLs (web addresses to some social networks).
My code works perfectly in simulator of iOS6. But in real iPhone with iOS6, stores all values except the urls.
I've been looking for a few days and can not find a solution, I will be very grateful if anyone can help.
My code:
-(void) addContactToAddressBook:(ABAddressBookRef) iPhoneAddressBook
{
CFErrorRef error = NULL;
ABRecordRef newPerson = ABPersonCreate();
//Name and phone number
ABRecordSetValue(newPerson, kABPersonFirstNameProperty, (__bridge CFStringRef)_nameField.text, &error);
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFStringRef)_phoneField.text, kABPersonPhoneMainLabel, NULL);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
//Email value
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef)_emailField.text, kABWorkLabel, NULL);
ABRecordSetValue(newPerson, kABPersonEmailProperty, emailMultiValue, nil);
CFRelease(emailMultiValue);
//URL values
ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_FacebookField.text, (CFStringRef)#"Facebook", NULL);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_twitterField.text, (CFStringRef)#"Twitter", NULL);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_linkedinField.text, (CFStringRef)#"Linkedin", NULL);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_googleField.text, (CFStringRef)#"Google+", NULL);
ABRecordSetValue(newPerson, kABPersonURLProperty, urlMultiValue, nil);
CFRelease(urlMultiValue);
ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
ABAddressBookSave(iPhoneAddressBook, &error);
if (error != NULL)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Contact not saved" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:nil, nil];
[alert show];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Contact saved" message:#"Your contact was successfully saved" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
Thanks!
As per the documentation on apple's site (scroll down to Privacy in the middle of the page), access to the address book must be granted before it can be access programmatically. Here is what I ended up doing.
-(void)requestPermission
{
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(iPhoneAddressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
// First time access has been granted, add the contact
[self addContactToAddressBook:iPhoneAddressBook];
} else {
// User denied access
// Display an alert telling user the contact could not be added
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
[self addContactToAddressBook:iPhoneAddressBook];
}
else {
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}
}
-(void)addContactToAddressBook:(ABAddressBookRef)iPhoneAddressBook
{
CFErrorRef error = NULL;
ABRecordRef newPerson = ABPersonCreate();
//Name and phone number
ABRecordSetValue(newPerson, kABPersonFirstNameProperty, (__bridge CFStringRef)_nameField.text, &error);
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFStringRef)_phoneField.text, kABPersonPhoneMainLabel, NULL);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
CFRelease(multiPhone);
//Email value
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef)_emailField.text, kABWorkLabel, NULL);
ABRecordSetValue(newPerson, kABPersonEmailProperty, emailMultiValue, nil);
CFRelease(emailMultiValue);
//URL values
ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_FacebookField.text, (CFStringRef)#"Facebook", NULL);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_twitterField.text, (CFStringRef)#"Twitter", NULL);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_linkedinField.text, (CFStringRef)#"Linkedin", NULL);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_googleField.text, (CFStringRef)#"Google+", NULL);
ABRecordSetValue(newPerson, kABPersonURLProperty, urlMultiValue, nil);
CFRelease(urlMultiValue);
// ...
// Set other properties
// ...
ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
ABAddressBookSave(iPhoneAddressBook, &error);
CFRelease(newPerson);
CFRelease(iPhoneAddressBook);
if (error != NULL)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Contact not saved" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:nil, nil];
[alert show];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Contact saved" message:#"Your contact was successfully saved" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
I want to save contact directly to an iOS device's Address Book programmatically.
How can I do it?
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);
}
this is my code and it works flawless, where my_value is a string with separator ','.
everythign works fin but i'd like to display the person record from the address book after i saved it, so in the function
if(isSaved) {
// **** code here ***
}
here the complete function
- (void) addToAgenda: (NSString*) my_value{
//NSArray *strings = [my_value componentsSeparatedByString: #","];
NSArray *dati=[[NSArray alloc] initWithArray:[my_value componentsSeparatedByString:#","]];
NSString *userwebsite = [dati objectAtIndex:0];
NSString *fname = [dati objectAtIndex:1];
NSString *lname = [dati objectAtIndex:2];
NSString *useremail = [dati objectAtIndex:3];;
NSString *usermobile = [dati objectAtIndex:4];
NSString *usercompany = #"xxx";
ABRecordRef aRecord = ABPersonCreate();
CFErrorRef anError = NULL;
// fisrst name
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, fname, &anError);
// last name
ABRecordSetValue(aRecord, kABPersonLastNameProperty, lname, &anError);
// Phone Number.
ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multi, (CFStringRef)usermobile, kABWorkLabel, NULL);
ABRecordSetValue(aRecord, kABPersonPhoneProperty, multi, &anError);
CFRelease(multi);
// Company
ABRecordSetValue(aRecord, kABPersonDepartmentProperty, usercompany, &anError);
// email
NSLog(#"%#", useremail);
ABMutableMultiValueRef multiemail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiemail, (CFStringRef)useremail, kABWorkLabel, NULL);
ABRecordSetValue(aRecord, kABPersonEmailProperty, multiemail, &anError);
CFRelease(multiemail);
// website
NSLog(#"%#", userwebsite);
ABMutableMultiValueRef multiweb = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiweb, (CFStringRef)userwebsite, kABWorkLabel, NULL);
ABRecordSetValue(aRecord, kABPersonURLProperty, multiweb, &anError);
CFRelease(multiemail);
if (anError != NULL)
NSLog(#"error while creating..");
CFStringRef personname, personlname, personcompind, personemail, personwebsite, personcontact;
personname = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty);
personlname = ABRecordCopyValue(aRecord, kABPersonLastNameProperty);
personcompind = ABRecordCopyValue(aRecord, kABPersonDepartmentProperty);
personemail = ABRecordCopyValue(aRecord, kABPersonEmailProperty);
personwebsite = ABRecordCopyValue(aRecord, kABPersonURLProperty);
personcontact = ABRecordCopyValue(aRecord, kABPersonPhoneProperty);
ABAddressBookRef addressBook;
CFErrorRef error = NULL;
addressBook = ABAddressBookCreate();
BOOL isAdded = ABAddressBookAddRecord (addressBook, aRecord, &error);
if(isAdded){
NSLog(#"added..");
}
if (error != NULL) {
NSLog(#"ABAddressBookAddRecord %#", error);
}
error = NULL;
BOOL isSaved = ABAddressBookSave (addressBook, &error);
if(isSaved) {
// **** code here ***
}
if (error != NULL) {
NSLog(#"ABAddressBookSave %#", error);
UIAlertView *alertOnChoose = [[UIAlertView alloc] initWithTitle:#"Unable to save this time" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok", nil];
[alertOnChoose show];
[alertOnChoose release];
}
CFRelease(aRecord);
CFRelease(personname);
CFRelease(personlname);
CFRelease(personcompind);
CFRelease(personcontact);
CFRelease(personemail);
CFRelease(personwebsite);
CFRelease(addressBook);
}
You can use something like ABPersonViewController to display aRecord. Since it is a subclass of UIViewController, you can show it just like any other view controller, such as pushing it onto your navigation controller stack or presenting it modally.