p2 / OAuth2

OAuth2 framework for macOS and iOS, written in Swift.
Other
1.14k stars 276 forks source link

Change type of authorize context to ASPresentationAnchor #361

Closed longinius closed 2 years ago

longinius commented 3 years ago

The type of the presentation anchor for the ASWebAuthenticationSession needs to be an ASPresentationAnchor to work with SwiftUI. The change was introduced in 5.3.1, version 5.3.0 worked perfectly fine with SwiftUI.

In a pure SwiftUI app it's not easy (is it even possible?) to get a reference for a UIWindow.

With this change it's possible to provide an ASPresentationAnchor in SwiftUI like:

let presentationAnchor = ASPresentationAnchor()

oauth2.authConfig.authorizeContext = anchor

UIKit applications can provide a reference to a UIWindow since the ASPresentationAnchor is a typealias of the UIWindow. So instead of getting the UIWindow reference from a UIViewController, provide a reference to a UIWindow directly to the authorizeContext.

ossus-lib commented 3 years ago

This means the README must also be updated, and since it's likely a breaking change for existing users, maybe it should allows to keep the old behavior but fallback to the new one?

longinius commented 3 years ago

Yes, you are right that it is incompatible with the previous version and is basically a breaking change. With version 5.3.1 the change was introduced, the previous version 5.3.0 works fine with SwiftUI, because there the ASPresentationAnchor is used.

I will update the pull request and provide a solution which allows to pass an ASPresentationAnchor or alternatively a UIViewController as context. So we should have all possibilities covered.

longinius commented 3 years ago

I haven't explicitly tested the variant with the UIViewController, but it's basically the same code only with the ASPresentationAnchor checked before it falls back to the UIViewController.

With this customization you can pass a UIViewController as before or alternatively an ASPresentationAnchor for SwiftUI or a UIWindow directly as context.

dyegos commented 3 years ago

Is this still alive? We are looking forward to this change to use it in SwiftUI