Flutter module 打包集成到iOS项目

方式一 使用 CocoaPods

  1. Podfile 中添加下面代码:
    Add the following lines to your Podfile:
content_copy
flutter_application_path = '../my_flutter'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

2.每个需要集成 Flutter 的 [Podfile target][],执行 install_all_flutter_pods(flutter_application_path)

target 'MyApp' do
 install_all_flutter_pods(flutter_application_path)
end
  1. 运行 pod install。

注意:当你在 my_flutter/pubspec.yaml 改变了 Flutter plugin 依赖,需要在 Flutter module 目录运行 flutter pub get,来更新会被podhelper.rb 脚本用到的 plugin 列表,然后再次在你的应用目录 some/path/MyApp 运行 pod install.

方式二 在 Xcode 中集成 frameworks

  1. 下面的示例假设你想在 some/path/MyApp/Flutter/ 目录下创建 frameworks:
flutter build ios-framework --output=some/path/MyApp/Flutter/
some/path/MyApp/
└── Flutter/
    ├── Debug/
    │   ├── Flutter.xcframework
    │   ├── App.xcframework
    │   ├── FlutterPluginRegistrant.xcframework (only if you have plugins with iOS platform code)
    │   └── example_plugin.xcframework (each plugin is a separate framework)
    ├── Profile/
    │   ├── Flutter.xcframework
    │   ├── App.xcframework
    │   ├── FlutterPluginRegistrant.xcframework
    │   └── example_plugin.xcframework
    └── Release/
        ├── Flutter.xcframework
        ├── App.xcframework
        ├── FlutterPluginRegistrant.xcframework
        └── example_plugin.xcframework

注意 始终使用相同目录下的 Flutter.framework 和 App.framework。混合使用不同目录(例如 Profile/Flutter.framework 以及 Debug/App.framework)将会导致运行失败。

  1. 在 Xcode 中将生成的 frameworks 集成到你的既有应用中。例如,你可以在 some/path/MyApp/Flutter/Release/ 目录拖拽 frameworks 到你的应用 target 编译设置的 General > Frameworks, Libraries, and Embedded Content 下,然后在 Embed 下拉列表中选择 “Embed & Sign”。

例如,你可以将框架从 Finder 的 some/path/MyApp/Flutter/Release/ 拖到你的目标项目中,然后点击以下步骤 build settings > Build Phases > Link Binary With Libraries。

在 target 的编译设置中的 Framework Search Paths (FRAMEWORK_SEARCH_PATHS) 增加 $(PROJECT_DIR)/Flutter/Release/。


image

提示:

在 Xcode 11 中,你可以添加 --xcframework --no-universal 参数来生成 XCFrameworks,而不是使用通用的 framework。

  1. 内嵌框架
    生成的动态框架必须嵌入你的应用并且在运行时被加载。
    插件会帮助你生成 静态或动态框架。静态框架应该直接链接而不是嵌入。如果你在应用中嵌入了静态框架,你的应用将不能发布到 App Store 并且会得到一个 Found an unexpected Mach-O header code 的 archive error。
    例如,你可以从应用框架组中拖拽框架(除了 FlutterPluginRegistrant 以及其他的静态框架)到你的目标 ‘ build settings > Build Phases > Embed Frameworks。然后从下拉菜单中选择 “Embed & Sign”。
    image

    你现在可以在 Xcode中使用 ⌘B 编译项目
    提示:
    如果你想在 Debug 编译配置下使用 Debug 版本的 Flutter frameworks,在 Release 编译配置下使用 Release 版本的 Flutter frameworks,在 MyApp.xcodeproj/project.pbxproj 文件中,尝试在所有 Flutter 相关 frameworks 上使用 path = "Flutter/$(CONFIGURATION)/example.framework"; 替换 path = Flutter/Release/example.framework; (注意添加引号 ")。

你也必须在 Framework Search Paths (FRAMEWORK_SEARCH_PATHS) 编译设置中使用 (CONFIGURATION)。

https://flutter.cn/docs/development/add-to-app/ios/project-setup#option-a---embed-with-cocoapods-and-the-flutter-sdk

你可能感兴趣的:(Flutter module 打包集成到iOS项目)