Created
August 7, 2020 01:09
-
-
Save SarahAlsharif/66ed7413e75487e4ae1fbbf6977baab7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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