Search code examples
iphoneobjective-cabaddressbook

Extract entire addressbook name & number from ABperson objective c iphone


I need to know how to extract everyname & number from the address book in xcode for the iphone. I need to have the name and number in the following format:

John Doe:000000000000000 Jane Doe:000000000000000


Solution

  • Are you searching for all phone numbers of all people in the address book? Look at ABAddressBookCopyArrayOfAllPeople.

    You could use it like this: (untested coded in browser)

        ABAddressBookRef _addressBookRef = ABAddressBookCreate ();
        NSArray* allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(_addressBookRef);
        NSMutableArray* _allItems = [[NSMutableArray alloc] initWithCapacity:[allPeople count]]; // capacity is only a rough guess, but better than nothing
        for (id record in allPeople) {
            CFTypeRef phoneProperty = ABRecordCopyValue((ABRecordRef)record, kABPersonPhoneProperty);
            NSArray *phones = (NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty);
            CFRelease(phoneProperty);
            for (NSString *phone in phones) {
                NSString* compositeName = (NSString *)ABRecordCopyCompositeName((ABRecordRef)record);
                NSString* field = [NSString stringWithFormat@"%@:%@",compositeName,phone];
                [compositeName release];
                [_allItems addObject:field];
            }
            [phoness release];
        }
        CFRelease(_addressBookRef);
        [allPeople release];
        allPeople = nil;