Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

objective c - Get iPhone phone number label from Address Book

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: enter image description here

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];
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Simply use -

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];
}

EDIT Please see the notes for this answer about CFBridgingRelease and __bridge_transfer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...