从地址簿获取iPhone电话号码标签 [英] Get iPhone phone number label from Address Book

查看:122
本文介绍了从地址簿获取iPhone电话号码标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有办法从iPhone上的地址簿中获取所有联系电话号码,但是有办法获得电话号码标签吗?例如,您可以这样做:

So I have a method to get all the contact phone numbers from the address book on the iPhone, but is there a way to get the phone number label? For example you can do this:

我希望修改我的方法来打印标签(例如iPhone / Home / mobile / etc)。

And I'd be looking to modify my method to print out the label (such as iPhone/Home/mobile/etc).

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef all = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex n = ABAddressBookGetPersonCount(addressBook);

for( int i = 0 ; i < n ; i++ )
{
    ABRecordRef ref = CFArrayGetValueAtIndex(all, i);
    NSString *firstName = (NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
    NSLog(@"Name %@", firstName);

    ABMultiValueRef *phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);
    for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
    {
        NSString *phoneLabel = @""; // ???

        CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);
        //CFRelease(phones);
        NSString *phoneNumber = (NSString *)phoneNumberRef;
        CFRelease(phoneNumberRef);
        NSLog(@"  - %@ (%@)", phoneNumber, phoneLabel);
        [phoneNumber release];
    }
}


推荐答案

简单使用 -

ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
{
  CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);
  CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phones, j);
  NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);
  //CFRelease(phones);
  NSString *phoneNumber = (NSString *)phoneNumberRef;
  CFRelease(phoneNumberRef);
  CFRelease(locLabel);
  NSLog(@"  - %@ (%@)", phoneNumber, phoneLabel);
  [phoneNumber release];
}

编辑
请参阅备注关于 CFBridgingRelease __ bridge_transfer 的答案。

这篇关于从地址簿获取iPhone电话号码标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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