Flutter是否支持同时使用SceneDelegate与Siri Shortcut?MethodChannel报错如何解决?
问题根因
- 引入
SceneDelegate后,iOS 13+系统会将UI生命周期、Flutter引擎的绑定逻辑从AppDelegate转移到SceneDelegate中处理,你此前在AppDelegate中注册的MethodChannel没有关联到当前活跃的Scene对应的Flutter引擎实例,导致Flutter端调用Channel时找不到原生侧的实现,抛出MissingPluginException。 - 部分Flutter插件默认会依赖AppDelegate的回调完成初始化,新增SceneDelegate后如果没有正确配置Flutter引擎的共享关系,也会出现Channel注册失效的问题。
解决方案
步骤1:将MethodChannel注册逻辑转移到SceneDelegate中
在SceneDelegate的willConnectToSession方法中完成Channel的初始化和注册,确保和当前Scene的FlutterViewController绑定:
Swift示例代码:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = (scene as? UIWindowScene) else { return } let window = UIWindow(windowScene: windowScene) let flutterViewController = FlutterViewController.init(engine: FlutterEngineGroup.shared.makeEngine(withEntrypoint: nil, libraryURI: nil), nibName: nil, bundle: nil) // 注册MethodChannel let channel = FlutterMethodChannel(name: "sample/aaa", binaryMessenger: flutterViewController.binaryMessenger) channel.setMethodCallHandler { call, result in if call.method == "sample/aaa" { // 原有业务逻辑 result("success") } else { result(FlutterMethodNotImplemented) } } window.rootViewController = flutterViewController self.window = window window.makeKeyAndVisible() }若使用Objective-C开发,逻辑完全一致,仅需将Channel绑定到当前Scene对应的FlutterViewController的
binaryMessenger属性即可。
步骤2:清理AppDelegate冗余逻辑
保留AppDelegate中仅和应用全局生命周期相关的逻辑,移除原有MethodChannel注册代码,同时确保AppDelegate遵守FlutterAppDelegate协议,没有重写系统默认的Scene相关回调导致绑定失效。
步骤3:校验Info.plist配置
确认Info.plist中Application Scene Manifest配置正确,避免多余配置导致Flutter引擎实例重复创建:
- 如果应用不需要多窗口支持,将
Enable Multiple Windows设置为NO - 确认
Scene Configuration下的Delegate Class Name指向你自定义的SceneDelegate类
内容的提问来源于stack exchange,提问作者Ryoyoshi
相关产品推荐
相关产品推荐

