Skip to content

fix(ios): resolve UIApplication delegate window nil crash on iOS 13+ SceneDelegate lifecycles#362

Open
jvallori wants to merge 2 commits intolukasgit:masterfrom
jvallori:fix/ios-13-root-scene-window-crash
Open

fix(ios): resolve UIApplication delegate window nil crash on iOS 13+ SceneDelegate lifecycles#362
jvallori wants to merge 2 commits intolukasgit:masterfrom
jvallori:fix/ios-13-root-scene-window-crash

Conversation

@jvallori
Copy link
Copy Markdown

What does this PR do?

Fixes a severe native iOS crash (Unexpectedly found nil while unwrapping an Optional value) that occurs immediately upon initialization when the app utilizes iOS 13+ UIScene mechanics (which is increasingly standard in modern Flutter ecosystem templates).

The Bug

In SwiftContactsServicePlugin.swift, the rootViewController was being rigidly extracted using the legacy application lifecycle model via a forced unwrap:
UIApplication.shared.delegate!.window!!.rootViewController!
Because modern iOS templates leverage UISceneDelegate for multi-window support rather than the legacy AppDelegate, delegate.window correctly evaluates to nil, instantly crashing the application.

The Fix

This PR introduces a backward-compatible resolution that dynamically scans UIApplication.shared.connectedScenes (available on iOS 13.0+) to securely locate the active keyWindow and binding to its root view controller natively. It safely falls back to the legacy UIApplication.shared.delegate if the device is running iOS 12 or if scenes are unsupported.

Testing

  • Tested in a modern Flutter application incorporating UIScene lifecycle patterns cleanly.
  • Resolved fatal startup crashes upon plugin initialization gracefully.

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