ABAddressBookGetPersonWithRecordID返回nil

时间:2014-10-03 06:38:23

标签: ios iphone abaddressbook

我的应用程序存储具有相同名称的联系人的AddressBook记录ID,然后尝试向用户显示地址以选择所需的人员。但是,当我使用存储的recordIds和ABAddressBookGetPersonWithRecordID时,它返回nil。 下面的代码代表代码项目 - 我已经"复制"稍后尝试在存储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]);
                            }
                        }

所以,例如,我刚刚运行了这个,它在地址簿中找到了两个具有相同名称的联系人 - 使用ID 143和305,但是当我然后尝试使用ID 143和305的ABAddressBookGetPersonWithRecordID时,两者都返回了nil。 我在这里遇到了什么错误?

1 个答案:

答案 0 :(得分:2)

ABAddressBookGetPersonWithRecordID要求recordID为整数,而您的代码似乎将NSNumber对象传递给它。

试试这个;

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

希望这有帮助。