Click here to Skip to main content
15,881,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I use the Contacts Framework with Swift for iOS 15 using Xcode 13, when I use CNContactStore instance method unifiedContacts(matching:keysToFetch:), what happens to those CNContact objects that are not linked with any other CNContact objects, would they be returned as non-unified contacts, or would they be left out since they are non-unified contacts?

The sample iOS Swift project ManagingContacts, uses both enumerateContacts(with:usingBlock:) and unifiedContacts(withIdentifier:keysToFetch:). Understanding why they choose to use each when they do may help answer the question.

The name "unifiedContacts" suggests it only fetches unified contacts, and does not fetch non-unified contacts that otherwise would meet the specified criteria, but it doesn't make sense to me why there would ever be a need for it or why it is used in the said project when it is used.

What I have tried:

From my test code, it looks like they both do the same thing. What do you think about this?

I tried this code in a test project:

<pre lang="Swift">
class ViewController: UIViewController {
    let store = CNContactStore()
    
    let keys = [CNContactGivenNameKey as CNKeyDescriptor]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        store.requestAccess(for: .contacts) {
            
            permissionGranted, error in
            
            let request = CNContactFetchRequest(keysToFetch: self.keys)
            
            print("enumerate contacts")
            
            do {
                
                try self.store.enumerateContacts(with: request) {
                    
                    contact, pointer in
                    
                    print(contact.givenName)
                    
                }
            
            } catch {
                
                print("error")
                print(error.localizedDescription)
                
            }
            
            print("unified contacts")
            
            do {
                
                let containers = try self.store.containers(matching: nil)
            
                if containers.count > 1 {
                    fatalError("More than one container!")
                }
                
                let predicate = CNContact.predicateForContactsInContainer(withIdentifier: self.store.defaultContainerIdentifier())
                
                let contacts = try self.store.unifiedContacts(matching: predicate, keysToFetch: self.keys)
                for contact in contacts {
                    
                    print(contact.givenName)
                    
                }
                
            } catch {
                
                print("error")
                print(error.localizedDescription)
            }
                        
        }
        
    }
}


Here's the result in the debug window:
enumerate contacts
Kate
Daniel
John
Anna
Hank
David
unified contacts
Kate
Daniel
John
Anna
Hank
David
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900