Reputation:
What I want to do:
What I have tried:
ABPeoplePicker
but I am not able to get it to work with selecting multiple contacts.
Upvotes: 5
Views: 3585
Reputation: 4111
I was able to do what I think you are describing with the following code:
#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
This shows the entire addressbook in a tableView and then selecting puts a checkmark next to the contact and adds them to an NSSet. Deselecting removes the checkmark and removes the entry from the NSSet. I add the entire ABPerson to the NSSet so you will still have to use the C API to work with it later on.
Upvotes: 13
Reputation: 113747
You can return NO
for this delegate method:
- (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;
}
This allows the user to select a number of people.
Upvotes: 3