Flutter与iOS原生混合开发 iOS项目集成Flutter

1.创建flutter module

进入iOS工程根目录的上一级,创建flutter module工程

复制代码
flutter create --template module fluttermodule

2.进入iOS工程根目录,编辑podfile文件

复制代码
flutter_application_path = '../fluttermodule'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

target "iOSDemo" do

use_frameworks!
install_all_flutter_pods(flutter_application_path)

end

post_install do |installer|
  flutter_post_install(installer) if defined?(flutter_post_install)
end

执行pod install

3.iOS调用Flutter

在AppDelegate注册FlutterEngine

复制代码
import UIKit
import Flutter
import FlutterPluginRegistrant

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    lazy var flutterEngine = FlutterEngine(name: "my flutter engine")
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        GeneratedPluginRegistrant.register(with: self.flutterEngine);
        flutterEngine.run()
        return true
    }
}

使用 FlutterEngine调用utterViewController

复制代码
import UIKit
import Flutter

class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()

    // Make a button to call the showFlutter function when pressed.
    let button = UIButton(type:UIButton.ButtonType.custom)
    button.addTarget(self, action: #selector(showFlutter), for: .touchUpInside)
    button.setTitle("Show Flutter!", for: UIControl.State.normal)
    button.frame = CGRect(x: 80.0, y: 210.0, width: 160.0, height: 40.0)
    button.backgroundColor = UIColor.blue
    self.view.addSubview(button)
  }

  @objc func showFlutter() {
    let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
    let flutterViewController =
        FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
    present(flutterViewController, animated: true, completion: nil)
  }
}

参考链接
https://www.jianshu.com/p/aac1f387f3c0
https://www.jianshu.com/p/c3e5e210a585

相关推荐
风华圆舞8 分钟前
鸿蒙 + Flutter 下如何管理 AI 会话——AgentService 设计解析
人工智能·flutter·harmonyos
Legendary_0082 小时前
LDR6020P:iPad 一体式皮套键盘 OTG 应用的核心引擎
ios·计算机外设·ipad
spmcor16 小时前
Flutter 学习笔记 (3):布局初探 —— Row、Column、Stack 与 Container
flutter
风华圆舞17 小时前
DevEco Studio 和 Flutter 工具链如何协同工作
flutter·华为·架构·harmonyos
Digitally17 小时前
如何高效地将文件从电脑传输到 iPad:6 种简单方法
ios·电脑·ipad
朱莉^_^JuneLee17 小时前
Flutter 性能优化实战:用 ConsumerWidget + select 做到真正的局部刷新
flutter
G_dou_20 小时前
Flutter三方库适配OpenHarmony【palindrome_checker】回文检测器项目完整实战
flutter·harmonyos
朱莉^_^JuneLee21 小时前
Flutter 模块化架构实战:用 Barrel Export 管控模块边界
flutter·架构
萤萤七悬21 小时前
【Python笔记】AI帮封装Airtest IOS-WDA touch操作时的factor坐标转换
笔记·python·ios