Get a list of all contacts on iOS

2019-01-02 19:49发布

I want to get a list of all contacts of an iPhone.

I checked Address Book reference, I may missed something but I didn't see it provides a method to get a list of contacts.

13条回答
孤独总比滥情好
2楼-- · 2019-01-02 20:03

Use this code to display all names + lastnames + phonenumbers (iOS 6). Works on simulator too:

CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

for(int i = 0; i < numberOfPeople; i++) {

    ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );

    NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
    NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
    NSLog(@"Name:%@ %@", firstName, lastName);

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

    for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
        NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
        NSLog(@"phone:%@", phoneNumber);
    }

    NSLog(@"=============================================");

}
查看更多
余生请多指教
3楼-- · 2019-01-02 20:04

Perhaps ABPerson function ABAddressBookCopyArrayOfAllPeople might do?

Example:

ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

for ( int i = 0; i < nPeople; i++ )
{
    ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );
    ...
}
查看更多
情到深处是孤独
4楼-- · 2019-01-02 20:04

Check out https://github.com/heardrwt/RHAddressBook (254 stars 01/2014).

Provides an ObjC wrapper for AddressBook with much simpler API.

查看更多
与风俱净
5楼-- · 2019-01-02 20:05

Make sure you have the proper import

#import <AddressBook/AddressBook.h>

Then you can get a CFArray object with all contacts using

CFArrayRef ABAddressBookCopyArrayOfAllPeople (ABAddressBookRef addressBook);
查看更多
看淡一切
6楼-- · 2019-01-02 20:06

This works for ios 7 and ios 8 , i hope its help you.............

NSMutableArray *result = [[NSMutableArray alloc] init];
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
__block BOOL accessGranted = NO;

if (ABAddressBookRequestAccessWithCompletion != NULL){
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);

    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        accessGranted = granted;
        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else{
    accessGranted = YES;
}
if (accessGranted){
    // If the app is authorized to access the first time then add the contact
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

    for (int i=0; i<numberOfPeople; i++){
        CFStringRef phone;
        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
        CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
        CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
        NSString *userName = @"NoName";

        userName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
        userName = [userName stringByReplacingOccurrencesOfString:@"(null)" withString:@""];

        ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
        CFIndex phoneNumberCount = ABMultiValueGetCount( phoneNumbers );

        phone = nil;

        for ( CFIndex ind= 0; ind<phoneNumberCount; ind++ ){
            CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex( phoneNumbers, ind);
            CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex( phoneNumbers, ind);

            // converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile"
            // Find the ones you want here
            if (phoneNumberLabel != nil){
                NSStringCompareOptions  compareOptions = NSCaseInsensitiveSearch;
                if(CFStringCompare(phoneNumberLabel, CFSTR("mobile"),compareOptions)){
                    phone = phoneNumberValue;
                }
                phone = phoneNumberValue;

                NSStringCompareOptions  compareOptionss = NSCaseInsensitiveSearch;
                if(!CFStringCompare(phone, CFSTR("1-800-MY-APPLE"),compareOptionss)){
                    continue;
                }
                NSMutableArray *theKeys = [NSMutableArray arrayWithObjects:@"name", @"small_name",@"phone", @"checked", nil];
                NSMutableArray *theObjects = [NSMutableArray arrayWithObjects:userName, [userName lowercaseString],phone, @"NO", nil];
                NSMutableDictionary *theDict = [NSMutableDictionary dictionaryWithObjects:theObjects forKeys:theKeys];
                if (![[functions formatNumber:(__bridge NSString *)(phone)] isEqualToString:[[NSUserDefaults standardUserDefaults]valueForKey:@"phoneNumber"]]){
                    [result addObject:theDict];
                }
            }
        }
    }
}
//sort array
NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@"small_name"
                                                            ascending:YES]; // 1
NSArray * sortedArray = [result sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];
查看更多
还给你的自由
7楼-- · 2019-01-02 20:08

if you want to sort as a alphabetical, you can use below code.

 CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(kCFAllocatorDefault,
                                                           CFArrayGetCount(allPeople),
                                                           allPeople);

CFArraySortValues(peopleMutable,
                  CFRangeMake(0, CFArrayGetCount(peopleMutable)),
                  (CFComparatorFunction) ABPersonComparePeopleByName,
                  kABPersonSortByFirstName);
查看更多
登录 后发表回答