Add contact to addressbook in iphone objective-c - iphone

What is the correct way to set street address etc in addressbook and let the user save it on iphone?
EDIT: removed the specific code-problem and made it more general

This is a complete working sample of how to show an person by creating an ABRecordRef and pushing it into the view using an viewcontroller
/////////////////////////////
Hook it up to a custom action.
-(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];
}
////////////////////////////
This is the guy that builds the ABrecordRef
- (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;
}
////////////////////////////
Wire up in the header:
Remember to import these frameworks:
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
Set the delegate
ABNewPersonViewControllerDelegate
And add this to the interface
ABNewPersonViewController *newPersonController;

From eyeballing it, I think instead of
ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiStringPropertyType);
you want
ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABDictionaryPropertyType);

Related

my contact not add in addressbook in ios 7

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
}

Adding contact into ios addressbook

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);

Code get crashing at adding phone contacts query

I am able to add only showroom name to my contacts. But when I put the code to add phone numbers, further details....my code get crash. Any suggestion would be appreciated. Here is my code
- (IBAction)AddContact
{
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef Showroom = ABPersonCreate();
//adding contact name as showroom name
ABRecordSetValue(Showroom, kABPersonFirstNameProperty, ShowroomName.text , nil);
//adding phone number
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiPhone, p_BcardLabel.text, kABPersonPhoneMainLabel, NULL);
ABRecordSetValue(Showroom, kABPersonPhoneProperty, multiPhone,nil);
CFRelease(multiPhone);
//adding emailaddress
ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiEmail, email_BcardLabel.text, kABWorkLabel, NULL);
ABRecordSetValue(Showroom, kABPersonEmailProperty, multiEmail, #"");
CFRelease(multiEmail);
//adding URL
ABMutableMultiValueRef multiURL = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiURL, url_BcardLabel.text, kABHomeLabel, NULL);
ABRecordSetValue(Showroom, kABPersonURLProperty, multiURL, #"");
CFRelease(multiURL);
Shows following message
[Switching to thread 11779]
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.2 (8H7)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
If you want only to improve your code. I have checked once your code its fine working just add the following lines
ABAddressBookAddRecord(addressBook,Showroom, nil);
ABAddressBookSave(addressBook,nil);
objABPersonViewController=[[ABUnknownPersonViewController alloc]init];
objABPersonViewController.displayedPerson=Showroom;
[self.navigationController pushViewController:objABPersonViewController animated:YES];
CFRelease(Showroom);
And if you want to use mine check this out. It will also add an image named abc.png from your resource folder to the contact list.
-(IBAction)addToAddressbook:(id)sender{
NSString *fname=#"Person First Name";
NSString *lname=#"Person Last Name";
NSArray *arrayAdd=[[NSArray alloc]initWithObjects:#"street Name",#"city Name",#"country code",#"zip",nil];
UIImage *image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"abc.png" ofType:nil]];
[self addContact:fname:lname:arrayAdd:image];
}
-(void) addContact:(NSString *)firstname:(NSString *)lastname:(NSArray *)arrayAddress:(UIImage *)currentImage
{
ABAddressBookRef addressBook=ABAddressBookCreate();
ABRecordRef person=ABPersonCreate();
//set Image
NSData * dataRef = UIImagePNGRepresentation(currentImage);
ABPersonSetImageData(person, (CFDataRef)dataRef, nil);
//set FirstName and LastName
ABRecordSetValue(person, kABPersonFirstNameProperty,firstname, nil);
ABRecordSetValue(person, kABPersonLastNameProperty,lastname, nil);
//Add Address
ABMutableMultiValueRef address=ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary=[[NSMutableDictionary alloc]init];
[addressDictionary setObject:[arrayAddress objectAtIndex:0] forKey:(NSString *)kABPersonAddressStreetKey];
[addressDictionary setObject:[arrayAddress objectAtIndex:1]forKey:(NSString *)kABPersonAddressCityKey];
[addressDictionary setObject:[arrayAddress objectAtIndex:2] forKey:(NSString *)kABPersonAddressCountryCodeKey];
[addressDictionary setObject:[arrayAddress objectAtIndex:3] forKey:(NSString *)kABPersonAddressCountryKey];
ABMultiValueAddValueAndLabel(address, addressDictionary, kABHomeLabel, nil);
ABRecordSetValue(person, kABPersonAddressProperty, address, nil);
ABAddressBookAddRecord(addressBook,person, nil);
ABAddressBookSave(addressBook,nil);
objABPersonViewController=[[ABUnknownPersonViewController alloc]init];
objABPersonViewController.displayedPerson=person;
[self.navigationController pushViewController:objABPersonViewController animated:YES];
CFRelease(person);
}
for any further query just ask... Happy to help
This is the Answer to your question Navnath. Apple provides ABNewPersonViewController to open the particular add contact view.Add the ABNewPersonViewControllerDelegate in your header file.
case 1:Application is view based:-
-(IBAction)displayContactAddWindow:(id)sender{
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
picker.newPersonViewDelegate = self;
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:picker];
[self presentModalViewController:navigation animated:YES];
[picker release];
[navigation release];
}
case 2:Application is navigation based:-
-(IBAction)displayContactAddWindow:(id)sender{
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
picker.newPersonViewDelegate = self;
[self.navigationController pushViewController:picker animated:YES];
[picker release];
}
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person{
[self.navigationController popViewControllerAnimated:YES];
}

Address book implementation not working

I am using XCode 4.2 to develop a function to add a contact to the address book , here is my code
ABAddressBookRef *iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef contact = ABPersonCreate();
//add infos
ABRecordSetValue(contact, kABPersonFirstNameProperty,(__bridge_retained CFStringRef)firstName, nil);
ABRecordSetValue(contact, kABPersonLastNameProperty,(__bridge_retained CFStringRef)lastName, nil);
ABRecordSetValue(contact, kABPersonOrganizationProperty, (__bridge_retained CFStringRef)organization, nil);
ABRecordSetValue(contact, kABPersonJobTitleProperty, (__bridge_retained CFStringRef)title, nil);
ABMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiRealPropertyType);
ABMultiValueAddValueAndLabel(multiPhone, (__bridge_retained CFStringRef)workTel, kABPersonPhoneMainLabel, NULL);
ABMultiValueAddValueAndLabel(multiPhone, (__bridge_retained CFStringRef)workFax, kABPersonPhoneWorkFAXLabel, NULL);
ABRecordSetValue(contact, kABPersonPhoneProperty, multiPhone, nil);
CFRelease(multiPhone);
ABMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiEmail, (__bridge_retained CFStringRef)email, kABWorkLabel, NULL);
ABRecordSetValue(contact, kABPersonEmailProperty, multiEmail, nil);
CFRelease(multiEmail);
// address
ABMultiValueRef multiAddress =ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDict = [[NSMutableDictionary alloc]init];
[addressDict setObject:address forKey:(NSString *) kABPersonAddressStreetKey];
[addressDict setObject:city forKey:(NSString *) kABPersonAddressCityKey];
[addressDict setObject:province forKey:(NSString *) kABPersonAddressStateKey];
[addressDict setObject:postalCode forKey:(NSString *) kABPersonAddressZIPKey];
[addressDict setObject:address forKey:(NSString *) kABPersonAddressCountryKey];
ABMultiValueAddValueAndLabel(multiAddress, (__bridge_retained CFStringRef)addressDict, kABWorkLabel, NULL);
ABRecordSetValue(contact, kABPersonAddressProperty, multiAddress, NULL);
CFRelease(multiAddress);
ABMultiValueRef multiURL =ABMultiValueCreateMutable(kABMultiRealPropertyType);
ABMultiValueAddValueAndLabel(multiURL, (__bridge_retained CFStringRef)link, kABPersonURLProperty, NULL);
CFRelease(multiURL);
ABAddressBookAddRecord(iPhoneAddressBook, contact, nil);
BOOL didAdd = ABAddressBookSave(iPhoneAddressBook, nil);
CFRelease(contact);
CFRelease(iPhoneAddressBook);
//notifying the user that it was stored in his address book
if (didAdd) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Confirmation"
message:#"Contact Info successfully added to the Address Book"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
the program compiles and but it stops at this line :
ABMultiValueAddValueAndLabel(multiPhone, (__bridge_retained CFStringRef)workTel, kABPersonPhoneMainLabel, NULL);
I get this error (in green)
Thread 1
any clue ? what is wrong in the code ?
This is the definition of the function in which your error occurs:
bool ABMultiValueAddValueAndLabel (
ABMutableMultiValueRef multiValue,
CFTypeRef value,
CFStringRef label,
ABMultiValueIdentifier *outIdentifier
);
It appears you have switched the 2nd and 3rd arguments, right?

How to display a person record just after saving his data to iPhone Address Book?

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.