So I only recently uncovered the Contacts Framework through this video. As such I’m not yet accustomed to the API. Basically my main problem is that I can’t seem to find a way to access the name of the group a CNContact is in. The only support I can find is in Apple’s own documentation, which isn’t very helpful.
if someone could point me in the right direction towards how to fetch the group name, I would be very grateful.
My code is below,
Cheers
// ModelData.swift
// B-Day
import Foundation
import Contacts
import SwiftUI
struct Contact: Identifiable {
let id = UUID()
let category: String
let firstName: String
let lastName: String
let birthday: DateComponents?
}
func fetchAllContacts() async -> [Contact] {
var contacts = [Contact]()
let store = CNContactStore()
let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactBirthdayKey, CNContactIdentifierKey, CNGroupNameKey] as [CNKeyDescriptor]
let fetchRequest = CNContactFetchRequest (keysToFetch: keys)
do {
try store.enumerateContacts(with: fetchRequest, usingBlock: { contact, result in
//this should fetch the name of the contact's group
contacts.append(Contact(category: contact.groupName, firstName: contact.givenName, lastName: contact.familyName, birthday: contact.birthday))
})
}
catch {
print("Error")
}
return contacts
}