logixologist
logixologist

Reputation: 3834

ABAddressBookCopyArrayOfAllPeople Not returning any People

I am starting to work with the ABAddress Book and using a very simple starting point... I want to get all the entries in my address book and put it into an array. It keeps showing 0 elements.

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
NSArray *allContacts = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

Doesn't the ABAddressBookCopyArrayOfAllPeople actually get all people from your contact list or did I misunderstand something.

NOTE TO ADMIN's My question was a Objective C related and not for SWIFT so this answer doesn't apply. In the recommended answer it explains why the OP's code migration from Objective C to Swift is flawed and doesn't work. My question is not. I am trying to understand why when I install this on my iPhone, which has hundreds of contacts allContacts has no items. I was trying to understand the correct way to put all my contacts into an array. Basically because of business need I am doing my own contact picker because I don't want to use Apple's built in one.

Upvotes: 1

Views: 515

Answers (1)

Andrei
Andrei

Reputation: 2607

First of all you are trying to use deprecated apis. This may be why ABAddressBookCreateWithOptions doesn't work.

However, my recommendation based on your edit (where you say that you want to build your own UI) is to use the Contacts framework.

See this github gist:

#import <Contacts/Contacts.h>

@implementation ContactsScan

- (void) contactScan
{
    if ([CNContactStore class]) {
        //ios9 or later
        CNEntityType entityType = CNEntityTypeContacts;
        if( [CNContactStore authorizationStatusForEntityType:entityType] == CNAuthorizationStatusNotDetermined)
         {
             CNContactStore * contactStore = [[CNContactStore alloc] init];
             [contactStore requestAccessForEntityType:entityType completionHandler:^(BOOL granted, NSError * _Nullable error) {
                 if(granted){
                     [self getAllContact];
                 }
             }];
         }
        else if( [CNContactStore authorizationStatusForEntityType:entityType]== CNAuthorizationStatusAuthorized)
        {
            [self getAllContact];
        }
    }
}

-(void)getAllContact
{
    if([CNContactStore class])
    {
        //iOS 9 or later
        NSError* contactError;
        CNContactStore* addressBook = [[CNContactStore alloc]init];
        [addressBook containersMatchingPredicate:[CNContainer predicateForContainersWithIdentifiers: @[addressBook.defaultContainerIdentifier]] error:&contactError];
        NSArray * keysToFetch =@[CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPostalAddressesKey];
        CNContactFetchRequest * request = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch];
        BOOL success = [addressBook enumerateContactsWithFetchRequest:request error:&contactError usingBlock:^(CNContact * __nonnull contact, BOOL * __nonnull stop){
            [self parseContactWithContact:contact];
        }];
    }
}

- (void)parseContactWithContact :(CNContact* )contact
{
    NSString * firstName =  contact.givenName;
    NSString * lastName =  contact.familyName;
    NSString * phone = [[contact.phoneNumbers valueForKey:@"value"] valueForKey:@"digits"];
    NSString * email = [contact.emailAddresses valueForKey:@"value"];
    NSArray * addrArr = [self parseAddressWithContac:contact];
}

- (NSMutableArray *)parseAddressWithContac: (CNContact *)contact
{
    NSMutableArray * addrArr = [[NSMutableArray alloc]init];
    CNPostalAddressFormatter * formatter = [[CNPostalAddressFormatter alloc]init];
    NSArray * addresses = (NSArray*)[contact.postalAddresses valueForKey:@"value"];
    if (addresses.count > 0) {
        for (CNPostalAddress* address in addresses) {
            [addrArr addObject:[formatter stringFromPostalAddress:address]];
        }
    }
    return addrArr;
}

@end

Note: This code was not written by me. The source can be found on github.

Upvotes: 1

Related Questions