iPhone Address Book: How to get a list of only contacts with phone numbers?

送分小仙女□ 提交于 2020-01-01 03:31:09

问题


I'd like to get a list of all ABContacts that have a phone number and only those contacts. Any contacts with just an email I do not want to show.

Android has a field called HAS_PHNONE_NUMBER you can query on but I'm not seeing anything like that for iPhone.

For example:

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
//How do I filter people into an array of contacts that all have a phone number?

回答1:


You can use this code snippet

CFIndex numberOfPeople = CFArrayGetCount(_allPeople);
for (int i=0;i < numberOfPeople;++i) { 
    ABRecordRef ref = CFArrayGetValueAtIndex(_allPeople, i);
    ABMultiValueRef phones =(NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty);
    int phoneNumbersCount = ABMultiValueGetCount(phones);
    if (phoneNumbersCount>0)
    {
       // save this contact, it has phone number
    }
}



回答2:


There is no easy way or help like this in iOS, you have to parse your Array and if the people you are parsing have a phone number or a list of phones number not empty you add it to your final Array.



来源:https://stackoverflow.com/questions/5352938/iphone-address-book-how-to-get-a-list-of-only-contacts-with-phone-numbers

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