Fetching all contacts in ios Swift?

后端 未结 7 1409
天命终不由人
天命终不由人 2020-11-30 20:00

I am aware of the ios swift has a Contacts Framework where I can fetch contacts, but I cannot find any method to fetch all the contacts together where I can access each of t

7条回答
  •  爱一瞬间的悲伤
    2020-11-30 20:22

    A Swift 4.0 implementation to pull in all of the contacts.

    let contactStore = CNContactStore()
    var contacts = [CNContact]()
    let keys = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName)]
    let request = CNContactFetchRequest(keysToFetch: keys)
    
    do {
        try contactStore.enumerateContacts(with: request) { (contact, stop) in
            contacts.append(contact)
        }
    } catch {
        print(error.localizedDescription)
    }
    

    This creates a local property to store the contacts, which are then populated via the enumeration against contactStore.

提交回复
热议问题