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

相关推荐
2501_915106322 小时前
如何查看手机使用记录:Android和iOS设备全面指南
android·ios·智能手机·小程序·uni-app·iphone·webview
小蜜蜂嗡嗡6 小时前
flutter namespace问题
android·flutter
克喵的水银蛇6 小时前
Flutter 适配实战:屏幕适配 + 暗黑模式 + 多语言
前端·javascript·flutter
西西学代码7 小时前
flutter---自定义白噪音UI
flutter
肠胃炎7 小时前
Flutter ListView 组件及各种模式
flutter
sunly_7 小时前
Flutter:设备唯一id生成,存储,
flutter
0xAaron9 小时前
确定crash文件和dSYM是否对应
ios·uuid·crash·dsym
走在路上的菜鸟10 小时前
Android学Dart学习笔记第十节 循环
android·笔记·学习·flutter
0xAaron11 小时前
符号表和 dSYM UUID 确认
ios·cocoa·uuid·符号表·dsym
0xAaron11 小时前
如何使用dSYM文件来符号化崩溃信息
ios·swift·调试·崩溃·符号化·dsym