iPhone - How to fetch the contact detail using address book?

abaddressbook, iphone

Solution

you can try this code .

ABAddressBookRef m_addressbook = ABAddressBookCreate();

if (!m_addressbook) {
    NSLog(@"opening address book");
}

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
NSLog(@"  cfindex %ld",nPeople);

for ( int i=0;i < nPeople;i++) { 
    NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];

        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);

    //For username and surname      
    CFStringRef firstName, lastName;
    firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
    lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
    [dOfPerson setObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"name"];

    //For Email ids
    ABMutableMultiValueRef eMail  = ABRecordCopyValue(ref, kABPersonEmailProperty);
    if(ABMultiValueGetCount(eMail) > 0) {
        [dOfPerson setObject:(NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"];
        [contactList addObject:dOfPerson];
    }

    //For Phone number
    NSString* mobileLabel;
    for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
           {
        mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, j);
        if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
        {
[dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
        }
        else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
        {
[dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
            break ;
        }

        }
    }

Problem

I want to fetch the contact who have a birthday saved in the contact information and retrieve details of these contacts like phone numbers, emails, birthdate etc. Here is what I tried: ``` ABAddressBookRef myAddressBook = ABAddressBookCreate(); NSArray *allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(myAddressBook); contactList = [[NSMutableArray alloc]init]; for (id record in allPeople) { NSMutableDictionary *newRecord = [[NSMutableDictionary alloc] init]; CFTypeRef bDayProperty = ABRecordCopyValue((ABRecordRef)record, kABPersonBirthdayProperty); if (ABRecordCopyValue((ABRecordRef)record, kABPersonBirthdayProperty)) { NSDate *date=(NSDate*)bDayProperty; [newRecord setObject:date forKey:@"birthDate"]; date=nil; [date release]; } CFRelease(myAddressBook); } ``` any idea?

Original source