Framework

a. To access address book in you application first add two framework called:
1. AddressBook
2. AddressBookUI

in __.h file

b. Now write the following code

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

c. Add this delegate:

<ABPeoplePickerNavigationControllerDelegate>

in __.m file

d. Now write these code in that function from which  address book will load:

{

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
// place the delegate of the picker to the controll
picker.peoplePickerDelegate = self;
picker.navigationBar.barStyle = UIBarStyleBlackOpaque;

// showing the picker
[self presentModalViewController:picker animated:YES];
// releasing
[picker release];

}

#pragma mark addressbook delegate
– (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
// assigning control back to the main controller
[self dismissModalViewControllerAnimated:YES];
}

– (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)ref {

//clear all data
[delegate.nameInfo removeAllObjects];
[delegate.phoneNoInfo removeAllObjects];
[delegate.emailInfo removeAllObjects];
[delegate.addressInfo removeAllObjects];
[delegate.aniversaryInfo removeAllObjects];
[delegate.msgInfo removeAllObjects];
[delegate.urlInfo removeAllObjects];

abClicked = TRUE;

//Name property
NSMutableString* ms = [[NSMutableString alloc] initWithString:@””];
CFStringRef prefix = ABRecordCopyValue(ref, kABPersonPrefixProperty);
CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
CFStringRef middleName = ABRecordCopyValue(ref, kABPersonMiddleNameProperty);
CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
CFStringRef suffix = ABRecordCopyValue(ref, kABPersonSuffixProperty);
if(prefix){
if([ms length] > 0)
[ms appendString:@” “];
[ms appendString:[NSString stringWithFormat:@”%@”,prefix]];
[delegate.nameInfo addObject:[NSString stringWithFormat:@”%@”,prefix]];
CFRelease(prefix);
}
else
[delegate.nameInfo addObject:@””];

if(firstName){
if([ms length] > 0)
[ms appendString:@” “];
[ms appendString:[NSString stringWithFormat:@”%@”,firstName]];
[delegate.nameInfo addObject:[NSString stringWithFormat:@”%@”,firstName]];
CFRelease(firstName);
}
else
[delegate.nameInfo addObject:@””];

if(middleName){
if([ms length] > 0)
[ms appendString:@” “];
[ms appendString:[NSString stringWithFormat:@”%@”,middleName]];
[delegate.nameInfo addObject:[NSString stringWithFormat:@”%@”,middleName]];
CFRelease(middleName);
}
else
[delegate.nameInfo addObject:@””];

if(lastName){
if([ms length] > 0)
[ms appendString:@” “];
[ms appendString:[NSString stringWithFormat:@”%@”,lastName]];
[delegate.nameInfo addObject:[NSString stringWithFormat:@”%@”,lastName]];
CFRelease(lastName);
}
else
[delegate.nameInfo addObject:@””];

if(suffix){
if([ms length] > 0)
[ms appendString:@” “];
[ms appendString:[NSString stringWithFormat:@”%@”,suffix]];
[delegate.nameInfo addObject:[NSString stringWithFormat:@”%@”,suffix]];
CFRelease(suffix);
}
else
[delegate.nameInfo addObject:@””];

//[delegate.nameInfo addObject:[NSString stringWithFormat:@”%@”,ms]];
NSLog(@”Name = %@”,ms);
[ms release];

//Phonetics property
ms = [[NSMutableString alloc] initWithString:@””];
CFStringRef nickName = ABRecordCopyValue(ref, kABPersonNicknameProperty);
CFStringRef firstNamePhonetics = ABRecordCopyValue(ref, kABPersonFirstNamePhoneticProperty);
CFStringRef middleNamePhonetics = ABRecordCopyValue(ref, kABPersonMiddleNamePhoneticProperty);
CFStringRef lastNamePhonetics = ABRecordCopyValue(ref, kABPersonLastNamePhoneticProperty);

if(firstNamePhonetics){
[ms appendString:[NSString stringWithFormat:@”%@”,firstNamePhonetics]];
[delegate.nameInfo addObject:[NSString stringWithFormat:@”%@”,firstNamePhonetics]];
CFRelease(firstNamePhonetics);
}
else
[delegate.nameInfo addObject:@””];

if(middleNamePhonetics){
if([ms length] > 0)
[ms appendString:@” “];
[ms appendString:[NSString stringWithFormat:@”%@”,middleNamePhonetics]];
[delegate.nameInfo addObject:[NSString stringWithFormat:@”%@”,middleNamePhonetics]];
CFRelease(middleNamePhonetics);
}
else
[delegate.nameInfo addObject:@””];

if(lastNamePhonetics){
if([ms length] > 0)
[ms appendString:@” “];
[ms appendString:[NSString stringWithFormat:@”%@”,lastNamePhonetics]];
[delegate.nameInfo addObject:[NSString stringWithFormat:@”%@”,lastNamePhonetics]];
CFRelease(lastNamePhonetics);
}
else
[delegate.nameInfo addObject:@””];

//[delegate.nameInfo addObject:[NSString stringWithFormat:@”%@”,ms]];
if(nickName){
[delegate.nameInfo addObject:[NSString stringWithFormat:@”%@”,nickName]];
CFRelease(nickName);
}
else
[delegate.nameInfo addObject:@””];
//delegate.Name = ms;
NSLog(@”Phonetic Name = %@”,ms);
[ms release];

//person organization
ms = [[NSMutableString alloc] initWithString:@””];
CFStringRef company = ABRecordCopyValue(ref, kABPersonOrganizationProperty);
CFStringRef jobTitle = ABRecordCopyValue(ref, kABPersonJobTitleProperty);
CFStringRef department = ABRecordCopyValue(ref, kABPersonDepartmentProperty);

if(company){
[ms appendString:[NSString stringWithFormat:@”%@”,company]];
[delegate.nameInfo addObject:[NSString stringWithFormat:@”%@”,company]];
CFRelease(company);
}
else
[delegate.nameInfo addObject:@””];

if(jobTitle){
if([ms length] > 0)
[ms appendString:@” “];
[ms appendString:[NSString stringWithFormat:@”%@”,jobTitle]];
[delegate.nameInfo addObject:[NSString stringWithFormat:@”%@”,jobTitle]];
CFRelease(jobTitle);
}
else
[delegate.nameInfo addObject:@””];

if(department){
if([ms length] > 0)
[ms appendString:@” “];
[ms appendString:[NSString stringWithFormat:@”%@”,department]];
[delegate.nameInfo addObject:[NSString stringWithFormat:@”%@”,department]];
CFRelease(department);
}
else
[delegate.nameInfo addObject:@””];

//[delegate.nameInfo addObject:[NSString stringWithFormat:@”%@”,ms]];
NSLog(@”Organization = %@”,ms);
[ms release];

//person birth day
CFDateRef birthDate = ABRecordCopyValue(ref, kABPersonBirthdayProperty);
NSDateFormatter *dateFormatter = nil;

if(birthDate)
{
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
}
NSString* birthDay = [dateFormatter stringFromDate:(NSDate*)birthDate ];
if (dateFormatter != nil)
[dateFormatter release];
CFRelease(birthDate);
delegate.birthDay = birthDay;
NSLog(@”Birth Day = %@”,birthDay);
}
else
delegate.birthDay = @”No Birth Day found”;

//person Notes
CFStringRef note = ABRecordCopyValue(ref, kABPersonNoteProperty);
if(note)
{
NSLog(@”Notes = %@”,note);
delegate.Notes1 = [NSString stringWithFormat:@”%@”,note];
CFRelease(note);
}
else
delegate.Notes1 = @”No Note found”;

//person creation date
CFDateRef creationDate = ABRecordCopyValue(ref, kABPersonCreationDateProperty);
dateFormatter = nil;

if(creationDate)
{
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
}
NSString* creationDay = [dateFormatter stringFromDate:(NSDate*)creationDate ];
if (dateFormatter != nil)
[dateFormatter release];
CFRelease(creationDate);
delegate.creationDay = creationDay;
NSLog(@”Creation Day = %@”,creationDay);
}
else
delegate.creationDay = @”No Creation date found”;
//person modification date
CFDateRef modifyDate = ABRecordCopyValue(ref, kABPersonModificationDateProperty);
dateFormatter = nil;
if(modifyDate)
{
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
}
NSString* modifyDay = [dateFormatter stringFromDate:(NSDate*)modifyDate ];
if (dateFormatter != nil)
[dateFormatter release];
CFRelease(modifyDate);
delegate.modifyDay = modifyDay;
NSLog(@”Modification Day = %@”,modifyDay);
}
else
delegate.modifyDay = @”No Modify date found”;

//person email
ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty);
if(ABMultiValueGetCount(emails) != 0){
for(int i=0;i<ABMultiValueGetCount(emails);i++)
{
CFStringRef em = ABMultiValueCopyValueAtIndex(emails, i);
NSLog(@”Email-%i = %@”,i+1,[NSString stringWithFormat:@”%@”,em]);
[delegate.emailInfo addObject:[NSString stringWithFormat:@”%@”,em]];
CFRelease(em);
}
}
else
[delegate.emailInfo addObject:@”No Email Found”];
//NSLog(@”No email found”);
//self.email = @””;
CFRelease(emails);

//Person address
ABMultiValueRef addresses = ABRecordCopyValue(ref, kABPersonAddressProperty);
if(ABMultiValueGetCount(addresses) != 0){
for(int i=0;i<ABMultiValueGetCount(addresses);i++)
{
CFStringRef address = ABMultiValueCopyValueAtIndex(addresses, i);
//self.email = [NSString stringWithFormat:@”%@”,em];
NSLog(@”Address-%i = %@”,i+1,[NSString stringWithFormat:@”%@”,address]);
NSString* adrs = [NSString stringWithFormat:@”%@”,[(NSDictionary*)address objectForKey:@”City”]];
[delegate.addressInfo addObject:adrs];
adrs = [NSString stringWithFormat:@”%@”,[(NSDictionary*)address objectForKey:@”Country”]];
NSLog(@”country=%@”,adrs);
if([adrs isEqual:@”(null)”])
{
adrs = [NSString stringWithFormat:@”%@”,[(NSDictionary*)address objectForKey:@”State”]];
NSLog(@”State =%@”,adrs);
[delegate.addressInfo addObject:adrs];
}
else
[delegate.addressInfo addObject:adrs];

adrs = [NSString stringWithFormat:@”%@”,[(NSDictionary*)address objectForKey:@”CountryCode”]];
[delegate.addressInfo addObject:adrs];
NSString* street = (NSString*)[(NSDictionary*)address objectForKey:@”Street”];
street = [[street stringByReplacingOccurrencesOfString:@”\n” withString:@”,”] stringByReplacingOccurrencesOfString:@”\”” withString:@””];
NSLog(@”street = %@”,street);
[delegate.addressInfo addObject:street];
adrs = [NSString stringWithFormat:@”%@”,[(NSDictionary*)address objectForKey:@”ZIP”]];
[delegate.addressInfo addObject:adrs];
CFRelease(address);
}
}
else
[delegate.addressInfo addObject:@”No Address Found”];
//self.email = @””;
CFRelease(addresses);

//person Aniversary
ABMultiValueRef dates = ABRecordCopyValue(ref, kABPersonDateProperty);
if(ABMultiValueGetCount(dates) != 0){
for(int i=0;i<ABMultiValueGetCount(dates);i++)
{
CFStringRef aniversary = ABMultiValueCopyValueAtIndex(dates, i);
//self.email = [NSString stringWithFormat:@”%@”,em];
NSLog(@”Aniversary Date-%i = %@”,i+1,[NSString stringWithFormat:@”%@”,aniversary]);
[delegate.aniversaryInfo addObject:[NSString stringWithFormat:@”%@”,aniversary]];
CFRelease(aniversary);
}
}
else
[delegate.aniversaryInfo addObject:@”No Aniversary”];
//self.email = @””;
CFRelease(dates);

//Person Phone Number
ABMultiValueRef phoneNumbers = ABRecordCopyValue(ref, kABPersonPhoneProperty);
if(ABMultiValueGetCount(phoneNumbers) != 0){
for(int i=0;i<ABMultiValueGetCount(phoneNumbers);i++)
{
CFStringRef phoneNumber = ABMultiValueCopyValueAtIndex(phoneNumbers, i);
//self.email = [NSString stringWithFormat:@”%@”,em];
NSLog(@”Phone Number-%i = %@”,i+1,[NSString stringWithFormat:@”%@”,phoneNumber]);
[delegate.phoneNoInfo addObject:(NSString*)phoneNumber];
CFRelease(phoneNumber);
}
}
else
[delegate.phoneNoInfo addObject:@”No Phone Number”];

//self.email = @””;
CFRelease(phoneNumbers);

//Person Message
ABMultiValueRef messages = ABRecordCopyValue(ref, kABPersonInstantMessageProperty);
if(ABMultiValueGetCount(messages) != 0){
for(int i=0;i<ABMultiValueGetCount(messages);i++)
{
CFStringRef msg = ABMultiValueCopyValueAtIndex(messages, i);
//self.email = [NSString stringWithFormat:@”%@”,em];
NSLog(@”Message-%i = %@”,i+1,[NSString stringWithFormat:@”%@”,msg]);
[delegate.msgInfo addObject:[(NSDictionary*)msg objectForKey:@”service”]];
[delegate.msgInfo addObject:[(NSDictionary*)msg objectForKey:@”username”]];
CFRelease(msg);
}
}
else
[delegate.msgInfo addObject:@”No Messanger Address”];

//self.email = @””;
CFRelease(messages);

//Person URL
ABMultiValueRef urls = ABRecordCopyValue(ref, kABPersonURLProperty);
if(ABMultiValueGetCount(urls) != 0){
for(int i=0;i<ABMultiValueGetCount(urls);i++)
{
CFStringRef url = ABMultiValueCopyValueAtIndex(urls, i);
//self.email = [NSString stringWithFormat:@”%@”,em];
NSLog(@”URL-%i = %@”,i+1,[NSString stringWithFormat:@”%@”,url]);
[delegate.urlInfo addObject:(NSString*)url];
CFRelease(url);
}
}
else
[delegate.urlInfo addObject:@”No URL found”];
//self.email = @””;
CFRelease(urls);

//Person Related Name
/*ABMultiValueRef relatedNames = ABRecordCopyValue(ref, kABPersonRelatedNamesProperty);
if(ABMultiValueGetCount(relatedNames) != 0){
for(int i=0;i<ABMultiValueGetCount(relatedNames);i++)
{
CFStringRef relatedName = ABMultiValueCopyValueAtIndex(relatedNames, i);
//self.email = [NSString stringWithFormat:@”%@”,em];
NSLog(@”Related Name-%i = %@”,i+1,[NSString stringWithFormat:@”%@”,relatedName]);
CFRelease(relatedName);
}
}
else
NSLog(@”No Related Name found”);
//self.email = @””;
CFRelease(relatedNames);*/

//person Generic Properties
/*CFStringRef work = ABRecordCopyValue(ref, kABWorkLabel);
NSLog(@”Work = %@”,work);
CFRelease(work);
CFStringRef Home = ABRecordCopyValue(ref, kABHomeLabel);
NSLog(@”Work = %@”,Home);
CFRelease(Home);
CFStringRef Other = ABRecordCopyValue(ref, kABOtherLabel);
NSLog(@”Work = %@”,Other);
CFRelease(Other);*/

//person image
CFDataRef imgeData = ABPersonCopyImageData(ref);
if(imgeData)
{
delegate.Image =  [UIImage imageWithData:(NSData*)imgeData];
CFRelease(imgeData);
}
else
{
delegate.Image = [UIImage imageNamed:@”photo.png”];
}

NSLog(@”……..Finish……”);

[self dismissModalViewControllerAnimated:YES];

[self.addMealTable reloadData];
return NO;
}

– (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
return NO;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s