-3

Hi I am developing an apps in that I got one requirement where I have a button action method when I click the button I should retrive whole address book contacts and display in the form of string how do i do this can anyone tel me with code. I am not able to this from couple of days.

I dont want to navigate using ABPeoplePickerNavigationController and access it ,What all I need is click on button access all contact and display in the form of string.

icodebuster
  • 8,890
  • 7
  • 62
  • 65
Abhilash
  • 638
  • 4
  • 11
  • 28
  • Pl use search. `ABAddressBookCopyArrayOfAllPeople` is what you need. – maroux May 31 '13 at 05:03
  • ABAddressBookRef addressBook = ABAddressBookCreate( ); CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook ); CFIndex nPeople = ABAddressBookGetPersonCount( addressBook ); for ( int i = 0; i < nPeople; i++ ) { ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i ); ... } – alpha May 31 '13 at 05:04

2 Answers2

1

Try this it works for iOS 6 as well as iOS 5.0 or older:

First add the following frameworks in Link Binary With Libraries

  • AddressBookUI.framework
  • AddressBook.framework

Them Import

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

Then use the following code

ABAddressBookRef addressBook = ABAddressBookCreate();

__block BOOL accessGranted = NO;

if (ABAddressBookRequestAccessWithCompletion != NULL) { // We are on iOS 6
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        accessGranted = granted;
        dispatch_semaphore_signal(semaphore);
    });

    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    dispatch_release(semaphore);
}

else { // We are on iOS 5 or Older
    accessGranted = YES;
    [self getContactsWithAddressBook:addressBook];
}

if (accessGranted) {
    [self getContactsWithAddressBook:addressBook];
}

// Get the contacts.
- (void)getContactsWithAddressBook:(ABAddressBookRef )addressBook {
    NSArray *arrayOfPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

    NSUInteger index = 0;
    NSMutableArray *firstNames = [[NSMutableArray alloc] init];

    for(index = 0; index <= ([arrayOfPeople count] - 1); index++){
        ABRecordRef currentPerson = (__bridge ABRecordRef)[arrayOfPeople objectAtIndex:index];
        NSString *currentFirstName = (__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonFirstNameProperty);

        // If first name is empty then don't add to the array
        if (![currentFirstName length] == 0) {
            [firstNames addObject: currentFirstName];
        }

    }
    //OPTIONAL: Use the following line to sort the first names alphabetically
    NSArray *sortedFirstNames = [firstNames sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSLog(@"Total Count = %d \n Sorted By Name = %@", [sortedFirstNames count], sortedFirstNames);
}

I tested this and it works.

icodebuster
  • 8,890
  • 7
  • 62
  • 65
0

try this for .

    ABAddressBookRef addressBook = ABAddressBookCreate();
    NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
    for(id person in people){
         NSString *name = [NSString stringWithFormat:@"%@ %@",(NSString *)ABRecordCopyValue( person, kABPersonFirstNameProperty ),(NSString *)ABRecordCopyValue( person, kABPersonLastNameProperty )];
    //fetch multiple phone nos.
        ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
        for (CFIndex j=0; j < ABMultiValueGetCount(multi); j++) {
            NSString* phone = (NSString*)ABMultiValueCopyValueAtIndex(multi, j);
            [numbersArray addObject:phone];
            [phone release];
        }

          /fetch multiple email ids.
        ABMultiValueRef multiemail = ABRecordCopyValue(person, kABPersonEmailProperty);
        for (CFIndex j=0; j < ABMultiValueGetCount(multiemail); j++) {
             NSString* email = (NSString*)ABMultiValueCopyValueAtIndex(multiemail, j);
             NSLog(@"Email=%@",email);
        }
    }

and you have to alloc your array before you use. in viewDidLoad method write this for alloc array

           numbersArray=[[NSMutableArray alloc] init];
SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45