The function retrieveContactsWithStore
must be outside of the @IBAction
@IBAction func btnContactsTapped()
{
let store = CNContactStore()
if CNContactStore.authorizationStatus(for: .contacts) == .notDetermined {
store.requestAccess(for: .contacts, completionHandler: { (authorized, error) in
if authorized {
self.retrieveContactsWithStore(store: store)
}
})
} else if CNContactStore.authorizationStatus(for: .contacts) == .authorized {
self.retrieveContactsWithStore(store: store)
}
}
func retrieveContactsWithStore(store: CNContactStore)
{
do {
let groups = try store.groups(matching: nil)
let predicate = CNContact.predicateForContactsInGroup(withIdentifier: groups[0].identifier)
//let predicate = CNContact.predicateForContactsMatchingName("John")
let keysToFetch = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactEmailAddressesKey] as [Any]
let contacts = try store.unifiedContacts(matching: predicate, keysToFetch: keysToFetch as! [CNKeyDescriptor])
// self.objects = contacts
DispatchQueue.main.async {
print("Contacts: \(contacts)")
}
} catch {
print(error)
}
}
2
solved Value of type ” has no member ” in swift