How to fetch all contacts record in iOS 9 using Contacts Framework

前端 未结 20 1586
[愿得一人]
[愿得一人] 2020-11-28 01:52

Most part of AddressBook framework is deprecated in iOS 9. In the new Contacts Framework documentation only shows how to fetch records matches a NSPredicate, bu

20条回答
  •  無奈伤痛
    2020-11-28 02:12

    In swift 3 and Xcode 8 you can get all contacts list

    let keys = [CNContactGivenNameKey ,CNContactImageDataKey,CNContactPhoneNumbersKey]
            var message: String!
            //let request=CNContactFetchRequest(keysToFetch: keys)
            let contactsStore = AppDelegate.AppDel.contactStore
            // Get all the containers
            var allContainers: [CNContainer] = []
            do {
                allContainers = try contactsStore.containers(matching: nil)
            } catch {
                print("Error fetching containers")
            }
    
    
    
            // Iterate all containers and append their contacts to our results array
            for container in allContainers {
                let fetchPredicate = CNContact.predicateForContactsInContainer(withIdentifier: container.identifier)
    
                do {
                    let containerResults = try contactsStore.unifiedContacts(matching: fetchPredicate, keysToFetch: keys as [CNKeyDescriptor])
                    self.results.append(contentsOf: containerResults)
                    self.tableView.reloadData()
                    message="\(self.results.count)"
                } catch {
                    print("Error fetching results for container")
                }
            }
    

提交回复
热议问题