-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathEPContactsPicker.swift
358 lines (290 loc) · 14.6 KB
/
EPContactsPicker.swift
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//
// EPContactsPicker.swift
// EPContacts
//
// Created by Prabaharan Elangovan on 12/10/15.
// Copyright © 2015 Prabaharan Elangovan. All rights reserved.
//
import UIKit
import Contacts
@objc public protocol EPPickerDelegate {
optional func epContactPicker(_: EPContactsPicker, didContactFetchFailed error: NSError)
optional func epContactPicker(_: EPContactsPicker, didCancel error: NSError)
optional func epContactPicker(_: EPContactsPicker, didSelectContact contact: EPContact)
optional func epContactPicker(_: EPContactsPicker, didSelectMultipleContacts contacts: [EPContact])
}
typealias ContactsHandler = (contacts : [CNContact] , error : NSError?) -> Void
public enum SubtitleCellValue{
case PhoneNumber
case Email
case Birthday
case Organization
}
public class EPContactsPicker: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {
// MARK: - Properties
public var contactDelegate: EPPickerDelegate?
var contactsStore: CNContactStore?
var resultSearchController = UISearchController()
var orderedContacts = [String: [CNContact]]() //Contacts ordered in dicitonary alphabetically
var sortedContactKeys = [String]()
var selectedContacts = [EPContact]()
var filteredContacts = [CNContact]()
var subtitleCellValue = SubtitleCellValue.PhoneNumber
var multiSelectEnabled: Bool = false //Default is single selection contact
// MARK: - Lifecycle Methods
override public func viewDidLoad() {
super.viewDidLoad()
self.title = EPGlobalConstants.Strings.contactsTitle
registerContactCell()
inititlizeBarButtons()
initializeSearchBar()
reloadContacts()
}
func initializeSearchBar() {
self.resultSearchController = ( {
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.delegate = self
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
}
func inititlizeBarButtons() {
let cancelButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Cancel, target: self, action: #selector(onTouchCancelButton))
self.navigationItem.leftBarButtonItem = cancelButton
if multiSelectEnabled {
let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: #selector(onTouchDoneButton))
self.navigationItem.rightBarButtonItem = doneButton
}
}
private func registerContactCell() {
let podBundle = NSBundle(forClass: self.classForCoder)
if let bundleURL = podBundle.URLForResource(EPGlobalConstants.Strings.bundleIdentifier, withExtension: "bundle") {
if let bundle = NSBundle(URL: bundleURL) {
let cellNib = UINib(nibName: EPGlobalConstants.Strings.cellNibIdentifier, bundle: bundle)
tableView.registerNib(cellNib, forCellReuseIdentifier: "Cell")
}
else {
assertionFailure("Could not load bundle")
}
}
else {
let cellNib = UINib(nibName: EPGlobalConstants.Strings.cellNibIdentifier, bundle: nil)
tableView.registerNib(cellNib, forCellReuseIdentifier: "Cell")
}
}
override public func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Initializers
convenience public init(delegate: EPPickerDelegate?) {
self.init(delegate: delegate, multiSelection: false)
}
convenience public init(delegate: EPPickerDelegate?, multiSelection : Bool) {
self.init(style: .Plain)
self.multiSelectEnabled = multiSelection
contactDelegate = delegate
}
convenience public init(delegate: EPPickerDelegate?, multiSelection : Bool, subtitleCellType: SubtitleCellValue) {
self.init(style: .Plain)
self.multiSelectEnabled = multiSelection
contactDelegate = delegate
subtitleCellValue = subtitleCellType
}
// MARK: - Contact Operations
public func reloadContacts() {
getContacts( {(contacts, error) in
if (error == nil) {
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
})
}
func getContacts(completion: ContactsHandler) {
if contactsStore == nil {
//ContactStore is control for accessing the Contacts
contactsStore = CNContactStore()
}
let error = NSError(domain: "EPContactPickerErrorDomain", code: 1, userInfo: [NSLocalizedDescriptionKey: "No Contacts Access"])
switch CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts) {
case CNAuthorizationStatus.Denied, CNAuthorizationStatus.Restricted:
//User has denied the current app to access the contacts.
let productName = NSBundle.mainBundle().infoDictionary!["CFBundleName"]!
let alert = UIAlertController(title: "Unable to access contacts", message: "\(productName) does not have access to contacts. Kindly enable it in privacy settings ", preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { action in
self.contactDelegate?.epContactPicker!(self, didContactFetchFailed: error)
completion(contacts: [], error: error)
self.dismissViewControllerAnimated(true, completion: nil)
})
alert.addAction(okAction)
self.presentViewController(alert, animated: true, completion: nil)
case CNAuthorizationStatus.NotDetermined:
//This case means the user is prompted for the first time for allowing contacts
contactsStore?.requestAccessForEntityType(CNEntityType.Contacts, completionHandler: { (granted, error) -> Void in
//At this point an alert is provided to the user to provide access to contacts. This will get invoked if a user responds to the alert
if (!granted ){
dispatch_async(dispatch_get_main_queue(), { () -> Void in
completion(contacts: [], error: error!)
})
}
else{
self.getContacts(completion)
}
})
case CNAuthorizationStatus.Authorized:
//Authorization granted by user for this app.
var contactsArray = [CNContact]()
let contactFetchRequest = CNContactFetchRequest(keysToFetch: allowedContactKeys())
do {
try contactsStore?.enumerateContactsWithFetchRequest(contactFetchRequest, usingBlock: { (contact, stop) -> Void in
//Ordering contacts based on alphabets in firstname
contactsArray.append(contact)
var key: String = "#"
//If ordering has to be happening via family name change it here.
if let firstLetter = contact.givenName[0..<1] where firstLetter.containsAlphabets() {
key = firstLetter.uppercaseString
}
var contacts = [CNContact]()
if let segregatedContact = self.orderedContacts[key] {
contacts = segregatedContact
}
contacts.append(contact)
self.orderedContacts[key] = contacts
})
self.sortedContactKeys = Array(self.orderedContacts.keys).sort(<)
if self.sortedContactKeys.first == "#" {
self.sortedContactKeys.removeFirst()
self.sortedContactKeys.append("#")
}
completion(contacts: contactsArray, error: nil)
}
//Catching exception as enumerateContactsWithFetchRequest can throw errors
catch let error as NSError {
print(error.localizedDescription)
}
}
}
func allowedContactKeys() -> [CNKeyDescriptor]{
//We have to provide only the keys which we have to access. We should avoid unnecessary keys when fetching the contact. Reducing the keys means faster the access.
return [CNContactNamePrefixKey,
CNContactGivenNameKey,
CNContactFamilyNameKey,
CNContactOrganizationNameKey,
CNContactBirthdayKey,
CNContactImageDataKey,
CNContactThumbnailImageDataKey,
CNContactImageDataAvailableKey,
CNContactPhoneNumbersKey,
CNContactEmailAddressesKey,
]
}
// MARK: - Table View DataSource
override public func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if resultSearchController.active { return 1 }
return sortedContactKeys.count
}
override public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if resultSearchController.active { return filteredContacts.count }
if let contactsForSection = orderedContacts[sortedContactKeys[section]] {
return contactsForSection.count
}
return 0
}
// MARK: - Table View Delegates
override public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! EPContactCell
cell.accessoryType = UITableViewCellAccessoryType.None
//Convert CNContact to EPContact
var contact = EPContact()
if resultSearchController.active {
contact = EPContact(contact: filteredContacts[indexPath.row])
} else {
if let contactsForSection = orderedContacts[sortedContactKeys[indexPath.section]] {
contact = EPContact(contact: contactsForSection[indexPath.row])
}
}
if multiSelectEnabled && selectedContacts.contains({ $0.contactId == contact.contactId }) {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
cell.updateContactsinUI(contact, indexPath: indexPath, subtitleType: subtitleCellValue)
return cell
}
override public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! EPContactCell
let selectedContact = cell.contact!
if multiSelectEnabled {
//Keeps track of enable=ing and disabling contacts
if cell.accessoryType == UITableViewCellAccessoryType.Checkmark {
cell.accessoryType = UITableViewCellAccessoryType.None
selectedContacts = selectedContacts.filter(){
return selectedContact.contactId != $0.contactId
}
}
else {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
selectedContacts.append(selectedContact)
}
}
else {
//Single selection code
contactDelegate?.epContactPicker!(self, didSelectContact: selectedContact)
self.dismissViewControllerAnimated(true, completion: nil)
}
}
override public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 60.0
}
override public func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
if resultSearchController.active { return 0 }
tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: index), atScrollPosition: UITableViewScrollPosition.Top , animated: false)
return sortedContactKeys.indexOf(title)!
}
override public func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
if resultSearchController.active { return nil }
return sortedContactKeys
}
override public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if resultSearchController.active { return nil }
return sortedContactKeys[section]
}
// MARK: - Button Actions
func onTouchCancelButton() {
contactDelegate?.epContactPicker!(self, didCancel: NSError(domain: "EPContactPickerErrorDomain", code: 2, userInfo: [ NSLocalizedDescriptionKey: "User Canceled Selection"]))
dismissViewControllerAnimated(true, completion: nil)
}
func onTouchDoneButton() {
contactDelegate?.epContactPicker!(self, didSelectMultipleContacts: selectedContacts)
dismissViewControllerAnimated(true, completion: nil)
}
// MARK: - Search Actions
public func updateSearchResultsForSearchController(searchController: UISearchController)
{
if let searchText = resultSearchController.searchBar.text where searchController.active {
let predicate: NSPredicate
if searchText.characters.count > 0 {
predicate = CNContact.predicateForContactsMatchingName(searchText)
} else {
predicate = CNContact.predicateForContactsInContainerWithIdentifier(contactsStore!.defaultContainerIdentifier())
}
let store = CNContactStore()
do {
filteredContacts = try store.unifiedContactsMatchingPredicate(predicate,
keysToFetch: allowedContactKeys())
//print("\(filteredContacts.count) count")
self.tableView.reloadData()
}
catch {
print("Error!")
}
}
}
public func searchBarCancelButtonClicked(searchBar: UISearchBar) {
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
}