https://github.com/apple/sample-cloudkit-sharing
https://github.com/apple/sample-cloudkit-sharing
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/apple/sample-cloudkit-sharing
- Owner: apple
- License: mit
- Created: 2021-04-12T23:00:08.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-09-13T21:53:48.000Z (over 3 years ago)
- Last Synced: 2025-03-31T05:07:02.248Z (9 months ago)
- Language: Swift
- Size: 42 KB
- Stars: 308
- Watchers: 19
- Forks: 41
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# CloudKit Samples: Sharing
### Goals
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.
### Prerequisites
* A Mac with [Xcode 13](https://developer.apple.com/xcode/) (or later) installed is required to build and test this project.
* An active [Apple Developer Program membership](https://developer.apple.com/support/compare-memberships/) is needed to create a CloudKit container.
### Setup Instructions
* Ensure the simulator or device you run the project on is signed in to an Apple ID account with iCloud enabled. This can be done in the Settings app.
* If you wish to run the app on a device, ensure the correct developer team is selected in the “Signing & Capabilities” tab of the Sharing app target, and a valid iCloud container is selected under the “iCloud” section.
#### Using Your Own iCloud Container
* Create a new iCloud container through Xcode’s “Signing & Capabilities” tab of the Sharing app target.
* Update the `containerIdentifier` property in [Config.swift](Sharing/App/Config.swift) with your new iCloud container ID.
### How it Works
#### User One: Initiating the Share
* 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](https://developer.apple.com/documentation/uikit/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.
#### User Two: Accepting the Share Invitation
* 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.
### Note on Swift Concurrency
This project uses Swift concurrency APIs. A prior `completionHandler`-based implementation has been tagged [`pre-async`](https://github.com/apple/cloudkit-sample-sharing/tree/pre-async).
### Further Reading
* [Sharing CloudKit Data with Other iCloud Users](https://developer.apple.com/documentation/cloudkit/shared_records/sharing_cloudkit_data_with_other_icloud_users)