iphone - 在 iOS 7 中获取联系人

标签 iphone ios ipad ios7

此代码适用于 iOS5/iOS6,但不适用于 iOS7。

CFErrorRef *error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    //ABAddressBookRef addressBook = ABAddressBookCreate();
    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);
        NSString *phoneNumber;
        for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
            phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
            //   NSLog(@"phone:%@", phoneNumber);
        }

最佳答案

今天更新了我的示例并消除了内存泄漏:)

 + (NSArray *)getAllContacts {

    CFErrorRef *error = nil;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
    CFArrayRef allPeople = (ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName));
    //CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
      CFIndex nPeople = CFArrayGetCount(allPeople); // bugfix who synced contacts with facebook
    NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];

    if (!allPeople || !nPeople) {
        NSLog(@"people nil");
    }


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

        @autoreleasepool {

            //data model
            ContactsData *contacts = [ContactsData new];

            ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

            //get First Name
            CFStringRef firstName = (CFStringRef)ABRecordCopyValue(person,kABPersonFirstNameProperty);
            contacts.firstNames = [(__bridge NSString*)firstName copy];

            if (firstName != NULL) {
                CFRelease(firstName);
            }


            //get Last Name
            CFStringRef lastName = (CFStringRef)ABRecordCopyValue(person,kABPersonLastNameProperty);
            contacts.lastNames = [(__bridge NSString*)lastName copy];

            if (lastName != NULL) {
                CFRelease(lastName);
            }


            if (!contacts.firstNames) {
                contacts.firstNames = @"";
            }

            if (!contacts.lastNames) {
                contacts.lastNames = @"";
            }



            contacts.contactId = ABRecordGetRecordID(person);
            //append first name and last name
            contacts.fullname = [NSString stringWithFormat:@"%@ %@", contacts.firstNames, contacts.lastNames];


            // get contacts picture, if pic doesn't exists, show standart one
            CFDataRef imgData = ABPersonCopyImageData(person);
            NSData *imageData = (__bridge NSData *)imgData;
            contacts.image = [UIImage imageWithData:imageData];

            if (imgData != NULL) {
                CFRelease(imgData);
            }

            if (!contacts.image) {
                contacts.image = [UIImage imageNamed:@"avatar.png"];
            }


            //get Phone Numbers
            NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
            ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);

            for(CFIndex i=0; i<ABMultiValueGetCount(multiPhones); i++) {
                @autoreleasepool {
                    CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
                    NSString *phoneNumber = CFBridgingRelease(phoneNumberRef);
                    if (phoneNumber != nil)[phoneNumbers addObject:phoneNumber];
                    //NSLog(@"All numbers %@", phoneNumbers);
                }
            }

            if (multiPhones != NULL) {
                CFRelease(multiPhones);
            }

            [contacts setNumbers:phoneNumbers];

            //get Contact email
            NSMutableArray *contactEmails = [NSMutableArray new];
            ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);

            for (CFIndex i=0; i<ABMultiValueGetCount(multiEmails); i++) {
                @autoreleasepool {
                    CFStringRef contactEmailRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
                    NSString *contactEmail = CFBridgingRelease(contactEmailRef);
                    if (contactEmail != nil)[contactEmails addObject:contactEmail];
                    // NSLog(@"All emails are:%@", contactEmails);
                }
            }

            if (multiEmails != NULL) {
                CFRelease(multiEmails);
            }

            [contacts setEmails:contactEmails];

            [items addObject:contacts];

#ifdef DEBUG
            //NSLog(@"Person is: %@", contacts.firstNames);
            //NSLog(@"Phones are: %@", contacts.numbers);
            //NSLog(@"Email is:%@", contacts.emails);
#endif

        }
    } //autoreleasepool
    CFRelease(allPeople);
    CFRelease(addressBook);
    CFRelease(source);
    return items;

}

实际上最近使用CNContacts在Swift 3中编写了pod,可能需要它

Swift ContactBook Picker

关于iphone - 在 iOS 7 中获取联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19027118/

相关文章:

iphone - 如何制作一个显示下拉菜单的按钮?

iphone - Google App Engine Java - 对于 POST 参数,request.getParameter 在部署时返回 null,但在本地工作

iphone - 复制时如何用 NSFileManager 覆盖文件?

iphone - 使用 if 语句检查多个应用内购买

iphone - 在 iPhone 上创建文件夹的权限

ios - 使用 SQLite iOS 更改数据模型

iphone - 在 uitableviewcell 内滚动时防止表格 View 滚动

ios - reloadData 堆叠在旧数据之上

javascript - YouTube 视频上的叠加图像在 I-pod/I-phone 设备上不可点击

ios - 如何使用 SWRevealViewController 显示调整后的侧边栏?