2

In the iOS 13 world, I had code like this:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
        func windowScene(_ windowScene: UIWindowScene, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata) {
                // do stuff with the metadata, eventually call CKAcceptSharesOperation
        }
}

I am migrating my app to the new SwiftUI app lifecycle, and can’t figure out where to put this method. It used to live in AppDelegate pre-iOS13, and I tried going back to that, but the AppDelegate version never gets called.

There doesn’t seem to be a SceneDelegateAdaptor akin to UIApplicationDelegateAdaptor available, which would provide a bridge to the old code.

So, I’m lost. How do I accept CloudKit shares with SwiftUI app lifecycle?

Jaanus
  • 17,688
  • 15
  • 65
  • 110
  • There is no one. Submit feedback to Apple, maybe they will include it till release... and, probably, it is not time for yet to migrate to SwiftUI life-cycle. – Asperi Aug 07 '20 at 06:29

1 Answers1

1

You can still use AppDelegate with SwiftUI's new life-cycle until Apple releases APIs to handle this natively in SwiftUI’s App Life-cycle.

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        return true
    }
}

@main
struct MyApp: App {

    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Read this for more

koen
  • 5,383
  • 7
  • 50
  • 89
Ishmeet
  • 707
  • 6
  • 18
  • 2
    Some methods of AppDelegate are called, but not all. In iOS 13, some callbacks moved from AppDelegate to SceneDelegate. In iOS 14 with SwiftUI, neither AppDelegate nor SceneDelegate version of `userDidAcceptCloudKitShareWith` seems to be called. – Jaanus Aug 07 '20 at 10:37
  • @Jaanus is there a workout around? I have the same issue https://stackoverflow.com/questions/67541441/using-uiapplicationdelegateadaptor-to-get-callbacks-from-userdidacceptcloudkitsh – GarySabo May 15 '21 at 15:33
  • The only workaround that I know is to keep using UIKit app lifecycle until this gets fixed in iOS SDK. (It works fine in AppKit with SwiftUI lifecycle, but not UIKit.) – Jaanus May 16 '21 at 18:46