2

I want reference of my address book in one of iPhone application. I have used below code. But its showing [people count] = 0.

I have 3-4 contacts on my addressbook application still [people count] returns zero.

Is anything more, I am missing here?

Code is:

ABAddressBookRef addressBook = ABAddressBookCreate();

    NSMutableArray *people = [[[(NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook) autorelease] mutableCopy]autorelease];
    [people sortUsingFunction:(int (*)(id,id,void*)) ABPersonComparePeopleByName context:(void*)ABPersonGetSortOrdering()];
    NSLog(@"count..%i",[people count]);

    for(int i = 0 ; i < [people count]; i++){
        ABRecordRef person = [people objectAtIndex:i];
        //get phone no fill phone no into  array
        ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
        if(multi!=NULL && ABMultiValueGetCount(multi)>0) {
            NSLog(@"in value..");
        }
        else {
            NSLog(@"no value");
        }

    }

Thank You

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
spaleja
  • 1,435
  • 15
  • 24

2 Answers2

2

try that -

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people;
people = ABAddressBookCopyArrayOfAllPeople(HiByeGroup);

if (people){
   CFArrayCreateMutableCopy(kCFAllocatorDefault,CFArrayGetCount(people),people);
    NSInteger numberOfPersonsInAB=CFArrayGetCount(peopleMutable);

    for (int i =0; i< numberOfPersonsInAB; i++) {
        //your code
       }
 CFRelease(people);

UPdate As Jamon Holmgren noted, this answer is old and ABAddressBookCreate() is deprecated in iOS6. You need to use ABAddressBookCreateWithOptions() instead. GOOD LUCK

shannoga
  • 19,649
  • 20
  • 104
  • 169
  • 1
    Note that in iOS 6 ABAddressBookCreate() is deprecated. Use ABAddressBookCreateWithOptions(). More info here: http://stackoverflow.com/questions/12083643/how-do-i-correctly-use-abaddressbookcreatewithoptions-method-in-ios-6/12510649#12510649 – Jamon Holmgren Oct 19 '12 at 15:27
  • By the way why you do not call CFRelease for addressBook? – user4951 Feb 28 '14 at 08:15
1

Just follow given code over here. A perfect example to interact with ABAddressBook.

Community
  • 1
  • 1
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54