14

I just want to get contact given name and family name by phone number. I tried this but this is too much slow and cpu is hitting over %120.

let contactStore = CNContactStore()
            let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey]
            var contacts = [CNContact]()
            do {
                try contactStore.enumerateContactsWithFetchRequest(CNContactFetchRequest.init(keysToFetch: keys), usingBlock: { (contact, cursor) in
                    if (!contact.phoneNumbers.isEmpty) {
                        for phoneNumber in contact.phoneNumbers {
                            if let phoneNumberStruct = phoneNumber.value as? CNPhoneNumber {
                                do {
                                    let libPhone = try util.parseWithPhoneCarrierRegion(phoneNumberStruct.stringValue)
                                    let phoneToCompare = try util.getNationalSignificantNumber(libPhone)
                                    if formattedPhone == phoneToCompare {
                                        contacts.append(contact)
                                    }
                                }catch {
                                    print(error)
                                }
                            }

                        }
                    }
                })
                if contacts.count > 0 {
                    contactName = (contacts.first?.givenName)! + " " + (contacts.first?.familyName)!
                    print(contactName)
                    completionHandler(contactName)
                }
            }catch {
                print(error)
            }

Also When I Use phonenumber Kit for find contacts its increasing cpu and giving late response.

var result: [CNContact] = []
        let nationalNumber = PhoneNumberKit().parseMultiple([phoneNumber])
        let number = nationalNumber.first?.toNational()
        print(number)

        for contact in self.addressContacts {
            if (!contact.phoneNumbers.isEmpty) {

                let phoneNumberToCompareAgainst = number!.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
                for phoneNumber in contact.phoneNumbers {
                    if let phoneNumberStruct = phoneNumber.value as? CNPhoneNumber {
                        let phoneNumberString = phoneNumberStruct.stringValue
                        let nationalContactNumber = PhoneNumberKit().parseMultiple([phoneNumberString])
                        let nationalContactNumberString = nationalContactNumber.first?.toNational()
                        if nationalContactNumberString == number {
                            result.append(contact)
                        }
                    }
                }
            }
        }

        return result
Murat Kaya
  • 1,281
  • 3
  • 28
  • 52

3 Answers3

27

The problem with your implementation is that you access the address book in every search you are making.

If instead you will hold in-memory the address book content after the first access you will not reach this high CPU usage.

  1. First hold a lazy var in your controller that will hold the address book content:

    lazy var contacts: [CNContact] = {
        let contactStore = CNContactStore()
        let keysToFetch = [
            CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName),
            CNContactEmailAddressesKey,
            CNContactPhoneNumbersKey,
            CNContactImageDataAvailableKey,
            CNContactThumbnailImageDataKey]
    
        // Get all the containers
        var allContainers: [CNContainer] = []
        do {
            allContainers = try contactStore.containersMatchingPredicate(nil)
        } catch {
            print("Error fetching containers")
        }
    
        var results: [CNContact] = []
    
        // Iterate all containers and append their contacts to our results array
        for container in allContainers {
            let fetchPredicate = CNContact.predicateForContactsInContainerWithIdentifier(container.identifier)
    
            do {
                 let containerResults = try     contactStore.unifiedContactsMatchingPredicate(fetchPredicate, keysToFetch: keysToFetch)
                results.appendContentsOf(containerResults)
            } catch {
                print("Error fetching results for container")
            }
        }
    
        return results
    }()
    
    1. Iterate through the in-memory array when you are looking for a contact with a specific phone number:

    .

       func searchForContactUsingPhoneNumber(phoneNumber: String) -> [CNContact] {
        var result: [CNContact] = []
    
        for contact in self.contacts {
            if (!contact.phoneNumbers.isEmpty) {
                let phoneNumberToCompareAgainst = phoneNumber.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
                for phoneNumber in contact.phoneNumbers {
                    if let phoneNumberStruct = phoneNumber.value as? CNPhoneNumber {
                        let phoneNumberString = phoneNumberStruct.stringValue
                        let phoneNumberToCompare = phoneNumberString.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
                        if phoneNumberToCompare == phoneNumberToCompareAgainst {
                            result.append(contact)
                        }
                    }
                }
             } 
        }
    
        return result
    }
    

I tested it with a very big address book, it works smoothly.

Here is the entire view controller patched together for reference.

import UIKit
import Contacts

class ViewController: UIViewController {

    lazy var contacts: [CNContact] = {
        let contactStore = CNContactStore()
        let keysToFetch = [
                CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName),
                CNContactEmailAddressesKey,
                CNContactPhoneNumbersKey,
                CNContactImageDataAvailableKey,
                CNContactThumbnailImageDataKey]

        // Get all the containers
        var allContainers: [CNContainer] = []
        do {
            allContainers = try contactStore.containersMatchingPredicate(nil)
        } catch {
            print("Error fetching containers")
        }

        var results: [CNContact] = []

        // Iterate all containers and append their contacts to our results array
        for container in allContainers {
            let fetchPredicate = CNContact.predicateForContactsInContainerWithIdentifier(container.identifier)

            do {
                let containerResults = try contactStore.unifiedContactsMatchingPredicate(fetchPredicate, keysToFetch: keysToFetch)
                results.appendContentsOf(containerResults)
            } catch {
                print("Error fetching results for container")
            }
        }

        return results
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let contact = searchForContactUsingPhoneNumber("(555)564-8583")
        print(contact)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func searchForContactUsingPhoneNumber(phoneNumber: String) -> [CNContact] {

        var result: [CNContact] = []

        for contact in self.contacts {
            if (!contact.phoneNumbers.isEmpty) {
                let phoneNumberToCompareAgainst = phoneNumber.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
                for phoneNumber in contact.phoneNumbers {
                    if let phoneNumberStruct = phoneNumber.value as? CNPhoneNumber {
                        let phoneNumberString = phoneNumberStruct.stringValue
                        let phoneNumberToCompare = phoneNumberString.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
                        if phoneNumberToCompare == phoneNumberToCompareAgainst {
                            result.append(contact)
                        }
                    }
                }
            }
        }

        return result
    }
}

I used flohei's answer for the lazy var part.

steveSarsawa
  • 1,559
  • 2
  • 14
  • 31
Matan Lachmish
  • 1,235
  • 12
  • 17
  • 1
    yes That's fast but It's not getting all of numbers in my list.for example I need the fetch a contact but I have number with country code. So If contact is not saved by country code I can't fetch it. What should I do for this – Murat Kaya Aug 21 '16 at 17:18
  • 1
    Actually that is a different question from the one you originally asked. You can simply format the numbers the user input and the numbers from the address book to be on the same format, you can use an external library for that (like https://github.com/marmelroy/PhoneNumberKit). – Matan Lachmish Aug 21 '16 at 18:13
  • Another option is to remove the country code - if it exist. Then just compare the plane number. – Matan Lachmish Aug 21 '16 at 18:14
  • I used phonenumberKit and its increasing cpu and giving late response.I edited question – Murat Kaya Aug 21 '16 at 18:47
  • 8
    Not cool man, you shouldn't edit a question after you get an answer! – Matan Lachmish Aug 21 '16 at 18:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121483/discussion-between-murat-kaya-and-matan-lachmish). – Murat Kaya Aug 21 '16 at 18:52
  • 2
    @MuratKaya I've been following this topic of yours, and you have got a good straight answer from MatanLachmish. And you have now edited and changed original question. This is wrong. – MCMatan Aug 24 '16 at 10:16
  • @MuratKaya I've got the same issue while using PhoneNumberKit, did you find a solution for this? – Oluwatobi Omotayo Oct 09 '18 at 07:36
7

SWIFT 4 UPDATE

1) Add to .plist

<key>NSContactsUsageDescription</key>
<string>Our application needs to your contacts</string>

2) Request Authorization if you don't have it

func requestAccess() {

    let store = CNContactStore()
    store.requestAccess(for: .contacts) { granted, error in
        guard granted else {
            DispatchQueue.main.async {
               self.presentSettingsActionSheet()
            }
            return
        }
    }
}

func presentSettingsActionSheet() {
    let alert = UIAlertController(title: "Permission to Contacts", message: "This app needs access to contacts in order to ...", preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Go to Settings", style: .default) { _ in
        let url = URL(string: UIApplicationOpenSettingsURLString)!
        UIApplication.shared.open(url)
    })
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
    present(alert, animated: true)
}

2) Check Authorization Status if you ask for it before

    if CNContactStore.authorizationStatus(for: .contacts) == .authorized {
        getContacts()

    }

3) Call Get Contacts

    var contacts = [CNContact]()

    func getContacts(){

    let contactStore = CNContactStore()
    let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactImageDataAvailableKey, CNContactThumbnailImageDataKey]
    let request = CNContactFetchRequest(keysToFetch: keys as [CNKeyDescriptor])
    request.sortOrder = CNContactSortOrder.givenName

    do {
        try contactStore.enumerateContacts(with: request) {
            (contact, stop) in
            self.contacts.append(contact)
        }
    }
    catch {
        print("unable to fetch contacts")
    }
}

4) THIS IS THE FUNCTION TO GET THE CONTACT NAME OR BY NUMBER

    func getNameFromContacts(number: String) -> String {
    var contactFetched : CNContact
    var contactName = ""
    if contacts.count > 0 {

        let numberToBeCompared = number.components(separatedBy:CharacterSet.decimalDigits.inverted).joined(separator: "")
        for c in contacts {
            for n in c.phoneNumbers {
                if let numberRetrived = n.value as? CNPhoneNumber {
                     let numberRetrivedFixed = numberRetrived.stringValue.components(separatedBy:CharacterSet.decimalDigits.inverted).joined(separator: "")
                    if numberRetrivedFixed.elementsEqual(numberToBeCompared){
                        contactName = c.givenName
                        // OR get the contact --> c
                 contactFetched = c

                    }
                }
            }
        }

        return contactName

    } else {
        return ""
    }
}
user3826696
  • 1,045
  • 12
  • 13
3

Had to make these changes for Swift 5:


lazy var contacts = {
            let contactStore = CNContactStore()
            let keysToFetch: [CNKeyDescriptor] = [
                CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
                CNContactEmailAddressesKey as CNKeyDescriptor,
                CNContactPhoneNumbersKey as CNKeyDescriptor,
                CNContactImageDataAvailableKey as CNKeyDescriptor,
                CNContactThumbnailImageDataKey as CNKeyDescriptor]

            // Get all the containers
            var allContainers: [CNContainer] = []
            do {
                allContainers = try contactStore.containers(matching: nil)
            } catch {
                print("Error fetching containers")
            }

            var results: [CNContact] = []

            // 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     contactStore.unifiedContacts(matching: fetchPredicate, keysToFetch: keysToFetch)
                    results.append(contentsOf: containerResults)
                } catch {
                    print("Error fetching results for container")
                }
            }

            return results
        }()

And for the iteration and finding:

    func searchForContactUsingPhoneNumber(phoneNumber: String) -> [CNContact] {
         var result: [CNContact] = []

         for contact in contacts {
             if (!contact.phoneNumbers.isEmpty) {
                let phoneNumberToCompareAgainst = phoneNumber.components(separatedBy: NSCharacterSet.decimalDigits.inverted).joined(separator: "")
                 for phoneNumber in contact.phoneNumbers {
                     if let phoneNumberStruct = phoneNumber.value as? CNPhoneNumber {
                         let phoneNumberString = phoneNumberStruct.stringValue
                        let phoneNumberToCompare = phoneNumberString.components(separatedBy: NSCharacterSet.decimalDigits.inverted).joined(separator: "")
                         if phoneNumberToCompare == phoneNumberToCompareAgainst {
                             result.append(contact)
                         }
                     }
                 }
              }
         }

         return result
    }

Kody_06
  • 163
  • 2
  • 9