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

相关推荐
icc_tips5 小时前
Flutter Async 与 Async*
flutter
张风捷特烈6 小时前
每日一题 Flutter#7,8 | 关于 State 两道简答题
android·flutter·面试
pop_xiaoli14 小时前
OC—UI学习-2
学习·ui·ios
90后的晨仔17 小时前
git 命令汇总
ios
liucan23320 小时前
JS执行速度似乎并不比Swift或者C语言慢
前端·ios
方文_1 天前
flutter~loading效果
flutter
唯有选择1 天前
让你的应用界面好看的基石:Flutter主题Theme使用和扩展自定义字段
前端·flutter
安和昂1 天前
【iOS】 Block再学习
学习·ios·cocoa
pop_xiaoli1 天前
OC学习—命名规范
学习·ios
sunly_1 天前
Flutter:导航固定背景图,滚动时导航颜色渐变
android·javascript·flutter