iOS项目中集成 Flutter 模块 2021-06-20

2021-06-20  本文已影响0人  老P打码

环境

准备

flutter create --template module flutter_module
$flutter_path = '../flutter_module/'

target 'FlutterDemo' do
    flutter_application_path = $flutter_path
    load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
    install_all_flutter_pods(flutter_application_path)
end

页面跳转

AppDelegate.swift

import Flutter

lazy var flutterEngine: FlutterEngine = {
        // "main" 为对应的dart文件
        let engine: FlutterEngine = FlutterEngine(name: "main", project: nil)
        return engine
    }()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        flutterEngine.run(withEntrypoint: nil)        
        return true
    }

Native跳转Flutter页面

let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
let vc: FlutterViewController = FlutterViewController(engine: appDelegate.flutterEngine, nibName: nil, bundle: nil)
navigationController?.pushViewController(vc, animated: true)

使用CocoaPods引入远程仓库Flutter Module

解决方案:Flutter Module 生成 Framework

  1. 先打包Framework
flutter build ios-framework --output=./path
  1. 将导出的所有Framework都合并架构
/// 查看 framework 支持的架构
lipo -info Flutter.framework  
/// 合并架构
lipo -create Flutter-1.framework/Flutter Flutter-2.framework/Flutter -output Flutter.framework
  1. 创建私有库
pod lib create FlutterLib

4.修改podspec文件, 添加依赖flutter module打包生成所有framework

 s.ios.vendored_frameworks = 'ios_frameworks/App.framework',
  'ios_frameworks/Flutter.framework'
  1. 将第2步合并的所有framework移动到 第4步指定的目录中,如ios_frameworks目录下
  2. 在主app下通过引用私有库 FlutterLib 引入Flutter Module
上一篇 下一篇

猜你喜欢

热点阅读