Skip to content

Conversation

@Barba2k2
Copy link

Summary

  • Fixed crash on iOS where rootViewController was force-unwrapped during plugin registration when it was still nil
  • Changed viewController from a static stored property to a computed property that safely retrieves the root view controller
  • Uses UIWindowScene for modern iOS with fallback for older versions

Problem

The app crashed with "force unwrapped a nil value" at line 64 of SwiftFlutterBarcodeScannerPlugin.swift during plugin registration because rootViewController is not yet available at that point in the app lifecycle.

Solution

Changed the static viewController property to a computed property that lazily and safely fetches the root view controller when actually needed (not during registration).

public static var viewController: UIViewController? {
    if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
       let rootVC = windowScene.windows.first?.rootViewController {
        return rootVC
    }
    return UIApplication.shared.delegate?.window??.rootViewController
}

@Barba2k2 Barba2k2 force-pushed the fix/ios-rootviewcontroller-crash branch from ea5d282 to 5aee169 Compare December 26, 2025 15:26
@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant