Commit 3f6e618b by Javier Heisekce

Agrega funcionalidad de busqueda de contactos

parent 6ae7dfa8
...@@ -27,7 +27,6 @@ class ContactsViewController: UIViewController { ...@@ -27,7 +27,6 @@ class ContactsViewController: UIViewController {
} }
func importContacts() { func importContacts() {
let keys = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey] as [Any] let keys = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey] as [Any]
let request = CNContactFetchRequest( keysToFetch: keys as! [CNKeyDescriptor]) let request = CNContactFetchRequest( keysToFetch: keys as! [CNKeyDescriptor])
request.sortOrder = CNContactSortOrder.givenName request.sortOrder = CNContactSortOrder.givenName
...@@ -152,7 +151,26 @@ extension ContactsViewController: UISearchBarDelegate, UISearchDisplayDelegate, ...@@ -152,7 +151,26 @@ extension ContactsViewController: UISearchBarDelegate, UISearchDisplayDelegate,
} }
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
importContacts()
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
print(searchText) var foundContacts = [ContactStruct]()
if searchText == "" {
importContacts()
contactsTable.reloadData()
} else {
for indexContact in contactStructArray.indices{
let contactFound = contactStructArray[indexContact].contacts.filter({$0.givenName.lowercased().contains(searchText.lowercased())})
if !contactFound.isEmpty {
foundContacts.append(ContactStruct(isExpanded: true, contacts: contactFound, letter: contactStructArray[indexContact].letter))
}
}
contactStructArray = foundContacts
contactsTable.reloadData()
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment