Return to Snippet

Revision: 26683
at May 6, 2010 11:01 by espinallab


Updated Code
//This code is working properly in my project.  Hope it helps you too.

#pragma mark -
#pragma mark addToAddressBook

- (IBAction)addToAddressBook
{
	ABAddressBookRef addressBook = ABAddressBookCreate();
	ABRecordRef person = ABPersonCreate();
	CFErrorRef error = NULL;
	
	ABRecordSetValue(person, kABPersonOrganizationProperty, venue.name, &error);
	
	// a single url as the home page
    ABMutableMultiValueRef urlMultiValue = 
    ABMultiValueCreateMutable(kABStringPropertyType);
    ABMultiValueAddValueAndLabel(urlMultiValue, @"http://www.clubplanet.com",
                                 kABPersonHomePageLabel, NULL);
    ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, &error);
    CFRelease(urlMultiValue);
	
	if (venue.phone) {
		ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
		ABMultiValueAddValueAndLabel(multiPhone, venue.phone, kABPersonPhoneMainLabel, NULL);
		ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone, &error);
		CFRelease(multiPhone);
		
	}
		
	ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
	
	//Create a Disctionary Array to hold the address
	NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
	
	if (venue.address) [addressDictionary setObject:venue.address forKey:(NSString *)kABPersonAddressStreetKey];
	if (venue.city) [addressDictionary setObject:venue.city forKey:(NSString *)kABPersonAddressCityKey];
	if (venue.state) [addressDictionary setObject:venue.state forKey:(NSString *)kABPersonAddressStateKey];
	if (venue.zip) [addressDictionary setObject:venue.zip forKey:(NSString *)kABPersonAddressZIPKey];
	ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, kABWorkLabel, NULL);
	ABRecordSetValue(person, kABPersonAddressProperty, multiAddress,&error);
	CFRelease(multiAddress);
	
	ABAddressBookAddRecord(addressBook, person, &error);
	ABAddressBookSave(addressBook, &error);
	
	if (error != NULL)
	{
		
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
														message:@"Could not create unknown user" 
													   delegate:nil 
											  cancelButtonTitle:@"Cancel"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
	} else {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add To Contacts" 
														message:[NSString stringWithFormat:@"%@ was added to your contact successfully!", venue.name]
													   delegate:nil 
											  cancelButtonTitle:@"OK"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}


	CFRelease(person);
}

Revision: 26682
at May 6, 2010 10:40 by espinallab


Initial Code
//This code is working properly in my project.  Hope it helps you too.

#pragma mark -
#pragma mark addToAddressBook

- (IBAction)addToAddressBook
{
	ABAddressBookRef addressBook = ABAddressBookCreate();
	ABRecordRef person = ABPersonCreate();
	CFErrorRef error = NULL;
	
	ABRecordSetValue(person, kABPersonOrganizationProperty, venue.name, &error);
	
	// a single url as the home page
    ABMutableMultiValueRef urlMultiValue = 
    ABMultiValueCreateMutable(kABStringPropertyType);
    ABMultiValueAddValueAndLabel(urlMultiValue, @"http://www.clubplanet.com",
                                 kABPersonHomePageLabel, NULL);
    ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, &error);
    CFRelease(urlMultiValue);
	
	ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
	ABMultiValueAddValueAndLabel(multiPhone, venue.phone, kABPersonPhoneMainLabel, NULL);
	ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone, &error);
	CFRelease(multiPhone);
	
	ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
	
	//Create a Disctionary Array to hold the address
	NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
	
	[addressDictionary setObject:venue.address forKey:(NSString *)kABPersonAddressStreetKey];
	[addressDictionary setObject:venue.city forKey:(NSString *)kABPersonAddressCityKey];
	[addressDictionary setObject:venue.state forKey:(NSString *)kABPersonAddressStateKey];
	[addressDictionary setObject:venue.zip forKey:(NSString *)kABPersonAddressZIPKey];
	ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, kABWorkLabel, NULL);
	ABRecordSetValue(person, kABPersonAddressProperty, multiAddress,&error);
	CFRelease(multiAddress);
	
	ABAddressBookAddRecord(addressBook, person, &error);
	ABAddressBookSave(addressBook, &error);
	
	if (error != NULL)
	{
		
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
														message:@"Could not create unknown user" 
													   delegate:nil 
											  cancelButtonTitle:@"Cancel"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
	} else {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add To Contacts" 
														message:[NSString stringWithFormat:@"%@ was added to your contact successfully!", venue.name]
													   delegate:nil 
											  cancelButtonTitle:@"OK"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}


	CFRelease(person);
}

Initial URL
http://www.espinallab.com

Initial Description
Add the following framework "AddressBook.framework" to your project

Make sure to import these header files.
#import "AddressBook/ABAddressBook.h"
#import "AddressBook/ABPerson.h"
#import "AddressBook/ABMultiValue.h"

Initial Title
Adding entry to AddressBook iPhone

Initial Tags
iphone

Initial Language
Objective C