Search code examples
swiftpredicatecncontactcncontactstore

Swift Using Contacts Framework, search using Identifier to match


Trying unsuccessfully to get a contact match providing contact Identifier. I desire to return contact, then to use the image associated with it. I am getting a nil match. thank you. This code I got from a demo, I'm kinda new to programming

import Contacts

var contact = CNContact()  
var contactStore = CNContactStore()

let foundContact = getContactFromID("94AAD3B1-E9E1-48C9-A796-F7EC1014230A")

func getContactFromID(contactID: String) -> CNContact {

    AppDelegate.getAppDelegate().requestForAccess { (accessGranted) -> Void in
        if accessGranted {

            let predicate = CNContact.predicateForContactsWithIdentifiers([contactID])

            let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactImageDataAvailableKey]

            var contacts = [CNContact]()
            var message: String!

            let contactsStore = AppDelegate.getAppDelegate().contactStore

            do {
                contacts = try contactsStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keys)

                if contacts.count == 0 {
                    message = "No contacts were found matching the given name."
                }
            }
            catch {
                message = "Unable to fetch contacts."
            }


            if message != nil {
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    Utility.showAlert(nil, message: message)
                })
            } else {

                dispatch_async(dispatch_get_main_queue(), { () -> Void in

                    self.contact = contacts[0]

                    print("self.contact: \(self.contact)")

                })
            }
        }
    }

    return self.contact
}

Solution

  • I solved it :), I removed the dispatch_async stuff, works now: here is fixed code.

    func getContactFromID(contactID: String) -> CNContact {
    
            AppDelegate.getAppDelegate().requestForAccess { (accessGranted) -> Void in
                if accessGranted {
    
                    let predicate = CNContact.predicateForContactsWithIdentifiers([contactID])
    
                    let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactImageDataAvailableKey]
    
                    var contacts = [CNContact]()
                    var message: String!
    
                    let contactsStore = AppDelegate.getAppDelegate().contactStore
    
                    do {
                        contacts = try contactsStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keys)
    
                        if contacts.count == 0 {
                            message = "No contacts were found matching the given name."
                        }
                    }
                    catch {
                        message = "Unable to fetch contacts."
                    }
    
                     self.contact = contacts[0]
                }
            }
            return self.contact
        }