Search code examples
iosswiftxcodeios14xcode12

How to handle URL with AppDelegate in Swift5.3


I'm looking to handle a url like "myApp://oauth-callback/xxxx"

In Swift 5.3 we no longer have an "AppDelegate" file and the documentation is now obsolete. So I did this by following different documentation but it does not work... my print never appears

Any ideas ? (I am a beginner)

Thanks

@main
struct MyApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

class AppDelegate: NSObject, UIApplicationDelegate {
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey  : Any] = [:]) -> Bool {
        print("open via url")
        if url.host == "oauth-callback" {
            OAuthSwift.handle(url: url)
        }
        
      return true
    }
    
}


Solution

  • I used onOpenURL on ContentView

    @main
    struct MyApp: App {    
        var body: some Scene {
            WindowGroup {
                ContentView()
                    .onOpenURL(perform: { url in
                        print("URL")
                        print(url)
                    })
            }
        }
    }