-
Get the most out of CloudKit Sharing
Discover how apps can use CloudKit to share records with others. We'll show you how to encourage collaboration between people using your app and support those interactions with Apple frameworks. Learn how to create and manage shares, explore sharing options like public permissions, and find out how you can use zone sharing in iOS 15 and macOS Monterey to share entire record zones of data.
To get the most out of this session, we recommend being familiar with CloudKit and a basic understanding of record and data types.Recursos
Vídeos relacionados
WWDC23
WWDC21
-
Buscar neste vídeo...
-
-
1:58 - Create a new Contact record
// Create a new Contact record func addContact(name: String, phoneNumber: String) async throws { let id = CKRecord.ID(zoneID: recordZone.zoneID) let contactRecord = CKRecord(recordType: "Contact", recordID: id) contactRecord["name"] = name contactRecord["phoneNumber"] = phoneNumber try await privateCloudDatabase.save(contactRecord) } -
4:09 - Preparing a CKShare
// Preparing a CKShare func createShare(contactRecord: CKRecord) async throws -> CKShare { let share = CKShare(rootRecord: contactRecord) try await privateCloudDatabase.modifyRecords( saving: [contactRecord, share], deleting: [] ) return share } -
5:25 - UICloudSharingControllerDelegate
// UICloudSharingControllerDelegate public protocol UICloudSharingControllerDelegate { // ... // Called after the CloudKit sharing controller failed to save the share record. func cloudSharingController(UICloudSharingController, failedToSaveShareWithError: Error) // Called after the CloudKit sharing controller saves the share record. func cloudSharingControllerDidSaveShare(UICloudSharingController) // Called after the user decided to stop sharing the record. func cloudSharingControllerDidStopSharing(UICloudSharingController) } -
6:27 - Processing an accepted share invitation
// Processing a user’s acceptance of a share invitation func application( _ application: UIApplication, userDidAcceptCloudKitShareWith shareMetadata: CKShare.Metadata ) { let container = CKContainer(identifier: shareMetadata.containerIdentifier) Task { do { try await container.accept(shareMetadata) } catch { // Handle errors that may occur } } } -
7:24 - Fetching shared records
// Fetching records shared with the current iCloud user func fetchSharedContacts(in zone: CKRecordZone) async throws { var changeToken: CKServerChangeToken? = nil var moreChangesComing = true while moreChangesComing { let changes = try await sharedCloudDatabase.recordZoneChanges( inZoneWith: zone.zoneID, since: changeToken ) // Process changes as needed (modifications and deletions) processChanges(changes) moreChangesComing = changes.moreComing changeToken = changes.changeToken } } -
9:16 - Search: Phone Number
// Search by phone number let phoneNumber = "417-555-9311" let participant = try await container.shareParticipant(forPhoneNumber: phoneNumber) -
9:16 - Search: Email Address
// Search by email address let emailAddress = "dave_knox@icloud.com" let participant = try await container.shareParticipant(forEmailAddress: emailAddress) -
9:16 - Search: Record ID
// Search by user record ID let participant = try await container.shareParticipant(forUserRecordID: recordID) -
9:32 - Add participant to a share
// Add participant to existing CKShare record func addParticipant(_ participant: CKShare.Participant, to share: CKShare) async throws { participant.permission = .readWrite share.addParticipant(participant) try await privateCloudDatabase.save(share) } -
9:47 - Confirm invitation acceptance
// Fetch CKShare.Metadata and confirm accepting share from a given URL func confirmShareParticipation(from url: URL) async throws { let shareMetadata = try await container.shareMetadata(for: url) try await container.accept(shareMetadata) } -
11:14 - Share an entire record zone
// Create a CKShare sharing an entire record zone func createAndSaveShare(for zone: CKRecordZone) async throws -> CKShare { let share = CKShare(recordZoneID: zone.zoneID) try await privateCloudDatabase.save(share) if share.recordID.recordName == CKRecordNameZoneWideShare { // This is managing a shared record zone } return share }
-