ios - 从地址簿中获取电话号码

标签 ios abaddressbook

我正在使用以下代码获取通讯录中所有联系人的手机号码(即第一个电话号码)。我想将所有这些数字存储到 NSArray

self.dataSource = [[NSMutableArray alloc]init]; // dataSouce is delared in .h file
ABAddressBookRef addressBook = ABAddressBookCreate();
NSMutableArray *allPeople = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
int nPeople = ABAddressBookGetPersonCount(addressBook);

for(int i=0; i < nPeople; i++ ){
    ABRecordRef person = (__bridge ABRecordRef)([allPeople objectAtIndex:i]);
    NSString *name = @"";

    if(ABRecordCopyValue(person, kABPersonFirstNameProperty) != NULL)
        name = [[NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    [_dataSource addObject: name];

    ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
    _phoneNumbers = (__bridge NSArray*)ABMultiValueCopyValueAtIndex(phoneNumberProperty, 0);
    CFRelease(phoneNumberProperty);
    NSLog(@"Phone numbers = %@", _phoneNumbers);
}

但是当我使用 NSLog(@"Phone numbers = %@", _phoneNumbers); 时输出为

2012-11-07 15:31:06.116 contacts[4938:12b03] Phone numbers = 1 (800) 111-1111
2012-11-07 15:31:06.117 contacts[4938:12b03] Phone numbers = (222) 355-5668
2012-11-07 15:31:06.118 contacts[4938:12b03] Phone numbers = (910) 192-0192

我希望 NSArray 中的输出像

Phone numbers = ( 
"1 (800) 111-1111",
"(222) 355-5668",
"(910) 192-0192" 
)

我该怎么做?

最佳答案

你可以像这样简单地做到这一点

 self.dataSource = [[NSMutableArray alloc]init]; // dataSouce is delared in .h file
    ABAddressBookRef addressBook = ABAddressBookCreate();
    NSMutableArray *allPeople = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
    int nPeople = ABAddressBookGetPersonCount(addressBook);

    NSMutableArray *arrContacts = [[NSMutableArray alloc] init];

    for(int i=0; i < nPeople; i++ ){
        ABRecordRef person = (__bridge ABRecordRef)([allPeople objectAtIndex:i]);
        NSString *name = @"";

        if(ABRecordCopyValue(person, kABPersonFirstNameProperty) != NULL)
            name = [[NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        [_dataSource addObject: name];

        ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
        _phoneNumbers = (__bridge NSArray*)ABMultiValueCopyValueAtIndex(phoneNumberProperty, 0);
        CFRelease(phoneNumberProperty);
        NSLog(@"Phone numbers = %@", _phoneNumbers);
        [arrContacts addObject:_phoneNumbers];
    }
    NSLog(@"Phone numbers : %@",arrContacts);

关于ios - 从地址簿中获取电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13267545/

相关文章:

xcode - 按编号搜索并使用ABAddressBook获取图像

iphone - 通过内置 vCard 表示方法导出/导入时,不会传输联系人备注

ios - kABPersonEmailProperty 返回奇怪的东西

objective-c - 为MPMoviePlayerController设置新的contentURL

ios - 检查操作系统版本的特性/类?

ios - 如何让 Sprite 从屏幕顶部下来?

ios - OpenGL ES 2.0 纹理是透明的,但不显示后面的 OpenGL 内容

ios - Swift:REST JSON Get 请求带身份验证

ios - 我们可以在ios sdk中自定义ABNewPersonController吗?

swift - 没有姓名的联系人会导致应用程序在 iPhone 上崩溃