Skip to content

iOS Authentication works in 0.4.0, but not in 0.4.1 - AQUIRE_ROOT_VIEW_CONTROLLER_FAILED #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions ios/Classes/SwiftFlutterWebAuthPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}