检索iOS中的所有联系人电话号码 [英] Retrieve all contacts phone numbers in iOS

查看:132
本文介绍了检索iOS中的所有联系人电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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' numbers. Is it even possible?

推荐答案

试试这个适用于 iOS 6以及iOS 5.0或更早版本

示例项目演示

Sample Project Demo

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


  • AddressBookUI.framework

  • AddressBook.framework

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

进一步参考阅读Apple文档

Further Reference Read Apple Docs

更新
您需要添加有关您需要访问联系人的原因的说明 Apps-Info.plist

隐私 - 联系方式使用说明

OR

<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

The AddressBook framework is deprecated in iOS 9 and replaced with the new and improved Contacts Framework

这篇关于检索iOS中的所有联系人电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆