0

I have an iOS app that accesses the default container of the contact store and retrieves the groups within that container. After I signed out of my iPhone 8 device that the app is installed in, and then signed in with another account. Then signed out and signed in with my original account, the app was no longer able to retrieve the groups from the default container.

Why is it doing this and how do I fix this?

Here is the relevant code:

cnGroups = try contactStore.groups(matching: CNGroup.predicateForGroupsInContainer(withIdentifier: contactStore.defaultContainerIdentifier()))

print("cnGroups...")
print(cnGroups)

Here is the debug window output:

cnGroups...
[]

I initialized the contact store globally in a swift file outside of any class:

internal var contactStore = CNContactStore()

This code to request access to Contacts from the user is in the viewDidLoad() callback method. The initUTIGroups() method is where I try to retrieve the groups from the default container.

contactStore.requestAccess(for: .contacts) {

    permissionGranted, error in

    if error != nil {

        print(error?.localizedDescription as Any)

    }

    if permissionGranted {

        self.initUTIGroups()

    } else {

        let alertMessage = "\(displayName) will not work unless you allow access to Contacts. Please go to Settings and allow access to Contacts then restart \(displayName)"

        let alert = UIAlertController(title: nil, message: alertMessage, preferredStyle: .alert)

        let actionOK = UIAlertAction(title: "OK", style: .cancel, handler: nil)

        alert.addAction(actionOK)

        self.present(alert, animated: true, completion: nil)

    }

}

I retrieved all the containers in the contact store and I compared them with the default container. It looks like the current default container is not the same container as the default container before I signed out of iCloud the first time. I found the container that contains the groups, which are the same groups that I retrieved from the default container before I signed out of iCloud the first time.

Is there code I can write that would prevent the problem or fix the problem once it occurs?

daniel
  • 1,446
  • 3
  • 29
  • 65
  • where are you initializing the `contactStore` – gadu Aug 28 '19 at 01:14
  • I'll put that code in my post. I didn't think it was relevant when I first posted my question. – daniel Aug 28 '19 at 01:15
  • @gadu Ok. I have put the relevant code in the post. – daniel Aug 28 '19 at 01:21
  • 1
    yeah something is going on outside the context of this code - do you really need it to be a global variable just declare it locally before you use it: `let contactStore = CNContactStore()` then keep going? I worry about some thread safety issues otherwise – gadu Aug 28 '19 at 01:39
  • @gadu Ok. I will try that. – daniel Aug 28 '19 at 01:40
  • @gadu I added comments above saying that the current default container is not the same as the default container before I signed out of iCloud the first time. – daniel Aug 28 '19 at 01:42
  • 1
    yeah so just try what I said, if I had to guess its likely the defaultContainerIdentifier is some lazy property thats dynamically generated (all global properties are lazy) - so when you log in and out the defaultContainerIdentifier changes, but because you're keeping a reference as a global property it won't update – gadu Aug 28 '19 at 01:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/198561/discussion-between-daniel-brower-and-gadu). – daniel Aug 28 '19 at 01:47

0 Answers0