Add address book contact to iphone objective-c

What is the correct way to set an address, etc. in the address book and let the user save it to iphone?

EDIT: removed a specific code issue and made it more general

+4
source share
2 answers

This is a complete working example of how to show a person by creating an ABRecordRef and clicking on it in a view using the viewcontroller

//////////////////////////// Connect it to the user 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 who is building 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; } 

//////////////////////////// Plug in the header:

Remember to import these frameworks:

 #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> 

Set delegate

 ABNewPersonViewControllerDelegate 

And add this to the interface

 ABNewPersonViewController *newPersonController; 
+17
source

From the eyeball, I think instead

 ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiStringPropertyType); 

Do you want to

 ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABDictionaryPropertyType); 
0
source

All Articles