🔍 Problem Description

When running a Flutter app on an iOS 14+ physical device, the application crashes immediately upon the second launch.

  1. First Launch: App works perfectly when started from Xcode or flutter run.

  2. Termination: User manually terminates the app (swipes up from the app switcher).

  3. Second Launch: App crashes instantly when tapping the icon on the home screen.


💡 Root Cause Analysis

  • Debug Mode Limitation: Flutter’s Debug Mode uses Just-In-Time (JIT) compilation. On iOS 14+, Debug Mode apps require an active connection to the Flutter debugger (Dart VM) to function. Once the process is killed and restarted without a debugger attached, the app fails to initialize the JIT environment, leading to a crash (often a SIGSEGV).

  • Expected Behavior: This is a known limitation by the Flutter team and Apple’s security policy. Debug builds are not intended to run independently from the host computer.


🛠 Solution: Release Mode Testing

To verify if your app functions correctly when restarted, you must use Release Mode. Release builds use Ahead-of-Time (AOT) compilation and do not require a debugger.

Step 1: Deep Environment Clean

Before building for Release, clear old build artifacts and broken metadata:

Bash

# Clean Flutter build cache
flutter clean
flutter pub get

# Reinstall Native Dependencies
cd ios
rm -rf Pods Podfile.lock
pod install --repo-update
cd ..
Step 2: Configure Deployment Target

Ensure your ios/Podfile platform is set to 13.0 or higher to support modern plugins like webview_flutter:

Ruby

# Uncomment and update this line in your Podfile
platform :ios, '13.0'
Step 3: Run in Release Mode

Install the app using the Release configuration. You can then disconnect the cable and test the "Second Launch" safely:

Bash

flutter run --release

✅ Best Practices Checklist

To prevent other native-level crashes (like the SIGSEGV found in your logs), ensure these three configurations are in place:

  • Disable State Restoration: Prevent iOS from trying to restore a dead WebView process. In Info.plist, set UIApplicationSupportsStateRestoration to NO.
  • Ensure Initialization: In main.dart, call WidgetsFlutterBinding.ensureInitialized() before runApp().

  • Ignore Snapshots: In AppDelegate.swift, call application.ignoreSnapshotOnNextApplicationLaunch() to ensure a fresh UI state after a crash or manual kill.

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐