swiftswiftuisnapchat

Where to place app delegate code in App.swift file?


I'm trying to get Snapkit working with SwiftUI to allow logins via SnapChat. I'm following along with this StackOverflow question (Can I use the Snapchat SDK (SnapKit) with SwiftUI?) but I'm having trouble getting the accepted solution to work. The code posted as the answer was intended to go in the app delegate file but as of the latest version of XCode they are no longer used. Instead, the code snippet needs to be placed in the AppName.swift file but my breakpoint doesn't trigger. Here's my current version of my App.swift file:

import SwiftUI
import SCSDKLoginKit

@main
struct SampleApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    for urlContext in URLContexts {
        let url = urlContext.url
        var options: [UIApplication.OpenURLOptionsKey : Any] = [:]
        options[.openInPlace] = urlContext.options.openInPlace
        options[.sourceApplication] = urlContext.options.sourceApplication
        options[.annotation] = urlContext.options.annotation
        SCSDKLoginClient.application(UIApplication.shared, open: url, options: options)
    }
}

Any help is greatly appreciated. Thanks!

EDIT: Here's the solution that worked thanks to Asperi! Updated code here in case anyone runs into this:

import SwiftUI
import SCSDKLoginKit

@main
struct SampleApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onOpenURL { url in
                    SCSDKLoginClient.application(UIApplication.shared, open: url)
                }
            }
        }
    }

Solution

  • You should use .onOpenURL instead, like

    @main
    struct SampleApp: App {
        var body: some Scene {
            WindowGroup {
                ContentView()
                  .onOpenURL { url in 
                      // .. do whatever needed here
                  }
            }
        }
    }