Emile
Emile

Reputation: 11

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

Upvotes: 0

Views: 1345

Answers (2)

Emile
Emile

Reputation: 11

I used onOpenURL on ContentView

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

Upvotes: 1

PGDev
PGDev

Reputation: 24341

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

Upvotes: 2

Related Questions