Flutter iOS: Fix Application Crash on Second Launch (iOS 14+)
【代码】Flutter iOS: Fix Application Crash on Second Launch (iOS 14+)
🔍 Problem Description
When running a Flutter app on an iOS 14+ physical device, the application crashes immediately upon the second launch.
-
First Launch: App works perfectly when started from Xcode or
flutter run. -
Termination: User manually terminates the app (swipes up from the app switcher).
-
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, setUIApplicationSupportsStateRestorationtoNO. -
Ensure Initialization: In
main.dart, callWidgetsFlutterBinding.ensureInitialized()beforerunApp(). -
Ignore Snapshots: In
AppDelegate.swift, callapplication.ignoreSnapshotOnNextApplicationLaunch()to ensure a fresh UI state after a crash or manual kill.
更多推荐



所有评论(0)