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

标签 iphone objective-c ios ipad

我想做什么:

  1. 向用户展示其设备上所有联系人中包含电子邮件地址的联系人。
  2. 允许用户在点击“完成”之前选择/取消选择任意数量的联系人。
  3. 返回电子邮件地址数组...或包含所选联系人所有联系信息的字典数组。

我尝试过的:

ABPeoplePicker 但我无法使用它来选择多个联系人。

最佳答案

我能够使用以下代码完成您所描述的操作:

#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 来处理它。

关于iphone - ABPeoplePicker 选择许多并返回电子邮件数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10402191/

相关文章:

ios - 自动布局视觉格式语言

ios - 在 viewController 第一次出现时运行一​​段代码

ios - 当我同时使用 Objective-C 和 Swift 框架时,如何改善应用程序启动时间(主程序前)时间

iphone - 创建类似用户位置的动画 蓝色弹珠掉落

iphone - 为文本字段显示 uipicker 而不是键盘

ios - Tableview 和 MBProgressHUD 不加载对象

iphone - 在 iPhone 应用程序中弹出 View 后发出 SIGABRT 信号

iphone - 在主uiview的 subview 上获取点击事件

objective-c - theos mobilesubstrate 调整中的 id 对象,我无法禁用此 -(id)

iOS 8 AVFoundation - 如何在支持的设备上启用视频稳定