Andre Cytryn
Andre Cytryn

Reputation: 2705

Pop up ABAddressBook modal with only contacts with email

I'm trying to show a modal of an ABAddressBook with only contacts that have an email registered. How do I achieve this?

I tried this code:

- (IBAction)getContact {
    // creating the picker
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];

    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
    CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

    for( CFIndex emailIndex = 0; emailIndex < nPeople; emailIndex++ ) {
        ABRecordRef person = CFArrayGetValueAtIndex( allPeople, emailIndex );
        ABMutableMultiValueRef emailRef= ABRecordCopyValue(person, kABPersonEmailProperty);
        int emailCount = ABMultiValueGetCount(emailRef);
        if(!emailCount) {
            CFErrorRef error = nil;
            ABAddressBookRemoveRecord(addressBook, person, &error);
            if (error) NSLog(@"Error: %@", error);
        }
    }
    picker.addressBook = addressBook;
    picker.peoplePickerDelegate = self;
    [self presentModalViewController:picker animated:YES];
}

The list shows up with all my contact, but the "removed" ones appear like "No Name", and the ones that have a name, have a real email.

Upvotes: 2

Views: 957

Answers (1)

Andre Cytryn
Andre Cytryn

Reputation: 2705

I have managed to create another solution...

I added the contacts that have at least one email at an array... Instead of the solution that would look to the current addressbook and remove the ones that didn't have any email in it. Here is the code:

    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
    CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

    for( CFIndex emailIndex = 0; emailIndex < nPeople; emailIndex++ ) {
        ABRecordRef person = CFArrayGetValueAtIndex( allPeople, emailIndex );
        ABMutableMultiValueRef emailRef= ABRecordCopyValue(person, kABPersonEmailProperty);
        int emailCount = ABMultiValueGetCount(emailRef);
        if(!emailCount) {
            CFErrorRef error = nil;
            ABAddressBookRemoveRecord(addressBook, person, &error);
            if (error) NSLog(@"Error: %@", error);
        } else {
            ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
            NSString *email = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, 0);
            NSString *name = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));

            if (name) {
                NSMutableDictionary *contactDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                             name, @"name",
                                             email, @"email",
                                             nil];
                [self.contactsArray addObject:contactDict];
            }
        }
    }

Upvotes: 2

Related Questions