2

我正在使用 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;

}
4

1 回答 1

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>
于 2013-05-22T06:32:38.133 回答