From 1a54e737846e158f79892bd60dbb30acf024505e Mon Sep 17 00:00:00 2001 From: Will May <4763133+Will5@users.noreply.github.com> Date: Thu, 17 Nov 2022 16:32:42 -0500 Subject: [PATCH] iOS Authentication works in 0.4.0, but not in 0.4.1 - AQUIRE_ROOT_VIEW_CONTROLLER_FAILED --- ios/Classes/SwiftFlutterWebAuthPlugin.swift | 33 ++++++++++++++------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/ios/Classes/SwiftFlutterWebAuthPlugin.swift b/ios/Classes/SwiftFlutterWebAuthPlugin.swift index e334c20c..9c707157 100644 --- a/ios/Classes/SwiftFlutterWebAuthPlugin.swift +++ b/ios/Classes/SwiftFlutterWebAuthPlugin.swift @@ -54,20 +54,33 @@ public class SwiftFlutterWebAuthPlugin: NSObject, FlutterPlugin { let session = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackURLScheme, completionHandler: completionHandler) if #available(iOS 13, *) { - guard var topController = UIApplication.shared.keyWindow?.rootViewController else { - result(FlutterError.aquireRootViewControllerFailed) + var rootViewController: UIViewController? = nil + + // FlutterViewController + if (rootViewController == nil) { + rootViewController = UIApplication.shared.delegate?.window??.rootViewController as? FlutterViewController + } + + // UIViewController + if (rootViewController == nil) { + rootViewController = UIApplication.shared.keyWindow?.rootViewController + } + + // ACQUIRE_ROOT_VIEW_CONTROLLER_FAILED + if (rootViewController == nil) { + result(FlutterError.acquireRootViewControllerFailed) return } - while let presentedViewController = topController.presentedViewController { - topController = presentedViewController + while let presentedViewController = rootViewController!.presentedViewController { + rootViewController = presentedViewController } - if let nav = topController as? UINavigationController { - topController = nav.visibleViewController ?? topController + if let nav = rootViewController as? UINavigationController { + rootViewController = nav.visibleViewController ?? rootViewController } - guard let contextProvider = topController as? ASWebAuthenticationPresentationContextProviding else { - result(FlutterError.aquireRootViewControllerFailed) + guard let contextProvider = rootViewController as? ASWebAuthenticationPresentationContextProviding else { + result(FlutterError.acquireRootViewControllerFailed) return } session.presentationContextProvider = contextProvider @@ -100,7 +113,7 @@ extension FlutterViewController: ASWebAuthenticationPresentationContextProviding } fileprivate extension FlutterError { - static var aquireRootViewControllerFailed: FlutterError { - return FlutterError(code: "AQUIRE_ROOT_VIEW_CONTROLLER_FAILED", message: "Failed to aquire root view controller" , details: nil) + static var acquireRootViewControllerFailed: FlutterError { + return FlutterError(code: "ACQUIRE_ROOT_VIEW_CONTROLLER_FAILED", message: "Failed to acquire root view controller" , details: nil) } }