【问题标题】:fetching phone number from address book从通讯录中获取电话号码
【发布时间】:2012-10-27 09:08:07
【问题描述】:

我正在使用以下代码获取地址簿中所有联系人的手机号码(即第一个电话号码)。我想将所有这些号码存储到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" 
)

我该怎么做?

【问题讨论】:

    标签: ios abaddressbook


    【解决方案1】:

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

     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 7 上有没有办法做到这一点?
    【解决方案2】:

    我通过 Google 找到了这个,并以这个地址簿代码为例,但发现了一个错误,我会为其他来到这里的人更正。

    当使用ABMultiValueCopyValueAtIndex 时,它返回一个类型CFTypeRef。该函数仅获取该索引处的电话号码(在本例中为索引 0),在电话号码的情况下为 CFStringRef,不应将其转换为 NSArray*

    ABMultiValueCopyArrayOfAllValues 将是获取存储在联系人中的所有电话号码数组的正确调用。这可以存储到一个数组中并像 Sarafaraz 在他的回答中所做的那样记录。

    NSArray *_phoneNumbers = (__bridge NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
    

    如果您只想获取第一个电话号码,您可以这样做:

    CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phoneNumberProperty, 0);
    NSString *phoneString = (__bridge NSString*)phoneNumberRef;
    

    【讨论】:

      猜你喜欢
      • 2013-11-14
      • 1970-01-01
      • 2011-07-19
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-30
      • 1970-01-01
      相关资源
      最近更新 更多