guinnessman
guinnessman

Reputation: 65

ABAddressBookGetPersonWithRecordID return nil

My app stores the AddressBook recordIds of Contacts with the same name and later tries to present the addresses to the user to select the desired person. However, when I use the stored recordIds with ABAddressBookGetPersonWithRecordID, it returns nil. The code below represents the code project - I have "copied" the code that later tries to retrieve the Contact immediately below the code that stores the recordIds.

NSString *full = person.compositeName;
                CFArrayRef contacts = ABAddressBookCopyPeopleWithName(addressBook, (__bridge        CFStringRef)(full));
                CFIndex nPeople = CFArrayGetCount(contacts);
                if (nPeople)
                {
                    NSMutableArray *rIds = [[NSMutableArray alloc] init];
                    int numberOfContactsMatchingName = (int)CFArrayGetCount(contacts);
                    if (numberOfContactsMatchingName>1)
                    {
                        for ( int i=0; i<numberOfContactsMatchingName; ++i)
                        {

                            ABRecordID thisId = ABRecordGetRecordID(CFArrayGetValueAtIndex(contacts, i));
                            NSNumber *rid = [NSNumber numberWithInteger:thisId];
                            FLOG(@"%d Matched, this ID = %@", numberOfContactsMatchingName, rid);
                            [rIds addObject:rid];
                        }


                        for (int i=0; i<rIds.count; ++i)
                        {
                            //contactRecord = ABAddressBookGetPersonWithRecordID(addressBook, (ABRecordID)recId);
                            ABRecordRef contactRecord;
                            contactRecord = ABAddressBookGetPersonWithRecordID(addressBook, rIds[i]);
                            if (contactRecord)
                            {

                            }
                            else
                            {
                                FLOG (@"Noone found with recordId %@", rIds[i]);
                            }
                        }

So, for example, I just ran this, and it found two Contacts in the address book with the same name - with ids 143 and 305, but when I then tried ABAddressBookGetPersonWithRecordID with ids 143 and 305, both returned nil. What have I got wrong here?

Upvotes: 0

Views: 718

Answers (1)

Gismay
Gismay

Reputation: 804

ABAddressBookGetPersonWithRecordID expects the recordID to be an integer, whereas your code appears to be passing it an NSNumber object.

Try this;

contactRecord = ABAddressBookGetPersonWithRecordID(addressBook, [rIds[i] integerValue]);

Hope this helps.

Upvotes: 2

Related Questions