This project demonstrates sharing CloudKit records across user accounts. It shows how to initiate a share from one user account, and accept the share and subsequently view shared data on another account.
containerIdentifier
property in Config.swift with your new iCloud container ID.On either a device or simulator with a signed-in iCloud account, User One creates a new Contact record through the UI with a name and phone number. The Contact is saved to the user’s private iCloud database with the addContact(name:phoneNumber:completionHandler)
function in ViewModel.swift.
After the Contacts list is refreshed, the newly added Contact will appear under the “Private” section of the UI.
Tapping the share button on the Contact list entry creates a CKShare
object and writes it to the database with the createShare(contact:completionHandler)
function in ViewModel.swift. After the share is created, the CloudSharingView
is displayed which wraps UICloudSharingController in a SwiftUI compatible view. This view allows the user to configure share options and send or copy the share link to share with User Two.
On a separate device with a different signed-in iCloud account, User Two accepts the share by following the link provided by User One.
The link initiates a prompt on the user’s device to accept the share, which launches the Sharing app and accepts the share through a database operation defined in SceneDelegate’s userDidAcceptCloudKitShareWith
delegate callback.
After the share is accepted and the UI is refreshed, the shared Contact will display in User Two’s Contacts list in the “Shared” section. fetchSharedContacts(completionHandler:)
in ViewModel.swift shows how Contacts in shared database zones are fetched.
This project uses Swift concurrency APIs. A prior completionHandler
-based implementation has been tagged pre-async
.