Retrieve all contacts phone numbers in iOS

后端 未结 4 1796
闹比i
闹比i 2020-11-27 06:49

So far I saw methods to get multiple phone numbers if I show a picker so user can select people and then get the phone number. What I want is retrieving all contacts

4条回答
  •  盖世英雄少女心
    2020-11-27 07:53

    Get permission to the address book or notify the user that the need to change the permission in their settings.

    CGFloat iOSVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
        if(iOSVersion >= 6.0) {
            // Request authorization to Address Book
            addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
            if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
                ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
                    //start importing contacts
                    if(addressBookRef) CFRelease(addressBookRef);
                });
            }
            else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
                // The user has previously given access, add the contact
                //start importing contacts
                if(addressBookRef) CFRelease(addressBookRef);
            }
            else {
                // The user has previously denied access
                // Send an alert telling user to change privacy setting in settings app
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unable to Access" message:@"Grant us access now!" delegate:self cancelButtonTitle:@"Not Now" otherButtonTitles:@"I'll Do It!", nil];
                [alert show];
                if(addressBookRef) CFRelease(addressBookRef);
            }
        } else {
            addressBookRef = ABAddressBookCreate();
            //start importing contacts
            if(addressBookRef) CFRelease(addressBookRef);
        }
    

    Get the records

    CFArrayRef records = ABAddressBookCopyArrayOfAllPeople(addressBook);
    NSArray *contacts = (__bridge NSArray*)records;
    CFRelease(records);
    
    for(int i = 0; i < contacts.count; i++) {
        ABRecordRef record = (__bridge ABRecordRef)[contacts objectAtIndex:i];
    }
    

    Get the phone number

        ABMultiValueRef phonesRef    = ABRecordCopyValue(recordRef, kABPersonPhoneProperty);
    
        if(phonesRef) {
            count = ABMultiValueGetCount(phonesRef);
            for(int ix = 0; ix < count; ix++){
                CFStringRef typeTmp = ABMultiValueCopyLabelAtIndex(phonesRef, ix);
                CFStringRef numberRef = ABMultiValueCopyValueAtIndex(phonesRef, ix);
                CFStringRef typeRef = ABAddressBookCopyLocalizedLabel(typeTmp);
    
                NSString *phoneNumber = (__bridge NSString *)numberRef;
                NSString *phoneType = (__bridge NSString *)typeRef;
    
    
                if(typeTmp) CFRelease(typeTmp);
                if(numberRef) CFRelease(numberRef);
                if(typeRef) CFRelease(typeRef);
            }
            CFRelease(phonesRef);
        }
    

    Keep in mind, some people have 20,000 contacts in their phone. If you plan on doing this, you'll probably have to multithread the process.

提交回复
热议问题