我正在尝试从通讯簿中的联系人中获取 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-推荐答案 strong>
电话号码是一个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/
|