ABPeoplePicker选择了许多并返回电子邮件数组

时间:2012-05-01 18:17:27

标签: iphone objective-c ios ipad

我想做什么:

  1. 向用户显示其设备上的所有联系人,其中包含联系人中的电子邮件地址。
  2. 允许用户在点击“完成”之前选择/取消选择任意数量的联系人。
  3. 返回一系列电子邮件地址...或一系列字典,其中包含所选联系人的所有联系信息。
  4. 我尝试了什么:

    ABPeoplePicker但我无法选择多个联系人。

2 个答案:

答案 0 :(得分:13)

我能够使用以下代码执行我认为您所描述的内容:

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

@interface ELEViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@end

@interface ELEViewController()
@property (nonatomic, strong) NSArray *arrayOfPeople;
@property (nonatomic, assign) CFArrayRef people;
@property (nonatomic, strong) NSMutableSet *selectedPeople;
@end

@implementation ELEViewController 
@synthesize arrayOfPeople = _arrayOfPeople;
@synthesize people = _people;
@synthesize selectedPeople = _selectedPeople;

- (NSMutableSet *) selectedPeople {
  if (_selectedPeople == nil) {
    _selectedPeople = [[NSMutableSet alloc] init];
  }
  return _selectedPeople;
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
}

- (void)viewDidLoad {
  [super viewDidLoad];

  UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(50, 50, 300, 300) 
                                                    style:UITableViewStylePlain];
  tableView.delegate = self;
  tableView.dataSource = self;
  [self.view addSubview:tableView];
  ABAddressBookRef addressBook = ABAddressBookCreate();
  self.people = ABAddressBookCopyArrayOfAllPeople(addressBook);  
  self.arrayOfPeople = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
  [tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  static NSString *CellIdentifier = @"ContactCell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                              reuseIdentifier:CellIdentifier];
  }

  int index = indexPath.row;
  ABRecordRef person = CFArrayGetValueAtIndex(self.people, index);
  NSString* firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person,
                                                             kABPersonFirstNameProperty);
  NSString* lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person,
                                                                  kABPersonLastNameProperty);
  NSString *name = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

  cell.textLabel.text = name;
  return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {
  return self.arrayOfPeople.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

  [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  id person = [self.arrayOfPeople objectAtIndex:indexPath.row];
  if (cell.accessoryType == UITableViewCellAccessoryNone) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [self.selectedPeople addObject:person];
  } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
    cell.accessoryType = UITableViewCellAccessoryNone;
    [self.selectedPeople removeObject:person];
  }
  NSLog(@"%@", self.selectedPeople);  
}

@end

这会在tableView中显示整个地址簿,然后选择在联系人旁边放置一个复选标记并将它们添加到NSSet。取消选中将删除复选标记并从NSSet中删除该条目。我将整个ABPerson添加到NSSet中,因此您以后仍然需要使用C API来处理它。

答案 1 :(得分:3)

您可以为此委托方法返回NO

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker 
shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,
                      kABPersonFirstNameProperty);
    NSLog(@"Name %@", name);
    // Do stuff with the person record
    return NO;
}

这允许用户选择一些人。