1

in Appdelegate I have userDidAcceptCloudKitShareWith
but it gets only fired for the first device from the user who has accepted the invitation.

Example:

  • User A invites User B.
  • User B accepts the invitation on device B1 -> everything is ok, the method gets fired and I know what to do.
  • The problem is, when user B uses his device B2, the method does not get fired. How am I supposed to know on device B2, that user B has already accepted an invitation?

I tried this with real devices and get only on first device the method fired.

I could check whether he has a sharedZone with the expected zoneID, however this sounds a little strange to me - any help is more than appreciated!

Red
  • 1,425
  • 1
  • 10
  • 15
  • You could check the status of the share for , if it is already accepted than it won't be accepted again. – user3069232 Sep 25 '18 at 05:35
  • Thanks for your comment. i think your idea is similar to checking whether there is a sharedZone with the correct id. I guess the question is still, how do I know on device B2 that I need/can check the CKShare without checking every time. – Red Sep 25 '18 at 06:53
  • Did you implement didRegisterForRemoteNotificationsWithDeviceToken method in your app delegate, that will report the device token that will let you which device already accepted your share [assuming you have a log of the token IDs. – user3069232 Sep 25 '18 at 08:01
  • It makes sense to me. One a share has been accepted it is accessible on any of said individuals devices, it doesn't need to fire again. – user3069232 Sep 25 '18 at 08:02
  • Yes, I have didRegisterForRemoteNotificationsWithDeviceToken, but I do not know how to figure out of that token, that another device has been used. Could you please explain this. – Red Sep 25 '18 at 09:30
  • Red, I don't know what your trying to do ultimately. But imagine you log to iCloud every time a device is registered; that way you can search and check if user B is trying to register again maybe? – user3069232 Sep 25 '18 at 13:55
  • ok, got the idea! But this would mean, that every time the app comes up, I have to check in iCloud - this would work, but I'm wondering whether there is a better way – Red Sep 26 '18 at 04:47

1 Answers1

1

You need to create a subscription to the shared database when your app launches. That way, when a user accepts a share (on any device) the subscription will be triggered and all that user's devices (that have the subscription) will pull down the latest data in that database. It isn't necessary to accept the CKShare on multiple devices.

Here's an example of how you would create a subscription to the shared database:

let container = CKContainer(identifier: "...")

let subscription = CKDatabaseSubscription(subscriptionID: "shared")
let sharedInfo = CKNotificationInfo()
sharedInfo.shouldSendContentAvailable = true
sharedInfo.alertBody = "" //This needs to be set or pushes sometimes don't get sent
subscription.notificationInfo = sharedInfo

let operation = CKModifySubscriptionsOperation(subscriptionsToSave: [subscription], subscriptionIDsToDelete: nil)
container.sharedCloudDatabase.add(operation)

You will also need to process changes fetched from the shared database to show the new shared data in your app like this:

let fetchOperation = CKFetchDatabaseChangesOperation()
fetchOperation.fetchAllChanges = true
fetchOperation.recordZoneWithIDChangedBlock = { recordZoneID in
  //Process custom/shared zone changes...
}

container.sharedCloudDatabase.add(fetchOperation)

I hope that helps.

Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128
  • Thanks for the answer. I'm just wondering, how can I solve my issue, when the app on device B2 will be installed AFTER the user accepted on device B1. As far as I understand it, device B2 will not get the information. Is it right, that in that case I have to check whether there is already a CKShare object? – Red Sep 26 '18 at 05:27
  • When device B2 gets installed, it will subscribe to the shared database just like the other devices and automatically get access to the shared data. You don’t have to accept the `CKShare` again. The signed in iCloud account will have access to what is shared and accepted already. – Clifton Labrum Sep 26 '18 at 06:28
  • Understood, means, when device B2 gets the app first time installed ( after B1 has accepted the share) I need at first check, whether there is a shared Zone- correct? – Red Sep 26 '18 at 06:39
  • Yes, that's what creating the shared subscription does. It checks for a shared zone and triggers a download of all the shared records if one exists. – Clifton Labrum Sep 26 '18 at 18:20