iOS原生嵌入Flutter Module长期后台切前台白屏问题求助
问题描述
我们在Objective-C开发的iOS 13+项目中,通过Xcode插件框架+CocoaPods集成Flutter Module,采用FlutterAppDelegate子类初始化FlutterEngine,再用该引擎创建FlutterViewController推入导航栈,日常运行正常。
遇到的异常:
- App长期处于后台后切至前台,Flutter视图消失,仅显示白屏且无法恢复,只能强制杀App解决
- 该问题仅在测试包中偶现,模拟器无法复现
已尝试的无效方案:
- 通过Flutter Channel在前台时刷新Flutter视图
- 移除并重新初始化
FlutterViewController与FlutterEngine
技术栈:iOS 13+,Xcode 14.2,Flutter 3.3.10,Dart 2.18.6
排查与解决方案
1. 检查并重启FlutterEngine
iOS后台内存紧张时,系统可能会暂停Flutter Engine。在AppDelegate的前台恢复方法中检查引擎状态,必要时重启:
- (void)applicationWillEnterForeground:(UIApplication *)application { if (!self.flutterEngine.isRunning) { [self.flutterEngine runWithEntrypoint:nil]; } }
注意:必须复用全局持有的引擎实例,不要重复创建新引擎。
2. 监听FlutterViewController视图生命周期
自定义FlutterViewController子类,在viewWillAppear:中强制触发UI重建:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // 触发原生视图层重绘 [self.view setNeedsDisplay]; // 通过MethodChannel通知Dart侧刷新页面 [self.methodChannel invokeMethod:@"forceRefresh" arguments:nil]; }
Dart侧对应处理:
final methodChannel = MethodChannel('your_channel_name'); @override void initState() { super.initState(); methodChannel.setMethodCallHandler((call) async { if (call.method == 'forceRefresh') { setState(() {}); } }); }
3. 确保FlutterEngine全局单例持有
避免引擎被局部变量持有导致提前释放,在AppDelegate中声明全局强引用:
@property (nonatomic, strong) FlutterEngine *flutterEngine; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.flutterEngine = [[FlutterEngine alloc] initWithName:@"global_flutter_engine"]; [self.flutterEngine runWithEntrypoint:nil]; return YES; }
4. 处理iOS后台快照冲突
iOS后台生成的快照可能在前台恢复时覆盖Flutter视图,可通过显式控制视图显隐规避:
- (void)applicationDidEnterBackground:(UIApplication *)application { if (self.currentFlutterVC) { self.currentFlutterVC.view.hidden = YES; } } - (void)applicationWillEnterForeground:(UIApplication *)application { if (self.currentFlutterVC) { self.currentFlutterVC.view.hidden = NO; [self.currentFlutterVC.view setNeedsLayout]; } }
5. 升级Flutter版本
Flutter 3.3.10存在部分后台恢复相关的已知bug,建议升级到3.10+稳定版本,官方后续版本修复了大量引擎生命周期异常问题。
内容的提问来源于stack exchange,提问作者Rahul Kumar
相关产品推荐
相关产品推荐

