获取姓名和号码联系人列表地址簿

Fetch Name AND Number contact list Address Book

我正在尝试 NSLog 通讯录中联系人的姓名和号码。 我登录名字成功了,但是我没有成功解决数字问题

这是我的代码:

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
if (addressBook != NULL) {
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        if (granted) {
            CFArrayRef allNames = ABAddressBookCopyArrayOfAllPeople(addressBook);

            if (allNames != NULL) {
                NSMutableArray *names = [NSMutableArray array];
                for (int i = 0; i < CFArrayGetCount(allNames); i++) {
                    ABRecordRef group = CFArrayGetValueAtIndex(allNames, i);
                    CFStringRef name = ABRecordCopyCompositeName(group);
                    [names addObject:(__bridge NSString *)name];
                    CFRelease(name);
                }


                NSLog(@"names = %@", names);
                CFRelease(allNames);
            }
        }
        CFRelease(addressBook);
    });
}

也许我必须创建 NSDictionnary ?不知道怎么解决...

您需要使用 ABRecordCopyValue(group, kABPersonPhoneProperty),其中 returns ABMultiValueRef。有关更多信息,请参阅 。

phone 个数字是 ABMultiValueRef:

ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
if (phones != NULL) {
    for (NSInteger index = 0; index < ABMultiValueGetCount(phones); index++) {
        NSString *phone = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, index));
        NSString *label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, index));  // either kABHomeLabel or kABPersonPhoneMainLabel or ...
        // do something with `phone` and `label`
    }
    CFRelease(phones);
}