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];
}
Related
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 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?
I'm having some memory leaks with both ABAddressBookGetPersonWithRecordID and ABPersonSetImageData. I've been looking for solutions before posting here but I still don't understand. If I play quite a long time with the iPhone 3GS or with only a few contacts with iPhone 3G, actually the application crashes. Here is my code in the didSelectRowAtIndexPath method. I've seen sample codes with these methods and I don't see what I'm missing. Thank you in advance. (sorry for mistakes...)
Contact *myContact = [fetchedResultsController objectAtIndexPath:indexPath];
cancelCreateContact = NO;
ABAddressBookRef ab = ABAddressBookCreate();
int len = ABAddressBookGetPersonCount(ab);
ABRecordID contactID;
ABRecordRef person;
BOOL alreadyExists = NO;
CFStringRef first, last;
for(int i = 1; i < (len + 1); i++)
{
person = ABAddressBookGetPersonWithRecordID(ab, (ABRecordID)i);
if(!person){
len++;
continue;
}
first = ABRecordCopyValue(person, kABPersonFirstNameProperty);
last = ABRecordCopyValue(person, kABPersonLastNameProperty);
if ([[(NSString*)first lowercaseString] isEqualToString:[myContact.firstname lowercaseString]] && [[(NSString*)last lowercaseString] isEqualToString:[myContact.lastname lowercaseString]]) {
alreadyExists = YES;
contactID = ABRecordGetRecordID(person);
break;
}
}
if (alreadyExists) {
//NSLog(#"already exists");
ABRecordRef aContactFound = ABAddressBookGetPersonWithRecordID(ab, contactID);
ABRecordRef aRecord = ABPersonCreate();
CFErrorRef anError = NULL;
CFStringRef firstname = ABRecordCopyValue(aContactFound, kABPersonFirstNameProperty);
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, firstname, &anError);
CFRelease(firstname);
CFStringRef lastname = ABRecordCopyValue(aContactFound, kABPersonLastNameProperty);
ABRecordSetValue(aRecord, kABPersonLastNameProperty, lastname, &anError);
CFRelease(lastname);
CFStringRef job = ABRecordCopyValue(aContactFound, kABPersonJobTitleProperty);
ABRecordSetValue(aRecord, kABPersonJobTitleProperty, job, &anError);
CFRelease(job);
ABMultiValueRef instantMessage = ABRecordCopyValue(aContactFound, kABPersonInstantMessageProperty);
ABRecordSetValue(aRecord, kABPersonInstantMessageProperty, instantMessage, &anError);
CFRelease(instantMessage);
ABMultiValueRef phone = ABRecordCopyValue(aContactFound, kABPersonPhoneProperty);
ABRecordSetValue(aRecord, kABPersonPhoneProperty, phone, &anError);
CFRelease(phone);
ABMultiValueRef email = ABRecordCopyValue(aContactFound, kABPersonEmailProperty);
ABRecordSetValue(aRecord, kABPersonEmailProperty, email, &anError);
CFRelease(email);
CFDataRef imageData = ABPersonCopyImageData(aContactFound);
ABPersonSetImageData(aRecord, imageData, &anError);
ABAddressBookSave(ab, &anError);
CFRelease(imageData);
ABUnknownPersonViewController *ABView = [[ABUnknownPersonViewController alloc] init];
ABView.unknownPersonViewDelegate = self;
ABView.displayedPerson = aRecord;
ABView.allowsAddingToAddressBook = NO;
ABView.allowsActions = YES;
ABView.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:ABView animated:YES];
[ABView release];
CFRelease(aRecord);
}else{
//NSLog(#"doesn't exist");
//sinon ouvre une fiche pré-remplie
ABRecordRef aRecord = ABPersonCreate();
CFErrorRef anError = nil;
if(![myContact.firstname isEqualToString:#""]) ABRecordSetValue(aRecord, kABPersonFirstNameProperty, myContact.firstname, &anError);
if(![myContact.lastname isEqualToString:#""]) ABRecordSetValue(aRecord, kABPersonLastNameProperty, myContact.lastname, &anError);
if(![myContact.email isEqualToString:#""]) {
ABMultiValueRef ABemail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(ABemail, myContact.email, kABWorkLabel, NULL);
ABRecordSetValue(aRecord, kABPersonEmailProperty, ABemail, &anError);
CFRelease(ABemail);
}
if(![myContact.phone_business isEqualToString:#""] || ![myContact.phone_mobile isEqualToString:#""]){
ABMultiValueRef ABphones = ABMultiValueCreateMutable(kABMultiStringPropertyType);
if(![myContact.phone_business isEqualToString:#""]) ([myContact.phone_business stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length == 4 || [myContact.phone_business stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length == 5) ? ABMultiValueAddValueAndLabel(ABphones, [NSString stringWithFormat:#"014443%#", myContact.phone_business], kABPersonPhoneMainLabel, NULL) : ABMultiValueAddValueAndLabel(ABphones, myContact.phone_business, kABPersonPhoneMainLabel, NULL);
if(![myContact.phone_mobile isEqualToString:#""] && ([myContact.phone_mobile stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length == 10)) ABMultiValueAddValueAndLabel(ABphones, myContact.phone_mobile, kABPersonPhoneMobileLabel, NULL);
ABRecordSetValue(aRecord, kABPersonPhoneProperty, ABphones, &anError);
CFRelease(ABphones);
}
if(![myContact.job isEqualToString:#""]) ABRecordSetValue(aRecord, kABPersonJobTitleProperty, myContact.job, &anError);
if(![myContact.msn isEqualToString:#""] || ![myContact.twitter isEqualToString:#""] || ![myContact.facebook isEqualToString:#""]){
ABMultiValueRef ABmessaging = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *dMessaging;
if(![myContact.msn isEqualToString:#""]){
dMessaging = [[NSMutableDictionary alloc] init];
[dMessaging setObject:myContact.msn forKey:(NSString *) kABPersonInstantMessageUsernameKey];
[dMessaging setObject:#"MSN" forKey:(NSString *)kABPersonInstantMessageServiceKey];
ABMultiValueAddValueAndLabel(ABmessaging, dMessaging, kABPersonInstantMessageServiceMSN, NULL);
[dMessaging release];
}
if(![myContact.twitter isEqualToString:#""]){
dMessaging = [[NSMutableDictionary alloc] init];
[dMessaging setObject:myContact.twitter forKey:(NSString *) kABPersonInstantMessageUsernameKey];
[dMessaging setObject:#"Twitter" forKey:(NSString *)kABPersonInstantMessageServiceKey];
ABMultiValueAddValueAndLabel(ABmessaging, dMessaging, kABOtherLabel, NULL);
[dMessaging release];
}
if(![myContact.facebook isEqualToString:#""]){
dMessaging = [[NSMutableDictionary alloc] init];
[dMessaging setObject:myContact.facebook forKey:(NSString *) kABPersonInstantMessageUsernameKey];
[dMessaging setObject:#"Facebook" forKey:(NSString *)kABPersonInstantMessageServiceKey];
ABMultiValueAddValueAndLabel(ABmessaging, dMessaging, kABOtherLabel, NULL);
[dMessaging release];
}
ABRecordSetValue(aRecord, kABPersonInstantMessageProperty, ABmessaging, &anError);
CFRelease(ABmessaging);
}
//pas dans l'XMLToObjectParser parce que ça prenait une plombe...
NSURL *url = [NSURL URLWithString:myContact.picture_path];
NSData *data = [NSData dataWithContentsOfURL:url];
if(!data){
NSString *picture_path = (![myContact.gender isEqualToString:#""]) ? [NSString stringWithFormat:#"default_%#_head.png", [myContact.gender lowercaseString]] : #"default_m_head.png";
[myContact setPicture_path:picture_path];
NSError *error = nil;
if(![self.managedObjectContext save:&error]){
NSLog(#"pb lors de l'enregistrement de picture path");
}
//NSData *localData = [NSData dataWithContentsOfFile:myContact.picture_path];
UIImage *image = [UIImage imageNamed:picture_path];
NSData *localData = UIImagePNGRepresentation(image);
CFDataRef cfLocalData = CFDataCreate(NULL, [localData bytes], [localData length]);
ABPersonSetImageData(aRecord, cfLocalData, &anError);
ABAddressBookSave(ab, &anError);
CFRelease(cfLocalData);
}else {
UIImage *image = [UIImage imageWithData:data];
NSString *extension = [(NSArray*)[myContact.picture_path componentsSeparatedByString:#"."] objectAtIndex:1];
NSData *localData = ([extension isEqualToString:#"png"]) ? UIImagePNGRepresentation(image) : UIImageJPEGRepresentation(image, 1.0f);
CFDataRef cfLocalData = CFDataCreate(NULL, [localData bytes], [localData length]);
ABPersonSetImageData(aRecord, cfLocalData, &anError);
ABAddressBookSave(ab, &anError);
CFRelease(cfLocalData);
}
if (anError != nil) { NSLog(#"error :: %#", anError); }
ABUnknownPersonViewController *ABView = [[ABUnknownPersonViewController alloc] init];
ABView.unknownPersonViewDelegate = self;
ABView.displayedPerson = aRecord;
ABView.allowsAddingToAddressBook = YES;
ABView.allowsActions = YES;
ABView.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:ABView animated:YES];
[ABView release];
CFRelease(aRecord);
}
CFRelease(ab);
Firstly: read up on your Core Foundation memory management. You don't yet know the rules by heart.
Secondly: when a CF-friendly function has "Copy" in the name, you must check its result for NULL, and release that result when done if it's not NULL. So this:
first = ABRecordCopyValue(person, kABPersonFirstNameProperty);
Will become a memory leak if it's never followed by CFRelease(first);.
Thirdly: if a Core Foundation value is NULL, passing it to CFRelease will crash:
CFStringRef firstname = ABRecordCopyValue(aContactFound, kABPersonFirstNameProperty);
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, firstname, &anError);
CFRelease(firstname);
If firstname is NULL (which it could be--imagine a contact named simply "Smith") then a crash will happen.
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.
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);