[GH-ISSUE #594] SwiftUI Webview authorizeURLHandler #386

Closed
opened 2026-03-03 16:48:15 +03:00 by kerem · 5 comments
Owner

Originally created by @Veeit on GitHub (May 6, 2020).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/594

Description:

My SwiftUI application is working fine with oAuthSwift, the only problem is I use Safari for the login handler. But to get the app to the store I need to use a WebView inside my application, the problem that I have now is that I don't know how to create a authorizeURLHandler that opens a SwiftUI sheet with the WebView inside.

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

Makerlog

OAuth Version:

  • Version 1
  • Version 2

OS (Please fill the version) :

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

Installation method:

  • Carthage
  • CocoaPods
  • Swift Package Manager
  • Manually

Library version:

  • head
  • v2.1.0
  • v2.0.0
  • v1.4.1
  • v1.2 (Swift 4.0)
  • other: (Please fill in the version you are using.)

Xcode version:

  • 11.x (Swift 5.1)

  • 10.x (Swift 5.0)

  • 10.x (Swift 4.1)

  • 9.3 (Swift 4.1)

  • 9.0 (Swift 4.0)

  • other: (Please fill in the version you are using.)

  • objective c

Originally created by @Veeit on GitHub (May 6, 2020). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/594 ### Description: My SwiftUI application is working fine with oAuthSwift, the only problem is I use Safari for the login handler. But to get the app to the store I need to use a WebView inside my application, the problem that I have now is that I don't know how to create a authorizeURLHandler that opens a SwiftUI sheet with the WebView inside. ### OAuth Provider? (Twitter, Github, ..): Makerlog ### OAuth Version: - [ ] Version 1 - [x] Version 2 ### OS (Please fill the version) : - [x] iOS : - [ ] OSX : - [ ] TVOS : - [ ] WatchOS : ### Installation method: - [ ] Carthage - [ ] CocoaPods - [x] Swift Package Manager - [ ] Manually ### Library version: - [x] head - [ ] v2.1.0 - [ ] v2.0.0 - [ ] v1.4.1 - [ ] v1.2 (Swift 4.0) - [ ] other: (Please fill in the version you are using.) ### Xcode version: - [x] 11.x (Swift 5.1) - [ ] 10.x (Swift 5.0) - [ ] 10.x (Swift 4.1) - [ ] 9.3 (Swift 4.1) - [ ] 9.0 (Swift 4.0) - [ ] other: (Please fill in the version you are using.) - [ ] objective c
kerem 2026-03-03 16:48:15 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

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

good question, never tested
did you try other handler like SafariURLHandler or ASWebAuthenticationURLHandler?

<!-- gh-comment-id:624746635 --> @phimage commented on GitHub (May 6, 2020): good question, never tested did you try other handler like SafariURLHandler or ASWebAuthenticationURLHandler?
Author
Owner

@Veeit commented on GitHub (May 6, 2020):

I didn't I just looked at SafariURLHandler but I need to add a Controller and I don't have one. I have created a function inside my ObservableObject for the login.
The big problem is I need to warp the WebView (or what ever the webpage displays) inside a UIViewControllerRepresentable to use it with swiftUI.

<!-- gh-comment-id:624749886 --> @Veeit commented on GitHub (May 6, 2020): I didn't I just looked at SafariURLHandler but I need to add a Controller and I don't have one. I have created a function inside my ObservableObject for the login. The big problem is I need to warp the WebView (or what ever the webpage displays) inside a UIViewControllerRepresentable to use it with swiftUI.
Author
Owner

@Veeit commented on GitHub (May 6, 2020):

https://github.com/Veeit/makerslog-App/blob/release-v1/Shared/View%20Model/Api/LoginData.swift

line 258 is the login function, if you are interested in the code :)

<!-- gh-comment-id:624751663 --> @Veeit commented on GitHub (May 6, 2020): https://github.com/Veeit/makerslog-App/blob/release-v1/Shared/View%20Model/Api/LoginData.swift line 258 is the login function, if you are interested in the code :)
Author
Owner

@Veeit commented on GitHub (May 21, 2020):

        var root = UIHostingController(rootView: ContentView())

        oauthswift.authorizeURLHandler = SafariURLHandler(viewController: root, oauthSwift: oauthswift)

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = root
            self.window = window
            window.makeKeyAndVisible()
        }

Got it working :) I need to configure the SafariURLHandler like this inside the SceneDelegate, actually it took me a while to get to thins solution but that is simple.

<!-- gh-comment-id:632305305 --> @Veeit commented on GitHub (May 21, 2020): ```swift var root = UIHostingController(rootView: ContentView()) oauthswift.authorizeURLHandler = SafariURLHandler(viewController: root, oauthSwift: oauthswift) if let windowScene = scene as? UIWindowScene { let window = UIWindow(windowScene: windowScene) window.rootViewController = root self.window = window window.makeKeyAndVisible() } ``` Got it working :) I need to configure the SafariURLHandler like this inside the SceneDelegate, actually it took me a while to get to thins solution but that is simple.
Author
Owner

@phimage commented on GitHub (May 24, 2020):

thanks for the code it could help other people

<!-- gh-comment-id:633295271 --> @phimage commented on GitHub (May 24, 2020): thanks for the code it could help other people
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#386
No description provided.