【问题标题】:How can I get all phone numbers from addressbook IOS 5 KABPersonPhoneProperty如何从通讯簿 IOS 5 KA PersonPhone 属性中获取所有电话号码
【发布时间】:2012-06-30 20:02:00
【问题描述】:

我正在使用 kABPersonPhoneProperty 从通讯录中获取 iPhone“电话号码”。但是,一旦我运行该程序并且只有一个电话号码出现,即使联系人有手机号码、家庭号码和 iphone 号码。请帮忙,我希望能从每个联系人那里获得所有电话号码。谢谢。

完整方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *simpleTableIdentifier = @"SimpleCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    // cell.textLabel.text = [people objectAtIndex:indexPath.row];

    ABRecordRef record = (__bridge   ABRecordRef) [people objectAtIndex:[indexPath row]];

    NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(record, kABPersonFirstNameProperty);

    NSString *lastName = (__bridge_transfer NSString*)ABRecordCopyValue(record, kABPersonLastNameProperty);

    NSLog(@"FirstName %@, LastName %@", firstName, lastName);

    cell.nameLabel.text = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

    ABMultiValueRef mainPhone = ABRecordCopyValue(record, kABPersonPhoneProperty);
    for (CFIndex i = 0; i < ABMultiValueGetCount(mainPhone); i ++) {        

        NSString *phoneMobileNumber = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(mainPhone, i);

        cell.telNo.text = [NSString stringWithFormat:@"%@ %@" , home, phoneMobileNumber];

        NSLog(@"%@,%@", home, phoneMobileNumber);
    }

return cell;

}

【问题讨论】:

  • 文本应该出现在哪里?在表格视图单元格中?
  • 是在表格视图单元格中,并且表格视图单元格具有联系人姓名和联系人号码的姓名和联系人号码字段。如何在联系号码字段中显示联系号码结果(包括 Main、Mobile 和 Iphone 在内的所有联系号码)?
  • 那么这是在&lt;UITableViewDataSource&gt; 方法中吗?
  • 我正在 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath from 协议中实现。
  • 你能把整个cellForRowAtIndexPath:方法贴出来吗?

标签: iphone objective-c ios5 xcode4.3


【解决方案1】:

好的,这里是解决方案

`    ABAddressBookRef addressBook=ABAddressBookCreate();
NSArray *allPeople=(NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);


NSMutableArray *holdEachPersonMobileNumbers=[NSMutableArray new];

for (id person in allPeople) {
    ABMultiValueRef phoneNumbers = ABRecordCopyValue(( ABRecordRef)(person), kABPersonPhoneProperty);

    NSString* mobile=@"";
    for (int i=0; i < ABMultiValueGetCount(phoneNumbers); i++) {

        mobile = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, i);

        NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:@"#();$&-+"];
        mobile = [[mobile componentsSeparatedByCharactersInSet: trim] componentsJoinedByString: @""];
        mobile= [mobile stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        mobile=[mobile stringByReplacingOccurrencesOfString:@" " withString:@""];

        [holdEachPersonMobileNumbers addObject:mobile];
    }

}

`

这里holdEachPersonMobileNumbers保存与您在通讯录中选择的特定人相关的所有号码

把上面的代码放在这个方法中 - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person

希望你遵守这个协议ABPeoplePickerNavigationControllerDelegate导入这个

    #import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

【讨论】:

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