Accept CKShare with SwiftUI App lifecycle

In iOS 15, macOS 12, tvOS 15 and watchOS 8 we can finally share NSManagedObjects using NSPersistentCloudKitContainer 🎉

I've been playing around with it and I've managed to share an object from one Apple ID to another. But I'm unable to accept the invite. 😰

If I understand the docs correctly I'm supposed to call func acceptShareInvitations(…) with CKShare.Metadata representing the invite.

This metadata should be forwarded to my app using either AppDelegate or SceneDelegate.

The problem is that I'm using the SwiftUI App-protocol. I tried setting up an UIApplicationDelegateAdaptor with this code:

  • ivar added
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
  • AppDelegate implemented
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata) {
        assertionFailure()
    }
}

The assertionFailure is never called, so my guess is that the SceneDelegate is being called instead.

So, how do I implement the following two SceneDelegate-calls in an app that uses the SwiftUI App lifecycle?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        assertionFailure()
    }

func windowScene(_ windowScene: UIWindowScene, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata) {
        assertionFailure()
    }
Accept CKShare with SwiftUI App lifecycle
 
 
Q