Contact CNcontact and Digits Find Friends Swift 3

I am trying to create an iPhone application with numbers. Find a friend function

I can get a matching list digitUserIDfrom numbers.

Now I try to match UserIDand CNContacts.

Please provide any examples to handle this.

As an update:

do 
{
    try contactStore.enumerateContactsWithFetchRequest(CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactMiddleNameKey, CNContactEmailAddressesKey,CNContactPhoneNumbersKey])) {
        (contact, cursor) -> Void in

        self.results.append(contact)
    }
}
catch{
    print("Handle the error please")
}

The above I managed to get all the contacts, but I don’t know how to pass the phone number filter into this and get the exact match of the contacts with CNContact

+4
source share
1 answer

, predicate CNContactFetchRequest , ( ; argh) , CNContact (, CNContact predicateForContacts(matchingName:) predicateForContacts(withIdentifiers:). NSPredicate.

, , , .

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

do {
    try contactStore.enumerateContacts(with: request) { contact, stop in
        for phone in contact.phoneNumbers {
            // look at `phone.value.stringValue`, e.g.

            let phoneNumberDigits = String(phone.value.stringValue.characters.filter { String($0).rangeOfCharacter(from: CharacterSet.decimalDigits) != nil })

            if phoneNumberDigits == "8885551212" {
                self.results.append(contact)
                return
            }
        }
    }
} catch let enumerateError {
    print(enumerateError.localizedDescription)
}

"digit UserID", , ( Digits?).

+8

Source: https://habr.com/ru/post/1651479/


All Articles