Flutter 添加Flutter到已有的iOS项目

Flutter环境配置
CocoaPods安装

创建Flutter模块

创建iOS工程native_flutter_demo,在它的上一层目录创建flutter模块:

$ flutter create -t module flutter_module

如图所示:


创建Podfile

进入到你的iOS工程目录:

$ pod init

vim Podfile编辑 Podfile,在文件末尾添加:

注意: Flutter不同版本,Podfile是有差别的

查看flutter版本:

$ flutter --version
Flutter 1.10.15-pre.384 • channel master • https://github.com/flutter/flutter.git
Framework • revision ee032f67c7 (5 days ago) • 2019-11-02 09:46:25 -0700
Engine • revision 8ea19b1c76
Tools • Dart 2.6.0 (build 2.6.0-dev.8.2 bbe2ac28c9)

Flutter 1.8.4-pre.21之前:

target 'native_flutter_demo' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for native_flutter_demo

  target 'native_flutter_demoTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'native_flutter_demoUITests' do
    inherit! :search_paths
    # Pods for testing
  end

end

flutter_application_path = '../flutter_module'
eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)

为编译Dart 代码添加build phase

打开iOS项目,选中TARGETnative_flutter_demo项目的Build Phases选项,点击左上角+号按钮,选择New Run Script Phase,将下面的shell脚本添加到输入框中:

"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed

如下图所示:


Flutter 1.8.4-pre.21之后:

flutter_application_path = '../flutter_module'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

target 'native_flutter_demo' 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 FCPublicOpinionSystem

  target 'native_flutter_demoTests' do
    inherit! :search_paths
    # Pods for testing
    install_all_flutter_pods(flutter_application_path)
  end

  target 'native_flutter_demoUITests' do
    inherit! :search_paths
    # Pods for testing
    install_all_flutter_pods(flutter_application_path)
  end

end

其中flutter_application_path代表你的flutter模块路径。
运行

pod install

然后需要编译一下项目:Command + B

参考文章:

Add-Flutter-to-existing-apps
Upgrading-Flutter-added-to-existing-iOS-Xcode-project

你可能感兴趣的:(Flutter 添加Flutter到已有的iOS项目)