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?
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);
In my app I've stored ABPropertyID in database and based on that I've displayed number of ABRecordRef with ABAddressBookGetPersonWithRecordID(addressBook, recordID). But when contact deleted in contacts and I try to get ABRecordRef, App crashes is obvious.
So my question is:
Is there any way to check that recordID is available in addressBook?
This function does not throw an exception, but it returns NULL if there is no record with this ID.
See: http://developer.apple.com/library/ios/ipad/#documentation/AddressBook/Reference/ABPersonRef_iPhoneOS/Reference/reference.html
try this one
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
// First time access has been granted, add the contact
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABRecordRef newPerson = ABPersonCreate();
ABRecordSetValue(newPerson, kABPersonSortByFirstName,(__bridge CFTypeRef)(firstname),&error);
ABRecordSetValue(newPerson, kABPersonLastNameProperty,(__bridge CFTypeRef)(lastname), &error);
ABRecordSetValue(newPerson, kABPersonOrganizationProperty, #"", &error);
ABRecordSetValue(newPerson, kABPersonJobTitleProperty,#"", &error);
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFTypeRef)(contact_phone), kABPersonPhoneMainLabel, NULL);
ABMultiValueAddValueAndLabel(multiPhone, #"", kABPersonPhoneMobileLabel, NULL);
ABMultiValueAddValueAndLabel(multiPhone, #"", kABOtherLabel, NULL);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
CFRelease(multiPhone);
ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiEmail,(__bridge CFTypeRef)(contact_email) , kABWorkLabel, NULL);
ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error);
CFRelease(multiEmail);
// ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
// NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
// [addressDictionary setObject:#"750 North Orleans Street, Ste 601" forKey:(NSString *) kABPersonAddressStreetKey];
// [addressDictionary setObject:#"Chicago" forKey:(NSString *)kABPersonAddressCityKey];
// [addressDictionary setObject:#"IL" forKey:(NSString *)kABPersonAddressStateKey];
// [addressDictionary setObject:#"60654" forKey:(NSString *)kABPersonAddressZIPKey];
// ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, kABWorkLabel, NULL);
// ABRecordSetValue(newPerson, kABPersonAddressProperty, multiAddress,&error);
// CFRelease(multiAddress);
ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
ABAddressBookSave(iPhoneAddressBook, &error);
if (error != NULL)
{
NSLog(#"Danger Will Robinson! Danger!");
}
// ABAddressBookRef addressBook = ABAddressBookCreate();
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
CFErrorRef error = NULL;
// ABAddressBookRef addressBook =ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABRecordRef newPerson = ABPersonCreate();
ABRecordSetValue(newPerson, kABPersonSortByFirstName,(__bridge CFTypeRef)(firstname),&error);
NSLog(#"%#",(__bridge CFTypeRef)(firstname));
ABRecordSetValue(newPerson, kABPersonLastNameProperty,(__bridge CFTypeRef)(lastname), &error);
NSLog(#"%#",(__bridge CFTypeRef)(lastname));
ABRecordSetValue(newPerson, kABPersonOrganizationProperty, #"", &error);
ABRecordSetValue(newPerson, kABPersonJobTitleProperty,#"", &error);
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiPhone,(__bridge CFTypeRef)(contact_phone), kABPersonPhoneMainLabel, NULL);
ABMultiValueAddValueAndLabel(multiPhone, #"", kABPersonPhoneMobileLabel, NULL);
ABMultiValueAddValueAndLabel(multiPhone, #"", kABOtherLabel, NULL);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
CFRelease(multiPhone);
ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiEmail, (__bridge CFTypeRef)(contact_email), kABWorkLabel, NULL);
ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error);
CFRelease(multiEmail);
// ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
// NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
// [addressDictionary setObject:#"750 North Orleans Street, Ste 601" forKey:(NSString *) kABPersonAddressStreetKey];
// [addressDictionary setObject:#"Chicago" forKey:(NSString *)kABPersonAddressCityKey];
// [addressDictionary setObject:#"IL" forKey:(NSString *)kABPersonAddressStateKey];
// [addressDictionary setObject:#"60654" forKey:(NSString *)kABPersonAddressZIPKey];
// ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, kABWorkLabel, NULL);
// ABRecordSetValue(newPerson, kABPersonAddressProperty, multiAddress,&error);
// CFRelease(multiAddress);
ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
ABAddressBookSave(iPhoneAddressBook, &error);
alertvalue++;
if (error != NULL)
{
NSLog(#"Error not found!");
}
// ABAddressBookRef addressBook = ABAddressBookCreate();
}
else {
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
} }
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);