缺少ABAddressBookCopyArrayOfAllPeople中的联系人

时间:2012-11-27 10:05:42

标签: objective-c abaddressbook

我试图在我的应用程序中找到地址簿中的电话号码,我很惊讶没有找到它。 问题是我在我的应用程序访问的控制台中打印了我的地址簿的所有数字,奇怪的是有些联系人丢失,我将输出与我的地址簿进行比较,只有少数,但仍然。

这就是我访问AddressBook的方式:

 ABAddressBookRef addressBook = ABAddressBookCreate();
 NSArray *people = (NSArray *)   ABAddressBookCopyArrayOfAllPeople(addressBook);

 BOOL found = NO;
 NSString *name;
 int i = 0;
 while (!found) {//Here I print all the contact info, name and phone number
       ABRecordRef person = (ABRecordRef)[people objectAtIndex:i];
       ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
       NSLog(@"el telf: %@ y nombre %@",tempPhone2, [NSString stringWithFormat:@"%@ %@",ABRecordCopyValue(person, kABPersonFirstNameProperty) ? (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) : @"",ABRecordCopyValue(person, kABPersonLastNameProperty) ? (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty) : @""]);                           
      if([[key objectForKey:@"phone"] isEqualToString:tempPhone2]){
           found = YES;          
      }
 }

知道为什么我不能访问我的地址簿中的所有联系人吗?

[编辑] 最奇怪的是,当我使用ABPeoplePickerNavigationController时,会出现那些丢失的联系人。

1 个答案:

答案 0 :(得分:2)

请尝试以下方法。

ABAddressBookRef lAddressBook = ABAddressBookCreate();
CFArrayRef lRawAddressBookEntries =
ABAddressBookCopyArrayOfAllPeople(lAddressBook);
CFIndex lTotalContactsCount = ABAddressBookGetPersonCount(lAddressBook);
for (CFIndex i = 0; i < lTotalContactsCount; i++) {
    ABRecordRef lRef = CFArrayGetValueAtIndex(lRawAddressBookEntries, i);

    ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(lRef, kABPersonPhoneProperty);
    NSArray* phoneNumbers1 = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
    CFRelease(phoneNumberProperty);

    // Do whatever you want with the phone numbers
    if(phoneNumbers1 && [phoneNumbers1 isKindOfClass:[NSArray class]]) {
        for(NSString *stringPhoneNUmber in phoneNumbers1) {
            if([stringPhoneNUmber isEqualToString:tempPhone2]){
                found = YES;
            }
        }
    }
}