Created
May 16, 2017 23:16
-
-
Save dleonard00/3343dbb3689ed967b2ae2545c9d38bbe 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
lazy var contacts: [CNContact] = { | |
let contactStore = CNContactStore() | |
contactStore.requestAccess(for: .contacts, completionHandler: { (granted, error) in | |
guard granted else { | |
let alert = UIAlertController(title: "Can't access contact", message: "Please go to Settings to enable contact permissions.", preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) | |
//TODO: link directly to settings app here. | |
self.present(alert, animated: true, completion: nil) | |
return | |
} | |
}) | |
// Get all the containers | |
var allContainers: [CNContainer] = [] | |
do { | |
allContainers = try contactStore.containers(matching: nil) | |
} catch { | |
print("Error fetching containers") | |
} | |
var results: [CNContact] = [] | |
// Iterate all containers and append their contacts to our results array | |
for container in allContainers { | |
let fetchPredicate = CNContact.predicateForContactsInContainer(withIdentifier: container.identifier) | |
guard let keys = self.keysToFetch else {return []} | |
do { | |
let containerResults = try contactStore.unifiedContacts(matching: fetchPredicate, keysToFetch: keys) | |
results.append(contentsOf: containerResults) | |
} catch { | |
print("Error fetching results for container") | |
} | |
} | |
return results | |
}() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment