在iOS中检索所有联系人电话号码

到目前为止,我看到了如果我显示一个选择器来获取多个电话号码的方法,所以用户可以选择人,然后获取电话号码.
我想要的是检索所有联系人的号码.
甚至有可能吗

解决方法

尝试这样做适用于iOS 6以及iOS 5.0或更早版本:

Sample Project Demo

首先在链接二进制库中添加以下框架

> AddressBookUI.framework
> AddressBook.framework

然后导入

#import <AddressBook/ABAddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

然后使用以下代码

请求访问通讯录的权限

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL,NULL);

__block BOOL accessGranted = NO;

if (&ABAddressBookRequestAccessWithCompletion != NULL) { // We are on iOS 6
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

    ABAddressBookRequestAccessWithCompletion(addressBook,^(bool granted,CFErrorRef error) {
        accessGranted = granted;
        dispatch_semaphore_signal(semaphore);
    });

    dispatch_semaphore_wait(semaphore,disPATCH_TIME_FOREVER);
    dispatch_release(semaphore);
}

else { // We are on iOS 5 or Older
    accessGranted = YES;
    [self getContactsWithAddressBook:addressBook];
}

if (accessGranted) {
    [self getContactsWithAddressBook:addressBook];
}

从通讯录中检索联系人

// Get the contacts.
- (void)getContactsWithAddressBook:(ABAddressBookRef )addressBook {

    contactList = [[NSMutableArray alloc] init];
    CFArrayRef allPeople = ABAddressBookcopyArrayOfAllPeople(addressBook);
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

    for (int i=0;i < nPeople;i++) {
        NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];

        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);

        //For username and surname
        ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge Nsstring*)ABRecordcopyValue(ref,kABPersonPhoneProperty));

        CFStringRef firstName,lastName;
        firstName = ABRecordcopyValue(ref,kABPersonFirstNameProperty);
        lastName  = ABRecordcopyValue(ref,kABPersonLastNameProperty);
        [dOfPerson setobject:[Nsstring stringWithFormat:@"%@ %@",firstName,lastName] forKey:@"name"];

        //For Email ids
        ABMutableMultiValueRef eMail  = ABRecordcopyValue(ref,kABPersonEmailProperty);
        if(ABMultiValueGetCount(eMail) > 0) {
            [dOfPerson setobject:(__bridge Nsstring *)ABMultiValuecopyValueAtIndex(eMail,0) forKey:@"email"];

        }

        //For Phone number
        Nsstring* mobileLabel;

        for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) {
            mobileLabel = (__bridge Nsstring*)ABMultiValuecopyLabelAtIndex(phones,j);
            if([mobileLabel isEqualToString:(Nsstring *)kABPersonPhoneMobileLabel])
            {
                [dOfPerson setobject:(__bridge Nsstring*)ABMultiValuecopyValueAtIndex(phones,j) forKey:@"phone"];
            }
            else if ([mobileLabel isEqualToString:(Nsstring*)kABPersonPhoneIPhoneLabel])
            {
                [dOfPerson setobject:(__bridge Nsstring*)ABMultiValuecopyValueAtIndex(phones,j) forKey:@"phone"];
                break ;
            }

        }
    [contactList addobject:dOfPerson];

    }
NSLog(@"Contacts = %@",contactList);
}

取回其他信息

// All Personal information Properties
kABPersonFirstNameProperty;          // First name - kABStringPropertyType
kABPersonLastNameProperty;           // Last name - kABStringPropertyType
kABPersonMiddleNameProperty;         // Middle name - kABStringPropertyType
kABPersonPrefixProperty;             // Prefix ("Sir" "Duke" "General") - kABStringPropertyType
kABPersonSuffixProperty;             // Suffix ("Jr." "Sr." "III") - kABStringPropertyType
kABPersonNicknameProperty;           // Nickname - kABStringPropertyType
kABPersonFirstNamePhoneticProperty;  // First name Phonetic - kABStringPropertyType
kABPersonLastNamePhoneticProperty;   // Last name Phonetic - kABStringPropertyType
kABPersonMiddleNamePhoneticProperty; // Middle name Phonetic - kABStringPropertyType
kABPersonorganizationProperty;       // Company name - kABStringPropertyType
kABPersonJobTitleProperty;           // Job Title - kABStringPropertyType
kABPersonDepartmentProperty;         // Department name - kABStringPropertyType
kABPersonEmailProperty;              // Email(s) - kABMultiStringPropertyType
kABPersonBirthdayProperty;           // Birthday associated with this person - kABDateTimePropertyType
kABPersonNoteProperty;               // Note - kABStringPropertyType
kABPersonCreationDateProperty;       // Creation Date (when first saved)
kABPersonModificationDateProperty;   // Last saved date

// All Address information Properties
kABPersonAddressproperty;            // Street address - kABMultiDictionaryPropertyType
kABPersonAddressstreetKey;
kABPersonAddressCityKey;
kABPersonAddressstateKey;
kABPersonAddressZIPKey;
kABPersonAddressCountryKey;
kABPersonAddressCountryCodeKey;

Further Reference Read Apple Docs

更新:
您需要添加关于为什么需要访问您的Apps-Info.plist中的联系人的说明

隐私 – 联系人使用说明

要么

<key>NSContactsUsageDescription</key>
<string>Write the reason why your app needs the contact.</string>

获取用户图像.

UIImage *contactimage;
if(ABPersonHasImageData(ref)){
 contactimage = [UIImage imageWithData:(__bridge NSData *)ABPersoncopyImageData(ref)];
}

注意:

AddressBook框架在iOS 9中已被弃用,并被新的和改进的Contacts Framework替代

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...