[GH-ISSUE #626] How can I use SafariURLHandler in SwiftUI App Lifecycle? #408

Closed
opened 2026-03-03 16:48:26 +03:00 by kerem · 4 comments
Owner

Originally created by @kkebo on GitHub (Oct 6, 2020).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/626

Description:

The README.md describes how to use the SFSafariViewController. However, how can I use it in SwiftUI App Lifecycle like the below?

@main
struct FooApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

OAuth Provider? (Twitter, Github, ..):

Any

OAuth Version:

  • Version 1
  • Version 2

OS (Please fill the version) :

  • iOS :
  • OSX :
  • TVOS :
  • WatchOS :

Xcode version:

  • other: 12.x (Swift 5.3)
Originally created by @kkebo on GitHub (Oct 6, 2020). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/626 ### Description: The [README.md](https://github.com/OAuthSwift/OAuthSwift#use-the-sfsafariviewcontroller-ios9) describes how to use the `SFSafariViewController`. However, how can I use it in SwiftUI App Lifecycle like the below? ```swift @main struct FooApp: App { var body: some Scene { WindowGroup { ContentView() } } } ``` ### OAuth Provider? (Twitter, Github, ..): Any ### OAuth Version: - [ ] Version 1 - [x] Version 2 ### OS (Please fill the version) : - [x] iOS : - [ ] OSX : - [ ] TVOS : - [ ] WatchOS : ### Xcode version: - [x] other: 12.x (Swift 5.3)
kerem closed this issue 2026-03-03 16:48:26 +03:00
Author
Owner

@phimage commented on GitHub (Oct 6, 2020):

not without some adaptation

you must look at SFSafariViewController and swiftui

I never tested it but I think you must implement UIViewControllerRepresentable and provide the controller used with makeUIViewController(
maybe SafariURLHandler could implement it or add another object to encapsulate (do not hesitate to share if you have a solution and to close this issue)

<!-- gh-comment-id:704200209 --> @phimage commented on GitHub (Oct 6, 2020): not without some adaptation you must look at SFSafariViewController and swiftui I never tested it but I think you must implement [UIViewControllerRepresentable](https://developer.apple.com/documentation/swiftui/uiviewcontrollerrepresentable) and provide the controller used with ` makeUIViewController(` maybe SafariURLHandler could implement it or add another object to encapsulate (do not hesitate to share if you have a solution and to close this issue)
Author
Owner

@kkebo commented on GitHub (Oct 6, 2020):

Thanks you @phimage.

SafariURLHandler(present:,dismiss:,oauthSwift:) may be what I was looking for. If it works, I'll share it and close this issue.

<!-- gh-comment-id:704420083 --> @kkebo commented on GitHub (Oct 6, 2020): Thanks you @phimage. `SafariURLHandler(present:,dismiss:,oauthSwift:)` may be what I was looking for. If it works, I'll share it and close this issue.
Author
Owner

@kkebo commented on GitHub (Nov 10, 2020):

BTW, I gave up using OAuthSwift with SwiftUI's App Lifecycle.

<!-- gh-comment-id:724726316 --> @kkebo commented on GitHub (Nov 10, 2020): BTW, I gave up using OAuthSwift with SwiftUI's App Lifecycle.
Author
Owner

@kkebo commented on GitHub (Nov 20, 2020):

I succeeded in using OAuthSwift with SwiftUI App Lifecycle. I gave up using SafariURLHandler, but ASWebAuthenticationURLHandler and ASWebAuthenticationSessionURLHandler of #621 can be used with SwiftUI App Lifecyle.

Here is an example.

@main
struct DemoApp {}

extension DemoApp: App {
    var body: some Scene {
        DemoScene()
    }
}

struct DemoScene {
    init() {
        <SingletonDataManager>.oauthSwift.authorizeURLHandler = ASWebAuthenticationSessionURLHandler(
            callbackURLScheme: <CallbackURLScheme>,
            presentationAnchor: nil  // If the app doesn't support for multiple windows, it doesn't matter to use nil.
        )
    }
}

extension DemoScene: Scene {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
<!-- gh-comment-id:730808152 --> @kkebo commented on GitHub (Nov 20, 2020): I succeeded in using OAuthSwift with SwiftUI App Lifecycle. I gave up using `SafariURLHandler`, but [`ASWebAuthenticationURLHandler`](https://github.com/OAuthSwift/OAuthSwift/blob/master/Sources/Handler/ASWebAuthenticationURLHandler.swift) and [`ASWebAuthenticationSessionURLHandler`](https://github.com/raxityo/OAuthSwiftAuthenticationServices/blob/1.0.0/Sources/OAuthSwiftAuthenticationServices/ASWebAuthenticationSessionURLHandler.swift) of #621 can be used with SwiftUI App Lifecyle. Here is an example. ```swift @main struct DemoApp {} extension DemoApp: App { var body: some Scene { DemoScene() } } struct DemoScene { init() { <SingletonDataManager>.oauthSwift.authorizeURLHandler = ASWebAuthenticationSessionURLHandler( callbackURLScheme: <CallbackURLScheme>, presentationAnchor: nil // If the app doesn't support for multiple windows, it doesn't matter to use nil. ) } } extension DemoScene: Scene { var body: some Scene { WindowGroup { ContentView() } } } ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#408
No description provided.