Skip to content

iOS crash on launch with Flutter 3.41+ UIScene lifecycle — force-unwrap on nil window in plugin registration #46

@seanhamstra

Description

@seanhamstra

Describe the problem

Both cometchat_chat_uikit and cometchat_uikit_shared crash immediately on app launch when built with Flutter 3.41+, which auto-migrates iOS apps to the UIScene lifecycle.

The root cause is a force-unwrap (?!) on a nil window property during plugin registration:

SwiftCometchatChatUikitPlugin.swift:32:

let viewController = UIApplication.shared.delegate?.window?!.rootViewController

CometchatUikitSharedPlugin.swift:46:

let viewController = UIApplication.shared.delegate?.window?!.rootViewController

Under the UIScene lifecycle, UIApplication.shared.delegate?.window is nil during plugin registration because the window is now owned by UIWindowScene, not the AppDelegate. The ?! operator force-unwraps nil and causes a brk 1 (EXC_BREAKPOINT / SIGTRAP) crash.

What was the expected behavior?

The app should launch without crashing. The viewController can safely be nil at registration time — replacing ?! with ?? fixes the issue and the app runs normally.

Reproduction

  • Step 1: Create a Flutter app using cometchat_chat_uikit: ^5.2.10
  • Step 2: Build with Flutter 3.41+ (flutter build ipa --release) — this auto-migrates to UIScene lifecycle
  • Step 3: Install on device via TestFlight or Ad Hoc
  • Step 4: App crashes immediately on launch before any Dart code runs

Alternatively, to reproduce locally:

  • Step 1: Add the UIScene manifest to ios/Runner/Info.plist:
<key>UIApplicationSceneManifest</key>
<dict>
    <key>UIApplicationSupportsMultipleScenes</key>
    <false/>
    <key>UISceneConfigurations</key>
    <dict>
        <key>UIWindowSceneSessionRoleApplication</key>
        <array>
            <dict>
                <key>UISceneClassName</key>
                <string>UIWindowScene</string>
                <key>UISceneDelegateClassName</key>
                <string>FlutterSceneDelegate</string>
                <key>UISceneConfigurationName</key>
                <string>flutter</string>
                <key>UISceneStoryboardFile</key>
                <string>Main</string>
            </dict>
        </array>
    </dict>
</dict>
  • Step 2: Run flutter run
  • Step 3: App crashes on launch

This issue is 100% reproducible.

Suggested fix — change ?! to ?? in both files:

let viewController = UIApplication.shared.delegate?.window??.rootViewController

Tested and confirmed working.

Crash log (truncated):

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x00000001008b8564

Thread 0 Crashed:
0   cometchat_chat_uikit   0x00000001008b8564
1   cometchat_chat_uikit   0x00000001008b80ac
2   Runner                 0x00000001004780b8
3   Runner                 0x0000000100478604
4   Flutter                FlutterEngine performImplicitEngineCallback

Environment

  • Version used: cometchat_chat_uikit 5.2.10, cometchat_uikit_shared 5.2.0, cometchat_sdk 4.1.0
  • Flutter: 3.41.2 (stable)
  • iOS: 18.5, iPhone 17 Pro Max
  • Other modules/plugins/libraries that might be involved: Flutter's UIScene auto-migration (see Flutter docs). Apple will require UIScene lifecycle for all UIKit apps in the release following iOS 26.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions