From 501d22ae85e9850802d4296e98a6254301cfd683 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Mon, 1 Sep 2025 21:48:54 -0500 Subject: [PATCH 1/2] Document scene delegate methods for accepting shares. --- Examples/CloudKitDemo/CloudKitDemoApp.swift | 17 +++++++++++++- .../CloudKitPlaygroundApp.swift | 21 +++++++++++++++-- Examples/Reminders/RemindersApp.swift | 17 +++++++++++++- .../Articles/CloudKitSharing.md | 23 +++++++++++++++---- 4 files changed, 70 insertions(+), 8 deletions(-) diff --git a/Examples/CloudKitDemo/CloudKitDemoApp.swift b/Examples/CloudKitDemo/CloudKitDemoApp.swift index e96f652b..6fb3c017 100644 --- a/Examples/CloudKitDemo/CloudKitDemoApp.swift +++ b/Examples/CloudKitDemo/CloudKitDemoApp.swift @@ -52,8 +52,9 @@ struct CloudKitDemoApp: App { } class SceneDelegate: UIResponder, UIWindowSceneDelegate { - var window: UIWindow? @Dependency(\.defaultSyncEngine) var syncEngine + var window: UIWindow? + func windowScene( _ windowScene: UIWindowScene, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata @@ -62,5 +63,19 @@ struct CloudKitDemoApp: App { try await syncEngine.acceptShare(metadata: cloudKitShareMetadata) } } + + func scene( + _ scene: UIScene, + willConnectTo session: UISceneSession, + options connectionOptions: UIScene.ConnectionOptions + ) { + guard let cloudKitShareMetadata = connectionOptions.cloudKitShareMetadata + else { + return + } + Task { + try await syncEngine.acceptShare(metadata: cloudKitShareMetadata) + } + } } #endif diff --git a/Examples/CloudKitPlayground/CloudKitPlaygroundApp.swift b/Examples/CloudKitPlayground/CloudKitPlaygroundApp.swift index 44c9512f..b9010b25 100644 --- a/Examples/CloudKitPlayground/CloudKitPlaygroundApp.swift +++ b/Examples/CloudKitPlayground/CloudKitPlaygroundApp.swift @@ -12,7 +12,9 @@ struct CloudKitPlaygroundApp: App { $0.defaultDatabase = try! appDatabase() $0.defaultSyncEngine = try! SyncEngine( for: $0.defaultDatabase, - tables: ModelA.self, ModelB.self, ModelC.self + tables: ModelA.self, + ModelB.self, + ModelC.self ) } } @@ -48,12 +50,27 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } class SceneDelegate: UIResponder, UIWindowSceneDelegate { + @Dependency(\.defaultSyncEngine) var syncEngine var window: UIWindow? + func windowScene( _ windowScene: UIWindowScene, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata ) { - @Dependency(\.defaultSyncEngine) var syncEngine + Task { + try await syncEngine.acceptShare(metadata: cloudKitShareMetadata) + } + } + + func scene( + _ scene: UIScene, + willConnectTo session: UISceneSession, + options connectionOptions: UIScene.ConnectionOptions + ) { + guard let cloudKitShareMetadata = connectionOptions.cloudKitShareMetadata + else { + return + } Task { try await syncEngine.acceptShare(metadata: cloudKitShareMetadata) } diff --git a/Examples/Reminders/RemindersApp.swift b/Examples/Reminders/RemindersApp.swift index 6640a5cd..14cd2e38 100644 --- a/Examples/Reminders/RemindersApp.swift +++ b/Examples/Reminders/RemindersApp.swift @@ -55,12 +55,27 @@ class AppDelegate: UIResponder, UIApplicationDelegate, ObservableObject { } class SceneDelegate: UIResponder, UIWindowSceneDelegate { + @Dependency(\.defaultSyncEngine) var syncEngine var window: UIWindow? + func windowScene( _ windowScene: UIWindowScene, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata ) { - @Dependency(\.defaultSyncEngine) var syncEngine + Task { + try await syncEngine.acceptShare(metadata: cloudKitShareMetadata) + } + } + + func scene( + _ scene: UIScene, + willConnectTo session: UISceneSession, + options connectionOptions: UIScene.ConnectionOptions + ) { + guard let cloudKitShareMetadata = connectionOptions.cloudKitShareMetadata + else { + return + } Task { try await syncEngine.acceptShare(metadata: cloudKitShareMetadata) } diff --git a/Sources/SharingGRDBCore/Documentation.docc/Articles/CloudKitSharing.md b/Sources/SharingGRDBCore/Documentation.docc/Articles/CloudKitSharing.md index a492d29e..eae1c840 100644 --- a/Sources/SharingGRDBCore/Documentation.docc/Articles/CloudKitSharing.md +++ b/Sources/SharingGRDBCore/Documentation.docc/Articles/CloudKitSharing.md @@ -63,10 +63,10 @@ configure how they want to share the record. A record can be _unshared_ by prese ## Accepting shared records Extra steps must be taken to allow a user to _accept_ a shared record. Once the user taps on the -share link sent to them (whether that is by text, email, etc.), the app will be launched and a -special `userDidAcceptCloudKitShareWith` delegate method will be invoked in the app's scene -delegate. Your app must implement this delegate method and invoke the -``SyncEngine/acceptShare(metadata:)`` method. +share link sent to them (whether that is by text, email, etc.), the app will be launched with +special options provided or a special delegate method will be invoked in the app's scene delegate. +You must impelement these delegate methods and invoke the ``SyncEngine/acceptShare(metadata:)`` +method. As a simplified example, a `UIWindowSceneDelegate` subclass can implement the delegate method like so: @@ -75,6 +75,7 @@ so: class SceneDelegate: UIResponder, UIWindowSceneDelegate { @Dependency(\.defaultSyncEngine) var syncEngine var window: UIWindow? + func windowScene( _ windowScene: UIWindowScene, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata @@ -83,6 +84,20 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { try await syncEngine.acceptShare(metadata: cloudKitShareMetadata) } } + + func scene( + _ scene: UIScene, + willConnectTo session: UISceneSession, + options connectionOptions: UIScene.ConnectionOptions + ) { + guard let cloudKitShareMetadata = connectionOptions.cloudKitShareMetadata + else { + return + } + Task { + try await syncEngine.acceptShare(metadata: cloudKitShareMetadata) + } + } } ``` From ac2cfa0b95c991b677ba68d540bbbbc603b11fac Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Tue, 2 Sep 2025 10:06:08 -0700 Subject: [PATCH 2/2] Update Sources/SharingGRDBCore/Documentation.docc/Articles/CloudKitSharing.md --- .../Documentation.docc/Articles/CloudKitSharing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SharingGRDBCore/Documentation.docc/Articles/CloudKitSharing.md b/Sources/SharingGRDBCore/Documentation.docc/Articles/CloudKitSharing.md index eae1c840..c9b68901 100644 --- a/Sources/SharingGRDBCore/Documentation.docc/Articles/CloudKitSharing.md +++ b/Sources/SharingGRDBCore/Documentation.docc/Articles/CloudKitSharing.md @@ -65,7 +65,7 @@ configure how they want to share the record. A record can be _unshared_ by prese Extra steps must be taken to allow a user to _accept_ a shared record. Once the user taps on the share link sent to them (whether that is by text, email, etc.), the app will be launched with special options provided or a special delegate method will be invoked in the app's scene delegate. -You must impelement these delegate methods and invoke the ``SyncEngine/acceptShare(metadata:)`` +You must implement these delegate methods and invoke the ``SyncEngine/acceptShare(metadata:)`` method. As a simplified example, a `UIWindowSceneDelegate` subclass can implement the delegate method like