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

相关推荐
Ranye1232 小时前
从 JS 到 Dart:语法基础
javascript·flutter·dart
安和昂5 小时前
【iOS】Blocks
macos·ios·cocoa
我要最优解9 小时前
关于在mac中配置Java系统环境变量
java·flutter·macos
OKXLIN21 小时前
IOS UITextField 无法隐藏键盘问题
ios·objective-c
AL.千灯学长1 天前
DeepSeek接入Siri(已升级支持苹果手表)完整版硅基流动DeepSeek-R1部署
人工智能·gpt·ios·ai·苹果vision pro
江上清风山间明月2 天前
Flutter开发的应用页面非常多时如何高效管理路由
android·flutter·路由·页面管理·routes·ongenerateroute
openinstall全渠道统计2 天前
免填邀请码工具:赋能六大核心场景,重构App增长新模型
android·ios·harmonyos
Zsnoin能2 天前
flutter国际化、主题配置、视频播放器UI、扫码功能、水波纹问题
flutter
早起的年轻人2 天前
Flutter CupertinoNavigationBar iOS 风格导航栏的组件
flutter·ios
HappyAcmen2 天前
关于Flutter前端面试题及其答案解析
前端·flutter