From 7c36c44659134329c9d125f1327e04c491a28279 Mon Sep 17 00:00:00 2001 From: opficdev Date: Thu, 12 Feb 2026 16:35:20 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20TopViewControllerProvider=EB=A5=BC=20?= =?UTF-8?q?=EC=9E=AC=ED=99=9C=EC=9A=A9=ED=95=98=EC=97=AC=20MainActor=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GithubAuthenticationService.swift | 9 ++-- .../GoogleAuthenticationService.swift | 32 ------------- .../Util/TopViewControllerProvider.swift | 46 +++++++++++++++++++ 3 files changed, 49 insertions(+), 38 deletions(-) create mode 100644 DevLog/Infra/Util/TopViewControllerProvider.swift diff --git a/DevLog/Infra/Service/SocialLogin/GithubAuthenticationService.swift b/DevLog/Infra/Service/SocialLogin/GithubAuthenticationService.swift index 05b6da5..22e18dd 100644 --- a/DevLog/Infra/Service/SocialLogin/GithubAuthenticationService.swift +++ b/DevLog/Infra/Service/SocialLogin/GithubAuthenticationService.swift @@ -18,6 +18,7 @@ final class GithubAuthenticationService: NSObject, AuthenticationService { private let messaging = Messaging.messaging() private var user: User? { Auth.auth().currentUser } private let providerID = AuthProviderID.gitHub + private let provider = TopViewControllerProvider() private let logger = Logger(category: "GithubAuthService") func signIn() async throws -> AuthenticationDataResponse { @@ -143,6 +144,7 @@ final class GithubAuthenticationService: NSObject, AuthenticationService { } } + @MainActor func requestAuthorizationCode() async throws -> String { guard let clientID = Bundle.main.object(forInfoDictionaryKey: "GITHUB_CLIENT_ID") as? String, let redirectURL = Bundle.main.object(forInfoDictionaryKey: "APP_REDIRECT_URL") as? String, @@ -248,12 +250,7 @@ final class GithubAuthenticationService: NSObject, AuthenticationService { extension GithubAuthenticationService: ASWebAuthenticationPresentationContextProviding { func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor { - guard let window = UIApplication.shared.connectedScenes - .flatMap({ ($0 as? UIWindowScene)?.windows ?? [] }) - .first(where: { $0.isKeyWindow }) else { - return ASPresentationAnchor() - } - return window + return provider.keyWindow() ?? ASPresentationAnchor() } struct GitHubUser: Codable { diff --git a/DevLog/Infra/Service/SocialLogin/GoogleAuthenticationService.swift b/DevLog/Infra/Service/SocialLogin/GoogleAuthenticationService.swift index 5798975..55c4b1f 100644 --- a/DevLog/Infra/Service/SocialLogin/GoogleAuthenticationService.swift +++ b/DevLog/Infra/Service/SocialLogin/GoogleAuthenticationService.swift @@ -126,35 +126,3 @@ final class GoogleAuthenticationService: AuthenticationService { } } - -final class TopViewControllerProvider { - @MainActor - func topViewController() -> UIViewController? { - let keyWindow = UIApplication.shared.connectedScenes - .compactMap { $0 as? UIWindowScene } - .flatMap { $0.windows } - .first { $0.isKeyWindow } - - let rootController = keyWindow?.rootViewController - - return topViewController(controller: rootController) - } - - @MainActor - private func topViewController(controller: UIViewController?) -> UIViewController? { - if let navigationController = controller as? UINavigationController { - return topViewController(controller: navigationController.visibleViewController) - } - - if let tabController = controller as? UITabBarController, - let selectedController = tabController.selectedViewController { - return topViewController(controller: selectedController) - } - - if let presentedController = controller?.presentedViewController { - return topViewController(controller: presentedController) - } - - return controller - } -} diff --git a/DevLog/Infra/Util/TopViewControllerProvider.swift b/DevLog/Infra/Util/TopViewControllerProvider.swift new file mode 100644 index 0000000..ba5304c --- /dev/null +++ b/DevLog/Infra/Util/TopViewControllerProvider.swift @@ -0,0 +1,46 @@ +// +// TopViewControllerProvider.swift +// DevLog +// +// Created by 최윤진 on 2/12/26. +// + +import UIKit + +final class TopViewControllerProvider { + @MainActor + func topViewController() -> UIViewController? { + guard let keyWindow = keyWindow() else { + return nil + } + + let rootController = keyWindow.rootViewController + return topViewController(controller: rootController) + } + + @MainActor + func keyWindow() -> UIWindow? { + return UIApplication.shared.connectedScenes + .compactMap { $0 as? UIWindowScene } + .flatMap { $0.windows } + .first { $0.isKeyWindow } + } + + @MainActor + private func topViewController(controller: UIViewController?) -> UIViewController? { + if let navigationController = controller as? UINavigationController { + return topViewController(controller: navigationController.visibleViewController) + } + + if let tabController = controller as? UITabBarController, + let selectedController = tabController.selectedViewController { + return topViewController(controller: selectedController) + } + + if let presentedController = controller?.presentedViewController { + return topViewController(controller: presentedController) + } + + return controller + } +}