Question

I am writing code that attempts to parse all contacts from the IOS address book contact list. It seems pretty straightforward, and I've got the contacts and respective emails coming back as a list.

One thing I noticed is that I have multiple Groups in my Iphone's contact list (I see this browsing the phone's native contact list):

  • All Facebook
  • All Gmail
  • All on My IPhone

My phone current ignores Facebook, and displays the other two in my list.

When I reviewed the output of my code's contacts, for my app, I realized it is only showing the contacts for "All Gmail" by default.

Is there something I need to do to display the "All on my iphone" contacts as well?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    // Request authorization to Address Book
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            if (granted) {
                // First time access has been granted, add the contact
                [self addContactToAddressBook:addressBookRef];
            } else {
                // User denied access
                // Display an alert telling user the contact could not be added
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Contact list could not be loaded, since you denied access" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
            }
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        // The user has previously given access, add the contact
        [self addContactToAddressBook:addressBookRef];
    }
    else {
        // The user has previously denied access
        // Send an alert telling user to change privacy setting in settings app
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Please grant access to your contact list in Iphone Settings" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}

-(void)addContactToAddressBook:(ABAddressBookRef)addressBook {
    ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
    //CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
    CFIndex nPeople = CFArrayGetCount(allPeople);

    self.allContacts = [[NSMutableArray alloc] init];

   // int contactIndex = 0;
    for (int i = 0; i < nPeople; i++) {
        // Get the next address book record.
        ABRecordRef record = CFArrayGetValueAtIndex(allPeople, i);
        CFStringRef firstName, lastName, companyName;

        firstName = ABRecordCopyValue(record, kABPersonFirstNameProperty);
        lastName  = ABRecordCopyValue(record, kABPersonLastNameProperty);
        companyName  = ABRecordCopyValue(record, kABPersonOrganizationProperty);

        NSString *fullName = nil;
        if(firstName == nil && lastName == nil){
            fullName = [NSString stringWithFormat:@"%@", companyName];
        } else {
            if(firstName == nil){
                fullName = [NSString stringWithFormat:@"%@", lastName];
            } if(lastName == nil){
                fullName = [NSString stringWithFormat:@"%@", firstName];
            } else {
                fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
            }
        }

        // Get array of email addresses from address book record.
        ABMultiValueRef emailMultiValue = ABRecordCopyValue(record, kABPersonEmailProperty);
        NSArray *emailArray = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue);

        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:2];
        dict[@"fullname"] = fullName;
        dict[@"emailArr"] = (emailArray != nil ? emailArray : @[]);

       // if(emailArray != nil){
            [self.allContacts addObject:dict];
        //}
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
Was it helpful?

Solution

From the code in my working app, to get all the contacts in the Address Book, use the following code:

ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, nil);
NSMutableArray *contactRefs = [NSMutableArray array];
NSArray *sources = (__bridge NSArray *)(ABAddressBookCopyArrayOfAllSources(book));
for (id source in sources){
    NSArray *refs = (__bridge NSArray *)(ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(book, (__bridge ABRecordRef)(source), ABPersonGetSortOrdering()));
    [contactRefs addObjectsFromArray:refs];
}

Basically, what this does is iterates through all the groups, and adds the contacts from all the groups into contactRefs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top