0

I am new to iphone. I am stuck in my project in the step of getting all the address book contacts mainly(name and email) which are placed in iphone device into my table view which is created in my project. How can I do it?

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
user1268135
  • 57
  • 1
  • 7
  • duplicate question at http://stackoverflow.com/questions/1558543/iphone-address-book-sample-code the official doc is here https://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/ https://developer.apple.com/iPhone/library/documentation/AddressBookUI/Reference/AddressBookUI_Functions/index.html – adali May 09 '12 at 06:25

3 Answers3

3
NSMutableArray* contactsArray = [NSMutableArray new];

// open the default address book. 
ABAddressBookRef m_addressbook = ABAddressBookCreate();

if (!m_addressbook) 
{
    NSLog(@"opening address book");
}
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
for (int i=0;i < nPeople;i++) 
{ 
    NSMutableDictionary* tempContactDic = [NSMutableDictionary new];
    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
    CFStringRef firstName, lastName;
    firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
    lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
    [tempContactDic setValue:name forKey:@"name"];
    //fetch email id
    NSString *strEmail;
    ABMultiValueRef email = ABRecordCopyValue(ref, kABPersonEmailProperty);
    CFStringRef tempEmailref = ABMultiValueCopyValueAtIndex(email, 0);
    strEmail = (__bridge  NSString *)tempEmailref;

    [tempContactDic setValue:strEmail forKey:@"email"];

     [contactsArray addObject:tempContactDic];

}

all contacts Data saved in Contacts Array. Then you can use this array according to your need.

Bond
  • 328
  • 4
  • 16
0

use addressbook api to do this. see my answer here

Contact list on iPhone without using contact picker

and for more details get the apple documentation here.

Community
  • 1
  • 1
Saad
  • 8,857
  • 2
  • 41
  • 51
0
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

-(void)viewWillAppear:(BOOL)animated{

    NSMutableArray* contactsArray = [NSMutableArray new];

    ABAddressBookRef m_addressbook = ABAddressBookCreate();

    if (!m_addressbook)
    {
        NSLog(@"opening address book");
    }
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
    CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
    for (int i=0;i < nPeople;i++)
    {
        NSMutableDictionary* tempContactDic = [NSMutableDictionary new];
        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
        NSLog(@"tempContactDic ios a ==%@",tempContactDic);
       CFStringRef firstName, lastName;
        firstName = (__bridge CFStringRef)((__bridge UILabel *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty)));
        lastName  = (__bridge CFStringRef)((__bridge UILabel *)(ABRecordCopyValue(ref, kABPersonLastNameProperty)));
      //  [tempContactDic setValue:name forKey:@"name"];
        //fetch email id
        NSString *strEmail;
        ABMultiValueRef email = ABRecordCopyValue(ref, kABPersonEmailProperty);
        CFStringRef tempEmailref = ABMultiValueCopyValueAtIndex(email, 0);
        strEmail = (__bridge  NSString *)tempEmailref;

        [tempContactDic setValue:strEmail forKey:@"email"];

        [contactsArray addObject:tempContactDic];

    }

}
-(IBAction)getcontactlist:(id)sender{

         [self getContact];
}

-(IBAction)getContact {
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    [self presentViewController:picker animated:YES completion:nil];

}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {

    firstName.text = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    lastName.text = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
    number.text = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, 0);
    return YES;
}

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

This code is working fine for me...

Darshan Kunjadiya
  • 3,323
  • 1
  • 29
  • 31
sarit bahuguna
  • 875
  • 8
  • 8