Missing contacts in ABAddressBookCopyArrayOfAllPeople

不打扰是莪最后的温柔 提交于 2019-12-08 01:34:37

问题


Im trying to find a phone number in the address book from my app and I was surprised not to find it. The thing is that I've printed all the numbers of my address book in the console accessed by my app and strangely some of the contacts are missing, I was comparing the output with my address book, it's only few, but still.

This is how Im accesing the 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;          
      }
 }

Any idea why Im not accesing all the contacts in my Address Book?

[EDIT] Weirdest thing is that when I use ABPeoplePickerNavigationController those missing contacts appear.


回答1:


Please try the below.

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;
            }
        }
    }
}


来源:https://stackoverflow.com/questions/13581740/missing-contacts-in-abaddressbookcopyarrayofallpeople

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!