You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter与iOS原生混合App二次跳转失败:MissingPluginException问题排查

问题根源

MissingPluginException 是因为第二次回到Flutter后,MethodChannel的Native端处理逻辑没有和当前Flutter环境绑定。你目前的实现大概率是把通道注册在初始的FlutterViewController生命周期中,当切换到原生页面后,原FlutterViewController被销毁,通道关联失效;再次创建FlutterViewController时,没有重新注册通道处理方法,导致Flutter调用startNativeApp时找不到对应实现。

解决方案

1. 将MethodChannel注册绑定到全局FlutterEngine

把通道注册逻辑放在AppDelegate的FlutterEngine初始化阶段,确保全局唯一的FlutterEngine始终持有通道处理逻辑,不管创建多少个FlutterViewController,通道都能正常响应。

修改AppDelegate代码:

import Flutter

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    // 全局持久化的Flutter引擎
    let flutterEngine = FlutterEngine(name: "persistent_flutter_engine")

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // 预启动Flutter引擎,避免冷启动延迟
        flutterEngine.run()
        
        // 注册MethodChannel并绑定处理逻辑
        let nativeChannel = FlutterMethodChannel(name: "nativeChannel", binaryMessenger: flutterEngine.binaryMessenger)
        nativeChannel.setMethodCallHandler { [weak self] call, result in
            switch call.method {
            case "startNativeApp":
                // 执行原生页面跳转
                self?.presentLauncherViewController()
                result.success(nil)
            default:
                result.notImplemented()
            }
        }
        
        return true
    }

    // 你的原生页面跳转方法
    func presentLauncherViewController() {
        if let launcherViewController = LaunchVC.instantiateFromStoryboard() {
            launcherViewController.modalPresentationStyle = .fullScreen
            if let window = self.window {
                window.rootViewController = launcherViewController as! UINavigationController
                window.makeKeyAndVisible()
            }
        }
    }
}

2. 跳转到Flutter时复用全局引擎

保持你原有的跳转代码不变即可,因为已经在使用AppDelegate中的全局flutterEngine创建FlutterViewController,新的ViewController会复用已注册通道的引擎:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let flutterEngine = appDelegate.flutterEngine
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)

flutterViewController.modalPresentationStyle = .fullScreen
if let window = appDelegate.window {
    window.rootViewController = flutterViewController
    window.makeKeyAndVisible()
}

3. 禁止重复创建FlutterEngine

不要每次跳转Flutter都新建引擎,复用全局的flutterEngine,既能保持通道有效性,还能提升Flutter页面的启动速度。

验证流程
  1. 应用启动进入Flutter页面,此时全局引擎已注册通道,调用startNativeApp可正常跳转原生页面。
  2. 从原生跳转回Flutter时,复用全局引擎创建ViewController,通道依然有效,Flutter再次调用startNativeApp时,Native端能正常响应并执行跳转。

内容的提问来源于stack exchange,提问作者Devenom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 00:07:41