OStack程序员社区-中国程序员成长平台

标题: ios - 获取姓名和号码联系人列表地址簿 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:54
标题: ios - 获取姓名和号码联系人列表地址簿

我正在尝试从通讯簿中的联系人中获取 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 ?不知道怎么解决...



Best Answer-推荐答案


电话号码是一个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);
}

关于ios - 获取姓名和号码联系人列表地址簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31627047/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4