0

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
    }
    
}

Emile
  • 11
  • 3

2 Answers2

2

You need to implement scene(_:openURLContexts:) method in your SceneDelegate.

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    if let url = URLContexts.first?.url else {
        print(url)
        if url.host == "oauth-callback" {
            OAuthSwift.handle(url: url)
        }
    }
}
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • 1
    You don't have SceneDelegate in swift 5.3 / Xcode 12 **SceneDelegate** & **AppDelegate** have been replaced by single file **NameApp.swift** – Emile Sep 25 '20 at 12:47
1

I used onOpenURL on ContentView

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