When developing iOS and macOS apps, you often need to handle scenarios where external schemes redirect the app and perform related actions, such as handling website or SDK Oauth login. In the conventional AppKit framework, there are already very mature best practices to refer to, but when we come to the SwiftUI 2.0 world, when AppDelegate no longer exists, how should we handle scheme redirection, the following is a small practical exploration.

OpenUrl

SwiftUI officially supported a modifier onOpenURL as a substitute for open(_:options:completionHandler:) in UIApplication world.

Registers a handler to invoke when the view receives a url for the scene or window the view is in.

Example

let’s set an example with OauthSwift, put your code with handleURL in onOpenURL block, and that’s it.

.onOpenURL { (url) in
    /// Handle Oauth
    OAuthSwift.handle(url: url)
}