Browse files

Modify class

As added group list, relative code modified.
  • Loading branch information...
1 parent 1cf7c8b commit 7bcc7707b97426b92cb761b33331bcdbf7961e9c @qnibus committed Oct 10, 2012
View
21 README.md
@@ -7,6 +7,25 @@ iOS6 Update patch (Privacy settings & Retina 4inch)
====================
+Usage
+====================
+
+As shown below where you want to put your code
+<pre>
+TKPeoplePickerController *controller = [[[TKPeoplePickerController alloc] initPeoplePicker] autorelease];
+controller.actionDelegate = self;
+controller.modalPresentationStyle = UIModalPresentationFullScreen;
+[self presentViewController:controller animated:YES completion:nil];
+</pre>
+
+<pre>
+Delegate, using the contact information processing
+- (void)tkPeoplePickerController:(TKPeoplePickerController*)picker didFinishPickingDataWithInfo:(NSArray*)contacts;
+- (void)tkPeoplePickerControllerDidCancel:(TKPeoplePickerController*)picker;
+</pre>
+
+====================
+
License
====================
@@ -33,7 +52,7 @@ THE SOFTWARE.
====================
-About
+Our works
====================
<a href="http://hapsee.com" target="_blank">Than Happiness through the iLife!</a>
View
9 TKContactPicker/Controllers/TKContactsMultiPickerController.h
@@ -10,9 +10,10 @@
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import <malloc/malloc.h>
-#import "TKAddressBook.h"
+#import "TKContact.h"
+#import "TKGroup.h"
-@class TKAddressBook, TKContactsMultiPickerController;
+@class TKContact, TKContactsMultiPickerController;
@protocol TKContactsMultiPickerControllerDelegate <NSObject>
@required
- (void)tkContactsMultiPickerController:(TKContactsMultiPickerController*)picker didFinishPickingDataWithInfo:(NSArray*)contacts;
@@ -25,6 +26,7 @@
id _delegate;
@private
+ TKGroup *_group;
NSUInteger _selectedCount;
NSMutableArray *_listContent;
NSMutableArray *_filteredListContent;
@@ -33,10 +35,11 @@
@property (nonatomic, retain) id<TKContactsMultiPickerControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UITableView *tableView;
@property (nonatomic, retain) IBOutlet UISearchBar *searchBar;
-
+@property (nonatomic, retain) TKGroup *group;
@property (nonatomic, copy) NSString *savedSearchTerm;
@property (nonatomic) NSInteger savedScopeButtonIndex;
@property (nonatomic) BOOL searchWasActive;
+- (id)initWithGroup:(TKGroup*)group;
@end
View
118 TKContactPicker/Controllers/TKContactsMultiPickerController.m
@@ -32,19 +32,33 @@ @implementation TKContactsMultiPickerController
- (void)reloadAddressBook
{
// Create addressbook data model
- NSMutableArray *addressBookTemp = [NSMutableArray array];
+ NSMutableArray *contactsTemp = [NSMutableArray array];
ABAddressBookRef addressBooks = [(TKPeoplePickerController*)self.navigationController addressBook];
- CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);
- CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);
+ CFArrayRef allPeople;
+ CFIndex peopleCount;
+ if (_group) {
+ self.title = _group.name;
+ ABRecordRef groupRecord = ABAddressBookGetGroupWithRecordID(addressBooks, (ABRecordID)_group.recordID);
+ allPeople = ABGroupCopyArrayOfAllMembers(groupRecord);
+ peopleCount = (CFIndex)_group.membersCount;
+ } else {
+ self.title = NSLocalizedString(@"All Contacts", nil);
+ allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);
+ peopleCount = ABAddressBookGetPersonCount(addressBooks);
+ }
- for (NSInteger i = 0; i < nPeople; i++)
+ for (NSInteger i = 0; i < peopleCount; i++)
{
- TKAddressBook *addressBook = [[TKAddressBook alloc] init];
- ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
- CFStringRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
- CFStringRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
- CFStringRef abFullName = ABRecordCopyCompositeName(person);
+ ABRecordRef contactRecord = CFArrayGetValueAtIndex(allPeople, i);
+
+ // Thanks Steph-Fongo!
+ if (!contactRecord) continue;
+
+ CFStringRef abName = ABRecordCopyValue(contactRecord, kABPersonFirstNameProperty);
+ CFStringRef abLastName = ABRecordCopyValue(contactRecord, kABPersonLastNameProperty);
+ CFStringRef abFullName = ABRecordCopyCompositeName(contactRecord);
+ TKContact *contact = [[TKContact alloc] init];
/*
Save thumbnail image - performance decreasing
@@ -68,21 +82,24 @@ - (void)reloadAddressBook
[addressBook setThumbnail:personImage];
*/
- NSString *nameString = (NSString *)abName;
+ NSString *fullNameString;
+ NSString *firstString = (NSString *)abName;
NSString *lastNameString = (NSString *)abLastName;
if ((id)abFullName != nil) {
- nameString = (NSString *)abFullName;
+ fullNameString = (NSString *)abFullName;
} else {
if ((id)abLastName != nil)
{
- nameString = [NSString stringWithFormat:@"%@ %@", nameString, lastNameString];
+ fullNameString = [NSString stringWithFormat:@"%@ %@", firstString, lastNameString];
}
}
- addressBook.name = nameString;
- addressBook.recordID = (int)ABRecordGetRecordID(person);;
- addressBook.rowSelected = NO;
+ contact.name = fullNameString;
+ contact.recordID = (int)ABRecordGetRecordID(contactRecord);
+ contact.rowSelected = NO;
+ contact.lastName = (NSString*)abLastName;
+ contact.firstName = (NSString*)abName;
ABPropertyID multiProperties[] = {
kABPersonPhoneProperty,
@@ -91,7 +108,7 @@ - (void)reloadAddressBook
NSInteger multiPropertiesTotal = sizeof(multiProperties) / sizeof(ABPropertyID);
for (NSInteger j = 0; j < multiPropertiesTotal; j++) {
ABPropertyID property = multiProperties[j];
- ABMultiValueRef valuesRef = ABRecordCopyValue(person, property);
+ ABMultiValueRef valuesRef = ABRecordCopyValue(contactRecord, property);
NSInteger valuesCount = 0;
if (valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef);
@@ -104,11 +121,11 @@ - (void)reloadAddressBook
CFStringRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);
switch (j) {
case 0: {// Phone number
- addressBook.tel = [(NSString*)value telephoneWithReformat];
+ contact.tel = [(NSString*)value telephoneWithReformat];
break;
}
case 1: {// Email
- addressBook.email = (NSString*)value;
+ contact.email = (NSString*)value;
break;
}
}
@@ -117,23 +134,26 @@ - (void)reloadAddressBook
CFRelease(valuesRef);
}
- [addressBookTemp addObject:addressBook];
- [addressBook release];
+ [contactsTemp addObject:contact];
+ [contact release];
if (abName) CFRelease(abName);
if (abLastName) CFRelease(abLastName);
if (abFullName) CFRelease(abFullName);
}
- CFRelease(allPeople);
-// CFRelease(addressBooks);
+ if (allPeople) CFRelease(allPeople);
// Sort data
UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation];
- for (TKAddressBook *addressBook in addressBookTemp) {
- NSInteger sect = [theCollation sectionForObject:addressBook
- collationStringSelector:@selector(name)];
- addressBook.sectionNumber = sect;
+
+ // Thanks Steph-Fongo!
+ SEL sorter = ABPersonGetSortOrdering() == kABPersonSortByFirstName ? NSSelectorFromString(@"sorterFirstName") : NSSelectorFromString(@"sorterLastName");
+
+ for (TKContact *contact in contactsTemp) {
+ NSInteger sect = [theCollation sectionForObject:contact
+ collationStringSelector:sorter];
+ contact.sectionNumber = sect;
}
NSInteger highSection = [[theCollation sectionTitles] count];
@@ -143,12 +163,12 @@ - (void)reloadAddressBook
[sectionArrays addObject:sectionArray];
}
- for (TKAddressBook *addressBook in addressBookTemp) {
- [(NSMutableArray *)[sectionArrays objectAtIndex:addressBook.sectionNumber] addObject:addressBook];
+ for (TKContact *contact in contactsTemp) {
+ [(NSMutableArray *)[sectionArrays objectAtIndex:contact.sectionNumber] addObject:contact];
}
for (NSMutableArray *sectionArray in sectionArrays) {
- NSArray *sortedSection = [theCollation sortedArrayFromArray:sectionArray collationStringSelector:@selector(name)];
+ NSArray *sortedSection = [theCollation sortedArrayFromArray:sectionArray collationStringSelector:sorter];
[_listContent addObject:sortedSection];
}
[self.tableView reloadData];
@@ -157,9 +177,10 @@ - (void)reloadAddressBook
#pragma mark -
#pragma mark Initialization
-- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
+- (id)initWithGroup:(TKGroup*)group
{
- if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
+ if (self = [super initWithNibName:NSStringFromClass([self class]) bundle:nil]) {
+ self.group = group;
_selectedCount = 0;
_listContent = [NSMutableArray new];
_filteredListContent = [NSMutableArray new];
@@ -269,14 +290,14 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
- TKAddressBook *addressBook = nil;
+ TKContact *contact = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
- addressBook = (TKAddressBook *)[_filteredListContent objectAtIndex:indexPath.row];
+ contact = (TKContact *)[_filteredListContent objectAtIndex:indexPath.row];
else
- addressBook = (TKAddressBook *)[[_listContent objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
+ contact = (TKContact *)[[_listContent objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
- if ([[addressBook.name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0) {
- cell.textLabel.text = addressBook.name;
+ if ([[contact.name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0) {
+ cell.textLabel.text = contact.name;
} else {
cell.textLabel.font = [UIFont italicSystemFontOfSize:cell.textLabel.font.pointSize];
cell.textLabel.text = @"No Name";
@@ -287,7 +308,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
[button setBackgroundImage:[UIImage imageNamed:@"uncheckBox.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateSelected];
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
- [button setSelected:addressBook.rowSelected];
+ [button setSelected:contact.rowSelected];
cell.accessoryView = button;
@@ -308,15 +329,15 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
- TKAddressBook *addressBook = nil;
+ TKContact *contact = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
- addressBook = (TKAddressBook*)[_filteredListContent objectAtIndex:indexPath.row];
+ contact = (TKContact*)[_filteredListContent objectAtIndex:indexPath.row];
else
- addressBook = (TKAddressBook*)[[_listContent objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
+ contact = (TKContact*)[[_listContent objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
- BOOL checked = !addressBook.rowSelected;
- addressBook.rowSelected = checked;
+ BOOL checked = !contact.rowSelected;
+ contact.rowSelected = checked;
// Enabled rightButtonItem
if (checked) _selectedCount++;
@@ -356,10 +377,10 @@ - (IBAction)doneAction:(id)sender
{
NSMutableArray *objects = [NSMutableArray new];
for (NSArray *section in _listContent) {
- for (TKAddressBook *addressBook in section)
+ for (TKContact *contact in section)
{
- if (addressBook.rowSelected)
- [objects addObject:addressBook];
+ if (contact.rowSelected)
+ [objects addObject:contact];
}
}
@@ -404,12 +425,12 @@ - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[_filteredListContent removeAllObjects];
for (NSArray *section in _listContent) {
- for (TKAddressBook *addressBook in section)
+ for (TKContact *contact in section)
{
- NSComparisonResult result = [addressBook.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
+ NSComparisonResult result = [contact.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
- [_filteredListContent addObject:addressBook];
+ [_filteredListContent addObject:contact];
}
}
}
@@ -439,6 +460,7 @@ - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldRe
- (void)dealloc
{
+ [_group release];
[_filteredListContent release];
[_listContent release];
[_tableView release];
View
4 TKContactPicker/Controllers/TKPeoplePickerController.m
@@ -46,7 +46,7 @@ - (void)presentContactsMultiPickerController
{
_addressBook = ABAddressBookCreate();
- TKContactsMultiPickerController *contactMultiController = [[TKContactsMultiPickerController alloc] initWithNibName:NSStringFromClass([_contactController class]) bundle:nil];
+ TKContactsMultiPickerController *contactMultiController = [[TKContactsMultiPickerController alloc] initWithGroup:nil];
contactMultiController.delegate = self;
[self pushViewController:contactMultiController animated:NO];
self.contactController = contactMultiController;
@@ -94,7 +94,7 @@ - (id)initPeoplePicker
- (void)dealloc
{
- CFRelease(_addressBook);
+ if (_addressBook) CFRelease(_addressBook);
[_groupController release];
[_contactController release];
[super dealloc];
View
9 TKContactPicker/MainViewController.m
@@ -42,8 +42,7 @@ - (IBAction)showPeoplePicker:(id)sender
TKPeoplePickerController *controller = [[[TKPeoplePickerController alloc] initPeoplePicker] autorelease];
controller.actionDelegate = self;
controller.modalPresentationStyle = UIModalPresentationFullScreen;
- //[self presentViewController:controller animated:YES completion:nil];
- [self presentModalViewController:controller animated:YES];
+ [self presentViewController:controller animated:YES completion:nil];
}
#pragma mark - TKContactsMultiPickerControllerDelegate
@@ -63,8 +62,8 @@ - (void)tkPeoplePickerController:(TKPeoplePickerController*)picker didFinishPick
[contacts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- TKAddressBook *ab = (TKAddressBook*)obj;
- NSNumber *personID = [NSNumber numberWithInt:ab.recordID];
+ TKContact *contact = (TKContact*)obj;
+ NSNumber *personID = [NSNumber numberWithInt:contact.recordID];
ABRecordID abRecordID = (ABRecordID)[personID intValue];
ABRecordRef abPerson = ABAddressBookGetPersonWithRecordID(addressBook, abRecordID);
@@ -96,7 +95,7 @@ - (void)tkPeoplePickerController:(TKPeoplePickerController*)picker didFinishPick
[nameButton setAlpha:0.0f];
[nameButton.titleLabel setFont:[UIFont systemFontOfSize:11]];
[nameButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- [nameButton setTitle:ab.name forState:UIControlStateNormal];
+ [nameButton setTitle:contact.name forState:UIControlStateNormal];
[nameButton setTitleEdgeInsets:UIEdgeInsetsMake(45, 0, 0, 0)];
[self.scrollView addSubview:nameButton];

0 comments on commit 7bcc770

Please sign in to comment.