如何从地址簿IOS 5 KABPersonPhoneProperty获取所有电话号码

时间:2012-06-30 20:02:00

标签: iphone objective-c ios5 xcode4.3

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

}

1 个答案:

答案 0 :(得分: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>