2022 App ServicesSwiftUI & UI FrameworksSystem Services
WWDC22 · 24 min · App Services / SwiftUI & UI Frameworks / System Services
Enhance collaboration experiences with Messages
Discover how you can help improve communication and collaboration in your app with Collaboration in Messages. Learn how to tie a document to Messages conversations for simple sharing and discussion. Explore how you can keep everyone in the conversation up to date on the latest activity in the document. And find out how you can add customizable UI in your app to manage collaboration details and connect documents to Messages conversations and FaceTime calls. To learn more about the SharedWithYou framework, we recommend watching "Add Shared with You to your app.” For more information on adding collaboration APIs to apps that have custom collaboration infrastructure, check out "Integrate your custom collaboration app with Messages.”
Watch at developer.apple.com ↗Code shown on screen · 14 snippets
Create a CloudKit collaboration item provider
// CloudKit collaboration object
// Starting collaboration
let itemProvider = NSItemProvider()
itemProvider.registerCKShare(container: container,
allowedSharingOptions: CKAllowedSharingOptions.standard,
preparationHandler: {
// Create your share and save to server, or throw error
return savedShare
})
// Inviting to existing collaboration
let itemProvider = NSItemProvider()
itemProvider.registerCKShare(share,
container: container,
allowedSharingOptions: CKAllowedSharingOptions.standard) Set up Share Sheet on iOS and Mac Catalyst
// Setting up Share Sheet - iOS and Mac Catalyst
let activityViewController = UIActivityViewController(activityItems: [collaborationObject], applicationActivities: nil)
presentingViewController.present(activityViewController, animated: true) Set up Share Popover on macOS
// Setting up Share Popover - macOS
let sharingServicePicker = NSSharingServicePicker(items: [collaborationObject])
sharingServicePicker.show(relativeTo: view.bounds, of: view, preferredEdge: .minY) Provide metadata for CloudKit collaboration in Share Sheet (iOS, Mac Catalyst)
// Providing CloudKit metadata - iOS
let configuration = UIActivityItemsConfiguration(itemProviders: [collaborationItemProvider])
configuration.perItemMetadataProvider = { (_, key) in
switch key {
case .linkPresentationMetadata:
// Create LPLinkMetadata with title and imageProvider
return metadata
default:
return nil
}
}
let activityViewController = UIActivityViewController(activityItemsConfiguration: configuration) Provide metadata for CloudKit collaboration in Share Popover (macOS)
// Providing CloudKit metadata - macOS
let title = “Shared Item”
let image = NSImage(contentsOfFile: “Shared_Item_Preview_Image.png”)
let icon = NSImage(contentsOfFile: “App_Icon.png”) // Shared item source
let previewRepresentingItem = NSPreviewRepresentingActivityItem(item: collaborationItemProvider,
title: title,
image: image,
icon: icon)
let picker = NSSharingServicePicker(items: [previewRepresentingItem]) Create Transferable object for CloudKit collaboration with ShareLink
// SwiftUI CloudKit Transferable
struct Note: Transferable {
// Properties of the note e.g. name, preview image, content, ID, …
var share: CKShare?
func saveCKShareToServer() async throws -> CKShare { … }
static var transferRepresentation: some TransferRepresentation {
CKShareTransferRepresentation { note in
if let share = note.share {
return .existing(share, container: container, options: options)
} else {
return .prepareShare(container: container, options: options) {
return try await note.saveCKShareToServer()
}
}
}
}
} Adopt ShareLink in SwiftUI
// SwiftUI ShareLink adoption
struct ContentView: View {
let item = ShareItem()
var body: some View {
ShareLink(item: item, preview: SharePreview(item.title, image: item.previewImage))
}
} Initialize the collaborationView
// Collaboration View
let collaborationView = SWCollaborationView(itemProvider: itemProvider)
collaborationView.activeParticipantCount = myModel.activePeople.count
collaborationView.contentView = MyView(model: myModel)
collaborationView.manageButtonTitle = "Custom Manage Button" Observe when CKShare is saved with CKSystemSharingUIObserver
// Observing CKShare Changes
let observer = CKSystemSharingUIObserver(container: container)
observer.systemSharingUIDidSaveShareBlock = { _, result in
switch result {
case .success(let share):
// Handle successfully starting share
case .failure(let error):
// Handle error
}
} Observe when CKShare is removed with CKSystemSharingUIObserver
// Observing CKShare Changes
observer.systemSharingUIDidStopSharingBlock = { _, result in
switch result {
case .success(let share):
// Handle successfully starting share
case .failure(let error):
// Handle error
}
} Posting notice for edit SWHighlightChangeEvent
// Post an SWHighlightChangeEvent Notice
let highlightCenter: SWHighlightCenter = self.highlightCenter
let highlight = try highlightCenter.collaborationHighlight(forURL: ckShareURL, error: &error)
let editEvent = SWHighlightChangeEvent(highlight: highlight, trigger: .edit)
highlightCenter.postNotice(for: editEvent) Post an SWHighlightMentionEvent Notice
// Post an SWHighlightMentionEvent Notice
let highlightCenter: SWHighlightCenter = self.highlightCenter
let highlight = try highlightCenter.collaborationHighlight(forURL: ckShareURL, error: &error)
let mentionEvent = SWHighlightMentionEvent(highlight: highlight,
mentionedPersonCloudKitShareHandle: ckShareParticipantHandle)
highlightCenter.postNotice(for: mentionEvent) Post an SWHighlightPersistenceEvent Notice
// Post an SWHighlightPersistenceEvent Notice
let highlightCenter: SWHighlightCenter = self.highlightCenter
let highlight = try highlightCenter.collaborationHighlight(forURL: ckShareURL, error: &error)
let renamedEvent = SWHighlightPersistenceEvent(highlight: highlight, trigger: .renamed)
highlightCenter.postNotice(for: renamedEvent) Post an SWHighlightMembershipEvent Notice
// Post an SWHighlightMembershipEvent Notice
let highlightCenter: SWHighlightCenter = self.highlightCenter
let highlight = try highlightCenter.collaborationHighlight(forURL: ckShareURL, error: &error)
let membershipEvent = SWHighlightMembershipEvent(highlight: highlight,
trigger: .addedCollaborator)
highlightCenter.postNotice(for: membershipEvent) Related sessions
-
34 min -
14 min -
23 min -
28 min -
18 min -
27 min