iOS Contacts How to Fetch contact by phone Number

前端 未结 2 487
眼角桃花
眼角桃花 2021-02-02 01:49

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 = CN         


        
2条回答
  •  佛祖请我去吃肉
    2021-02-02 02:29

    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.

提交回复
热议问题