Flutter混编-iOS集成

Flutter可以作为嵌入式框架添加到现有的iOS项目中。

环境要求

Flutter 支持iOS 8.0或以上,开发环境必须满足以下条件:

  1. Xcode installed
  2. Operating Systems,macOS (64-bit) ;
  3. Disk Space,2.8 GB ;
  4. Tools,bash、rm、unzip、which … ;

创建Flutter module

用以下命令可以快速创建module, command line 运行:

cd some/path/
flutter create --template module my_flutter

集成modlue到iOS项目

有两种方式去集成到项目。

  1. 通过CocoaPods依赖管理和安装Flutter SDK;(官方推荐)
  2. 创建frameworks包含Dart代码的Flutter engine和 Flutter 插件代码,在Xcode 的 build settings 中更新frameworks即可。

方案A-通过CocoaPods和SDK集成

此方案要求在你的项目上工作的每个开发人员都必须在本地安装了Flutter SDK。 只需在Xcode中构建应用程序即可自动运行脚本以嵌入Dart和插件代码。 这允许使用Flutter模块的最新版本进行快速迭代,而无需在Xcode之外运行其他命令。项目结构如下:
在这里插入图片描述
在这里插入图片描述

如果新建的项目没有Podfile文件,先根据文章 Adding Pods to an Xcode project 去添加一个Podfile。

  1. 在iOS项目(MyApp)中的Podfile文件中加入
flutter_application_path = '../my_flutter'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
  1. 在Podfile target 需要集成flutter,install_all_flutter_pods(flutter_application_path)
target 'MyApp' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!
  install_all_flutter_pods(flutter_application_path)
  # Pods for MyApp
end
  1. 最后运行 pod install 就可以通过CocoaPods集成flutter代码到项目中

方案B1-通过frameworks在Xcode集成

通过flutter命令生产必要的frameworks,并通过手动嵌入到现在的Xcode项目中。这个方案不需要你的团队开发成员本地安装Flutter SDK和CocoaPods,但是在每次在module进行代码更改时,都要运行 flutter build ios-framework 更新frameworks

  1. 执行生成framework命令
  cd my_module
  flutter build ios-framework --output=../MyApp/Flutter/

在这里插入图片描述
在这里插入图片描述

  1. 在Xcode中链接frameworks

Build Setting->Build Phases->Link Binary With Libraries->+>AddOther->Add Files,添加Release下的所有framework文件
在这里插入图片描述
3. 最后在framework下拉列表中选择Embed&Sign,这样就完成frameworks集成

方案B2-在B1的基础上使用CocoaPods集成Flutter.framework

这个方案就是避免把体积较大的Flutter.framework(大概100MB)的拷贝给其他开发人员,减轻持续集成的成本负担,而是通过cocoapods加载远程仓库的包。

  1. 通过以下命令生成 Flutter.podspec
flutter build ios-framework --cocoapods --output=some/path/MyApp/Flutter/

在这里插入图片描述

#Flutter.podspec 文件内容
Pod::Spec.new do |s|
  s.name                  = 'Flutter'
  s.version               = '1.20.400' # 1.20.4
  s.summary               = 'Flutter Engine Framework'
  s.description           = <<-DESC desc DESC
  s.homepage              = 'https://flutter.dev'
  s.license               = {
    
     :type => 'MIT', :text => <<-LICENSE license LICENSE
  }
  s.author                = {
    
     'Flutter Dev Team' => '[email protected]' }
  s.source                = {
    
     :http => 'https://storage.flutter-io.cn/flutter_infra/flutter/d1bc06f032f9d6c148ea6b96b48261d6f545004f/ios/artifacts.zip' }
  s.documentation_url     = 'https://flutter.dev/docs'
  s.platform              = :ios, '8.0'
  s.vendored_frameworks   = 'Flutter.framework'
  s.prepare_command       = <<-CMD
unzip Flutter.framework -d Flutter.framework
CMD
end
  1. 添加Flutter到项目的Podfile
pod 'Flutter', :podspec => 'some/path/MyApp/Flutter/[build mode]/Flutter.podspec'
  1. App.framework, FlutterPluginRegistrant.framework按照方案B1的方式去集成

方案B3-最终方案

既然Flutter.framework可通过远程仓库的方式添加,App.framework, FlutterPluginRegistrant.framework也就可以通过发布到远程仓库,方便其他开发人员持续集成功能。
CocoaPods远程管理简单步骤如下,详细步骤可参考文章 Cocoapods的使用

  1. 在github创建项目
  2. 初始化好项目(把framework文件push上去)
  3. 到项目root目录下, 创建 spec 文件
cd 项目
pod spec create 名称
  1. 编辑 spec 文件
Pod::Spec.new do |s|
 s.name             = 'FlutterIosFramework'
 #tag版本号
 s.version          = '0.0.7'
 s.summary          = 'High-performance, high-fidelity mobile apps.'
 s.description      = <<-DESC
Flutter provides an easy and productive way to build and deploy high-performance mobile apps for Android and iOS.
                      DESC
#自己填写git地址
 s.homepage         = 'http://xxxx/FlutterIosFramework'
 s.license          = {
    
     :type => 'MIT' }
 s.author           = {
    
     'Flutter Dev Team' => '[email protected]' }
 #自己填写git地址
 s.source           = {
    
     :git => 'http://gitlab.admin.bluemoon.com.cn/xxx/FlutterIosFramework', :tag => s.version.to_s }
 s.ios.deployment_target = '9.0'
 #s.xcconfig = {
    
     'VALID_ARCHS' => 'arm64' }
 s.pod_target_xcconfig = {
    
    'VALID_ARCHS' => 'armv7 arm64 x86_64' }
 #这个是找到对应framework的路径,请按照我的填写来找到对应你自己的填写
 s.vendored_frameworks = '*.framework'
end
  1. 新建tag且提交项目
git add .
git commit -m '0.1.2'
git tag 0.1.2
git push --tags
git push origin master
  1. 验证一下 podspec 文件
pod lib lint --allow-warnings
  1. 如果是第一次提交需验证一下,不是第一次可以不用此步
pod trunk register ios@bluemoon.com 'eric' --description=‘我是描述’
  1. 提交给 cocoapods 管理
pod trunk push FlutterIosFramework.podspec --allow-warnings --verbose
  1. 如果成功,可能需要等几分钟才可以 search 到你的 lib, 可去cocoapods查看
  2. 在IOS项目在Podfile文件添加远程仓库链接,最后执行 pod install --repo-update 即可
target 'MyApp' do
    use_frameworks!
    pod 'FlutterIosFramework', '~> 0.1.2'
    pod 'Flutter', :podspec => '../Flutter/Flutter.podspec'
# Pods for MyApp
end

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/LIANGJIANGLI/article/details/109511112