从地址簿中获取 iPhone 电话号码标签

发布于 2024-10-24 05:43:03 字数 1076 浏览 2 评论 0原文

那么我有一个方法可以从iPhone上的通讯录中获取所有联系人电话号码,但是有没有办法获取电话号码标签呢?例如你可以这样做: 在此处输入图像描述

我希望修改打印标签的方法(例如 iPhone/Home/移动/等)。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

表情可笑 2024-10-31 05:43:03

只需使用 -

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 的注释。

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.

长途伴 2024-10-31 05:43:03
//get the particular contact or email from phone book

    - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)picker shouldContinueAfterSelectingPerson:(ABRecordRef)person
    {
        // Name of contact.

        NSString* name = (NSString *)ABRecordCopyCompositeName(person);

        // Numbers of selected contact

        ABMutableMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);

        NSMutableString *mobile = [[NSMutableString alloc] init];
        NSMutableString *office = [[NSMutableString alloc] init];

        // Getting if Mobile, Office(work) numbers exist

        for(CFIndex numberIndex = 0; numberIndex < ABMultiValueGetCount(phones); numberIndex++)
        {
            // Number in contact details of current index

        CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, numberIndex);

        // Label of Phone Number

        CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phones, numberIndex);
        NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);

        // Phone number

        NSString *phoneNumber = (NSString *)phoneNumberRef;

        // Release Phone Number and locationLabel reference object

        CFRelease(phoneNumberRef);
        CFRelease(locLabel);

        NSLog(@"  - %@ (%@)", phoneNumber, phoneLabel);

        if ([phoneLabel isEqualToString:NSLocalizedString(@"mobile", nil)])// Mobile number saving.
        {
            [mobile appendFormat:@"%@", phoneNumber];
        }
        else if ([phoneLabel isEqualToString:NSLocalizedString(@"work", nil)])// Office number saving.
        {
            [office appendFormat:@"%@", phoneNumber];
        }

        [phoneNumber release];
    }
    CFRelease(phones);

    // Emails of selected contact

    ABMutableMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);

    NSMutableString *generalMail = [[NSMutableString alloc] init];
    NSMutableString *officeMail = [[NSMutableString alloc] init];

    // Getting if Home, Office(work) mails exist

    for(CFIndex numberIndex = 0; numberIndex < ABMultiValueGetCount(emails); numberIndex++)
    {
        // Mail in contact details of current index

        CFStringRef mailRef = ABMultiValueCopyValueAtIndex(emails, numberIndex);

        // Label of Phone Number

        CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(emails, numberIndex);
        NSString *mailLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);

        // Phone number

        NSString *mail = (NSString *)mailRef;

        // Release Phone Number and locationLabel reference object

        CFRelease(mailRef);
        CFRelease(locLabel);
        NSLog(@"  - %@ (%@)", mail, mailLabel);

        if ([mailLabel isEqualToString:NSLocalizedString(@"mobile", nil)])// Home mail.
        {
            [generalMail appendFormat:@"%@", mail];
        }
        else if ([mailLabel isEqualToString:NSLocalizedString(@"work", nil)])// Office(Work) mail.
        {
            [officeMail appendFormat:@"%@", mail];
        }

        [mail release];
    }
    CFRelease(emails);

    [mobile release];
    [office release];

    [generalMail release];
    [officeMail release];

    [self dismissViewControllerAnimated:YES completion:nil];
    return NO;
}
//get the particular contact or email from phone book

    - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)picker shouldContinueAfterSelectingPerson:(ABRecordRef)person
    {
        // Name of contact.

        NSString* name = (NSString *)ABRecordCopyCompositeName(person);

        // Numbers of selected contact

        ABMutableMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);

        NSMutableString *mobile = [[NSMutableString alloc] init];
        NSMutableString *office = [[NSMutableString alloc] init];

        // Getting if Mobile, Office(work) numbers exist

        for(CFIndex numberIndex = 0; numberIndex < ABMultiValueGetCount(phones); numberIndex++)
        {
            // Number in contact details of current index

        CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, numberIndex);

        // Label of Phone Number

        CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phones, numberIndex);
        NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);

        // Phone number

        NSString *phoneNumber = (NSString *)phoneNumberRef;

        // Release Phone Number and locationLabel reference object

        CFRelease(phoneNumberRef);
        CFRelease(locLabel);

        NSLog(@"  - %@ (%@)", phoneNumber, phoneLabel);

        if ([phoneLabel isEqualToString:NSLocalizedString(@"mobile", nil)])// Mobile number saving.
        {
            [mobile appendFormat:@"%@", phoneNumber];
        }
        else if ([phoneLabel isEqualToString:NSLocalizedString(@"work", nil)])// Office number saving.
        {
            [office appendFormat:@"%@", phoneNumber];
        }

        [phoneNumber release];
    }
    CFRelease(phones);

    // Emails of selected contact

    ABMutableMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);

    NSMutableString *generalMail = [[NSMutableString alloc] init];
    NSMutableString *officeMail = [[NSMutableString alloc] init];

    // Getting if Home, Office(work) mails exist

    for(CFIndex numberIndex = 0; numberIndex < ABMultiValueGetCount(emails); numberIndex++)
    {
        // Mail in contact details of current index

        CFStringRef mailRef = ABMultiValueCopyValueAtIndex(emails, numberIndex);

        // Label of Phone Number

        CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(emails, numberIndex);
        NSString *mailLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);

        // Phone number

        NSString *mail = (NSString *)mailRef;

        // Release Phone Number and locationLabel reference object

        CFRelease(mailRef);
        CFRelease(locLabel);
        NSLog(@"  - %@ (%@)", mail, mailLabel);

        if ([mailLabel isEqualToString:NSLocalizedString(@"mobile", nil)])// Home mail.
        {
            [generalMail appendFormat:@"%@", mail];
        }
        else if ([mailLabel isEqualToString:NSLocalizedString(@"work", nil)])// Office(Work) mail.
        {
            [officeMail appendFormat:@"%@", mail];
        }

        [mail release];
    }
    CFRelease(emails);

    [mobile release];
    [office release];

    [generalMail release];
    [officeMail release];

    [self dismissViewControllerAnimated:YES completion:nil];
    return NO;
}
暖心男生 2024-10-31 05:43:03

如果您要将记录添加到 AddressBook,这些预定义常量可能就是您想要的,kABPersonPhoneMobileLabelkABPersonPhoneIPhoneLabel,它们在文件 ABPerson.h 中定义。

If you are adding records to the AddressBook, these predefined constants may be what you want, kABPersonPhoneMobileLabel, kABPersonPhoneIPhoneLabel, which are defined in the file ABPerson.h.

半枫 2024-10-31 05:43:03

以下内容应该有所帮助:

NSArray* AccountEmailAddresses(void)
{
    NSMutableArray *emailAddresses = [NSMutableArray array];
    @try
    {
        Class MailComposeController = NSClassFromString(@"MailComposeController") ?: NSClassFromString(@"MFMailComposeController");
        NSArray *accountEmailAddresses = [MailComposeController performSelector:@selector(accountEmailAddresses)];
        for (id address in accountEmailAddresses)
        {
            if ([address isKindOfClass:[NSString class]])
                [emailAddresses addObject:address];
        }
    }
    @catch (NSException *e) {}

    return [NSArray arrayWithArray:emailAddresses];
}


ABRecordRef ABGetMe(ABAddressBookRef addressBook)
{
    ABRecordRef me = NULL;
    NSArray *accountEmailAddresses = AccountEmailAddresses();
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex peopleCount = CFArrayGetCount(people);
    for (CFIndex i = 0; i < peopleCount; i++)
    {
        ABRecordRef record = CFArrayGetValueAtIndex(people, i);
        ABMultiValueRef emails = ABRecordCopyValue(record, kABPersonEmailProperty);
        if (emails)
        {
            CFIndex emailCount = ABMultiValueGetCount(emails);
            for (CFIndex j = 0; j < emailCount; j++)
            {
                CFStringRef email = ABMultiValueCopyValueAtIndex(emails, j);
                if (email)
                {
                    if ([accountEmailAddresses containsObject:(id)email])
                        me = record;

                    CFRelease(email);
                }
                if (me)
                    break;
            }
            CFRelease(emails);
        }
        if (me)
            break;
    }

    return me;
}

the following should help:

NSArray* AccountEmailAddresses(void)
{
    NSMutableArray *emailAddresses = [NSMutableArray array];
    @try
    {
        Class MailComposeController = NSClassFromString(@"MailComposeController") ?: NSClassFromString(@"MFMailComposeController");
        NSArray *accountEmailAddresses = [MailComposeController performSelector:@selector(accountEmailAddresses)];
        for (id address in accountEmailAddresses)
        {
            if ([address isKindOfClass:[NSString class]])
                [emailAddresses addObject:address];
        }
    }
    @catch (NSException *e) {}

    return [NSArray arrayWithArray:emailAddresses];
}


ABRecordRef ABGetMe(ABAddressBookRef addressBook)
{
    ABRecordRef me = NULL;
    NSArray *accountEmailAddresses = AccountEmailAddresses();
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex peopleCount = CFArrayGetCount(people);
    for (CFIndex i = 0; i < peopleCount; i++)
    {
        ABRecordRef record = CFArrayGetValueAtIndex(people, i);
        ABMultiValueRef emails = ABRecordCopyValue(record, kABPersonEmailProperty);
        if (emails)
        {
            CFIndex emailCount = ABMultiValueGetCount(emails);
            for (CFIndex j = 0; j < emailCount; j++)
            {
                CFStringRef email = ABMultiValueCopyValueAtIndex(emails, j);
                if (email)
                {
                    if ([accountEmailAddresses containsObject:(id)email])
                        me = record;

                    CFRelease(email);
                }
                if (me)
                    break;
            }
            CFRelease(emails);
        }
        if (me)
            break;
    }

    return me;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文