Skip to content

Instantly share code, notes, and snippets.

@SarahAlsharif
Created August 7, 2020 01:09
Show Gist options
  • Save SarahAlsharif/66ed7413e75487e4ae1fbbf6977baab7 to your computer and use it in GitHub Desktop.
Save SarahAlsharif/66ed7413e75487e4ae1fbbf6977baab7 to your computer and use it in GitHub Desktop.
import Contacts
class FetchContacts {
func fetchingContacts() -> [ContactInfo]{
var contacts = [ContactInfo]()
let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey]
let request = CNContactFetchRequest(keysToFetch: keys as [CNKeyDescriptor])
do {
try CNContactStore().enumerateContacts(with: request, usingBlock: { (contact, stopPointer) in
contacts.append(ContactInfo(firstName: contact.givenName, lastName: contact.familyName, phoneNumber: contact.phoneNumbers.first?.value))
})
} catch let error {
print("Failed", error)
}
contacts = contacts.sorted {
$0.firstName < $1.firstName
}
return contacts
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment