/
AutoFillTableViewController.m
186 lines (147 loc) · 6.71 KB
/
AutoFillTableViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// AutoFillTableViewController.m
// Invoice
//
// Created by Giuliano Giacaglia on 4/15/14.
// Copyright (c) 2014 Giuliano Giacaglia. All rights reserved.
//
#import "AutoFillTableViewController.h"
@implementation AutoFillTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
[self retrieveData];
self.toTextField.delegate = self;
self.staffTableArray = [[NSMutableArray alloc] initWithCapacity:1];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.scrollEnabled = YES;
self.tableView.hidden = YES;
[self.tableView setBackgroundColor:[UIColor whiteColor]];
}
return self;
}
- (void)createTableViewWithYPosition:(NSNumber *)yPosition {
self.tableView = [[UITableView alloc] initWithFrame:
CGRectMake(0, [yPosition intValue], 320, 200) style:UITableViewStylePlain];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.toTextField) {
self.tableView.hidden = NO;
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
}
return YES;
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
{
substring = [substring lowercaseString];
NSMutableArray *autoCompleteArray = [[NSMutableArray alloc] init];
NSMutableArray *autoCompleteNameArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [self.emailArray count]; i++) {
NSString *curString = [self.emailArray objectAtIndex:i];
NSString *lowerCaseCur = [curString lowercaseString];
NSRange substringRange = [lowerCaseCur rangeOfString:substring];
if (substringRange.location == 0)
{
[autoCompleteArray addObject:curString];
[autoCompleteNameArray addObject: [self.nameArray objectAtIndex:i]];
}
}
if (![substring isEqualToString:@""])
{
self.staffTableArray = [NSMutableArray arrayWithArray:autoCompleteArray];
self.staffTableNameArray = [NSMutableArray arrayWithArray:autoCompleteNameArray];
[self.tableView reloadData];
}
}
- (void) retrieveData {
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // We are on iOS 6
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(semaphore);
});
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
else { // We are on iOS 5 or Older
accessGranted = YES;
[self getContactsWithAddressBook:addressBook];
}
if (accessGranted) {
[self getContactsWithAddressBook:addressBook];
}
}
- (void)getContactsWithAddressBook:(ABAddressBookRef )addressBook {
NSMutableArray *contactList = [[NSMutableArray alloc] init];
self.emailArray = [[NSMutableArray alloc] init];
self.nameArray = [[NSMutableArray alloc] init];
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for (int i=0;i < nPeople;i++) {
NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
//For username and surname
ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));
CFStringRef firstName, lastName;
firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
[dOfPerson setObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"name"];
//For Email ids
ABMutableMultiValueRef eMail = ABRecordCopyValue(ref, kABPersonEmailProperty);
if(ABMultiValueGetCount(eMail) > 0) {
[self.emailArray addObject:(__bridge NSString *)ABMultiValueCopyValueAtIndex(eMail, 0)];
[dOfPerson setObject:(__bridge NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"];
[self.nameArray addObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName]];
}
//For Phone number
NSString* mobileLabel;
for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
{
[dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
}
else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
{
[dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
break ;
}
}
[contactList addObject:dOfPerson];
}
}
#pragma mark UITableViewDataSource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
// return 3;
return self.staffTableArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:AutoCompleteRowIdentifier];
}
cell.detailTextLabel.text = [self.staffTableArray objectAtIndex:indexPath.row];
cell.textLabel.text = [self.staffTableNameArray objectAtIndex:indexPath.row];
return cell;
}
#pragma mark UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
self.toTextField.text = selectedCell.detailTextLabel.text;
[self.toTextField resignFirstResponder];
self.tableView.hidden = YES;
}
@end