Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort contacts using Contacts with Swift

I've read official apple documentation about sorting contacts, although I am not sure how to implement it. So, here is fetch request:

let fetchRequest = CNContactFetchRequest(keysToFetch: keysToFetch)

and my prefered sort order:

let sortOrder = CNContactSortOrder.UserDefault

and this is how I usually fetch contacts:

    do {
        try store.enumerateContactsWithFetchRequest(fetchRequest, usingBlock: { (let contact, let stop) -> Void in
                self.contacts.append(contact)
        })
    }
    catch let error as NSError {
        print(error.localizedDescription)
    }

Now what should I do with sortOrder? Where and should I include in my whole fetching process?

like image 636
Xernox Avatar asked Jan 18 '16 09:01

Xernox


People also ask

How do you sort your Contacts?

In addition, the Android app lets you organize your contacts more efficiently. Tap your avatar and go to Contacts app settings—under Display and Edit contacts, you'll be able to sort contacts by first name or last name, or to show or hide phonetic names.

How do you sort Contacts by name?

​Here's how to sort the contact list alphabetically by last name: Go to the main Contact list by selecting "Contacts" in the navigation bar. In the page menu on the left, select "Sort by" under the "Filters & Sorting" section. In the dialog that appears, select "Last name" and then choose either "A to Z" or "Z to A."

How do I sort my Contacts in Apple?

You can sort contacts in the contacts list by first or last name. In the Contacts app on your Mac, choose Contacts > Preferences, then click General. Click the Sort By pop-up menu to sort contacts in alphabetical order by first or last name.

How do I access my phone Contacts in Swift?

Swift 4 and 5. import ContactsUI func phoneNumberWithContryCode() -> [String] { let contacts = PhoneContacts. getContacts() // here calling the getContacts methods var arrPhoneNumbers = [String]() for contact in contacts { for ContctNumVar: CNLabeledValue in contact. phoneNumbers { if let fulMobNumVar = ContctNumVar.


2 Answers

If you are using SwiftyContacts, you can pass in the sort option in the fetchContacts(..) request, see below:

import SwiftyContacts

fetchContacts(ContactsSortorder: .givenName) { (result) in
    switch result {
    case .success(let contacts):
        print(contacts)
    case .failure(let error):
        print(error)
    }
}
like image 57
Zorayr Avatar answered Oct 14 '22 19:10

Zorayr


Updated For Swift 4.0

let fetchRequest = CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor, CNContactMiddleNameKey as CNKeyDescriptor, CNContactEmailAddressesKey as CNKeyDescriptor,CNContactPhoneNumbersKey as CNKeyDescriptor])

        fetchRequest.sortOrder = CNContactSortOrder.userDefault

        let store = CNContactStore()

        do {
            try store.enumerateContacts(with: fetchRequest, usingBlock: { (contact, stop) -> Void in
              //  print(contact.phoneNumbers.first?.value ?? "not found")

            })
        }
        catch let error as NSError {
            print(error.localizedDescription)
        }

Old Version write like this

 fetchRequest.sortOrder = CNContactSortOrder.UserDefault

after fetchRequest object created so your final output is like

let fetchRequest = CNContactFetchRequest(keysToFetch: keysToFetch)

fetchRequest.sortOrder = CNContactSortOrder.UserDefault

 do {
        try store.enumerateContactsWithFetchRequest(fetchRequest, usingBlock: { (let contact, let stop) -> Void in
                self.contacts.append(contact)
        })
    }
    catch let error as NSError {
        print(error.localizedDescription)
    }
like image 31
Jaydeep Patel Avatar answered Oct 14 '22 20:10

Jaydeep Patel