2

I am trying to discover contacts for a user with the code below (the code is in an implementation of a UITableViewController. I put breakpoints in both code blocks, and I determined that the the userIdentityDiscoveredBlock is not called while the completionBlock is called. This indicates that the operation is being run as expected, it just isn't finding any contacts.

I am running on the simulator, but I verified that the simulator has synced all my iCloud contacts (opening the Contacts app on the simulator shows all my contacts).

override func viewDidLoad() {
    super.viewDidLoad()

    let op = CKDiscoverAllUserIdentitiesOperation()

    op.discoverAllUserIdentitiesCompletionBlock = { error -> Void in
        // reload my data table
    }

    op.userIdentityDiscoveredBlock = { user -> Void in
        if user.hasiCloudAccount {
            self.iCloudUsers.append(user)
        } else {
            self.nonICloudUsers.append(user)
        }
    }

    CKContainer.default().add(op)
}

So my question is this - Is there something else that has to be done in order to discover contacts? Is this a simulator issue?

I searched the documentation and other questions but I can't seem to find information on this given the operation is new to iOS 10.

Alex
  • 676
  • 9
  • 20

1 Answers1

3

There's quite a bit that must be done before CKDiscoverAllUserIdentitiesOperation will return any results.

First, each user of your app must grant permission to be looked up by email. Your app makes this request using CKContainer requestApplicationPermission.

Each user of your app must also be logged into an iCloud account. iCloud Drive must also be enabled by the user.

And lastly, for CKDiscoverAllUserIdentitiesOperation to return any users, the person must have contacts with email addresses that match other users that completed all of the previous steps.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Bummer, but thanks! I had hoped it would be easier than [what was deprecated](https://developer.apple.com/reference/cloudkit/ckdiscoverallcontactsoperation), but I guess not. – Alex Oct 30 '16 at 20:02
  • Why is it a bummer? You just need to add a couple of lines of code to your app and you're done. – rmaddy Oct 30 '16 at 20:04
  • You're right-- it's not a big deal, I was hoping for it to be easier for a user to be able to see all their contacts instead of just the ones that have opted in. Makes sense though. – Alex Oct 30 '16 at 21:52
  • The whole point of using `CKDiscoverAllUserIdentitiesOperation` is to find other users of your app that the user happens to have in their contacts. If you simply want to access the user's contacts, you don't use `CKDiscoverAllUserIdentitiesOperation`. You use the Contacts framework. – rmaddy Oct 30 '16 at 22:21
  • Ah, thanks for the clarification. I've been so entrenched in CloudKit these last few weeks that I forgot there was a whole world out there :) – Alex Oct 30 '16 at 22:34
  • @rmaddy I keep getting Internal Server Error while trying to use CKDiscoverAllUserIdentitiesOperation. Is there any way to find what is causing it or a way to resolve it ? – user1046037 Sep 23 '17 at 14:12
  • @user1046037 `CKDiscoverAllUserIdentitiesOperation` doesn't work under iOS 11. I reported it too Apple in early August and they still haven't fixed the problem. Please submit your own bug report demonstrating the problem. The more people that report the issue, the better chance it will be fixed. – rmaddy Sep 23 '17 at 15:33
  • Thanks I earlier submitted one for `CKDiscoverUserIdentitiesOperation` which uses lookupInfo. I will submit one for discover all operation. Really hope it gets fixed – user1046037 Sep 23 '17 at 15:57
  • @rmaddy You were absolutely spot on, thanks a ton !! It works fine on 10.3 and bug is with iOS 11. I spent 5 days breaking my head over it. Thanks once again. Would you like to answer https://stackoverflow.com/questions/45703354/ckcontainer-discoverallidentities-always-fails. It has a bounty on it – user1046037 Sep 24 '17 at 03:14