flutter 在OC工程中添加swift库报错的问题

下午看见群里有人问 swift 插件导入 oc 项目报错的问题, 我之前解决过, 但是忘了解决过程是怎么样的了, 这里记录下方便以后备查

没兴趣追踪原因的直接查看总结下章节

文章目录

    • 复现错误
    • 尝试解决
      • 总结下
    • 后记

复现错误

  1. 创建一个主工程(oc)
flutter create oc_project
  1. 创建一个插件工程(swift)
cd oc_project
flutter create -i swift -t plugin swift_plugin
  1. 关联工程
  swift_plugin:
    path: ./swift_plugin
    ```
  1. 运行 iOS 项目得到错误信息
Launching lib/main.dart on iPhone XS Max in debug mode...

CocoaPods' output:

↳

      Preparing

    Analyzing dependencies

    Inspecting targets to integrate

      Using `ARCHS` setting to build architectures of target `Pods-Runner`: (``)

    Fetching external sources

    -> Fetching podspec for `Flutter` from `.symlinks/flutter/ios`

    -> Fetching podspec for `swift_plugin` from `.symlinks/plugins/swift_plugin/ios`

    Resolving dependencies of `Podfile`

    Comparing resolved specification to the sandbox manifest

      A Flutter

      A swift_plugin

    Downloading dependencies

    -> Installing Flutter (1.0.0)

    -> Installing swift_plugin (0.0.1)

      - Running pre install hooks

    [!] Unable to determine Swift version for the following pods:

    - `swift_plugin` does not specify a Swift version and none of the targets (`Runner`) integrating it have the `SWIFT_VERSION` attribute set. Please contact the author or set the `SWIFT_VERSION` attribute in at least one of the targets that integrate this pod.

    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.1/lib/cocoapods/installer/xcode/target_validator.rb:115:in `verify_swift_pods_swift_version'

    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.1/lib/cocoapods/installer/xcode/target_validator.rb:37:in `validate!'

    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.1/lib/cocoapods/installer.rb:459:in `validate_targets'

    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.1/lib/cocoapods/installer.rb:138:in `install!'

    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.1/lib/cocoapods/command/install.rb:48:in `run'

    /Library/Ruby/Gems/2.3.0/gems/claide-1.0.2/lib/claide/command.rb:334:in `run'

    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.1/lib/cocoapods/command.rb:52:in `run'

    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.1/bin/pod:55:in `'

    /usr/local/bin/pod:22:in `load'

    /usr/local/bin/pod:22:in `
' Error output from CocoaPods: ↳ [!] `` attempted to initialize an object with an unknown UUID. `CF3B75C9A7D2FA2A4C99F110` for attribute: `children`. This can be the result of a merge and the unknown UUID is being discarded. [!] Automatically assigning platform `ios` with version `8.0` on target `Runner` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`. Error running pod install Error launching application on iPhone XS Max. Exited (sigterm)

错误信息大概是这样的

尝试解决

提示信息中让我们联系作者加入一个 SWIFT_VERSION 的鬼东西, 因为插件的作者是我们自己,所以直接改就行了

打开 swift_plugin/ios/swift_plugin.podspec 文件
接着查询下这个 podspec 的配置页面文档: https://guides.cocoapods.org/syntax/podspec.html

flutter 在OC工程中添加swift库报错的问题_第1张图片

看新闻最新的是 5.0, 我们就指定 5.0 吧, 在后面添加一行s.swift_version = '5.0', 接着运行, 不出意外的又报错了

果然没有这么顺利, 这次的报错信息是The “Swift Language Version” (SWIFT_VERSION) build setting must be set to a supported value for targets which use Swift. Supported values are: 3.0, 4.0, 4.2. This setting can be set in the build settings editor.

也就是说只支持这三个版本, 看到这里我想到的是两个解决方案:

  1. 升级 cocoapod 或查询文档看是否新版本支持 5.0+
  2. 查询当前 swift 文件的真实版本号

思考了几秒钟排除了 1, 因为 swift 可不是什么稳定的语言, 4.2 的语法 5.0 未必认,反之亦然

所以我们要查询当前 swift 的文件是用哪个版本编写的,以便于对号入座

用 xcode 打开 swift_plugin/example/ios, 就像这样
flutter 在OC工程中添加swift库报错的问题_第2张图片

如果打开后只有 Runner,那说明 pod 相关的文件夹没生成,如下图
flutter 在OC工程中添加swift库报错的问题_第3张图片

先关闭 xcode

然后我们需要在命令行这样做:

cd swift_plugin/example/ios;pod install

接着再 open in xcode
flutter 在OC工程中添加swift库报错的问题_第4张图片

见到 Pods 就说明对了
flutter 在OC工程中添加swift库报错的问题_第5张图片

接着查看 swift 语言

flutter 在OC工程中添加swift库报错的问题_第6张图片

往下翻,找到这个,我这里是 4
flutter 在OC工程中添加swift库报错的问题_第7张图片

设置给刚刚那个 swift_version: s.swift_version = '4.0'

接着运行项目, 嗯… 继续报错

这次的信息: The “Swift Language Version” (SWIFT_VERSION) build setting must be set to a supported value for targets which use Swift. Supported values are: 3.0, 4.0, 4.2. This setting can be set in the build settings editor.

这次倒是没让我们联系作者, 猜测一下,估计是主项目可能也需要设置, 查询文档: https://guides.cocoapods.org/syntax/podfile.html#supports_swift_versions

打开ios/Podfile文件

target 'Runner' do
supports_swift_versions '>=4.0', '<5.0'` # add

接着运行项目
Invalid Podfile file: undefined method `supports_swift_versions’

可能是我 cocoapod 版本太低

我升级一下: sudo gem install cocoapods

pod --version
1.7.1

接着运行项目, 这次又有另一个新花招
fatal error: 'swift_plugin/swift_plugin-Swift.h' file not found

告诉我, swift.h 没找到, 这个是 swift 在编译过程中自动生成的头文件

如何修改呢, 还是刚刚主工程的 Podfile 文件

在开头所有注释的后面加入一行: use_frameworks!

这个是使用 framework 代替静态库, pod 的官方文档说明: https://guides.cocoapods.org/syntax/podfile.html#use_frameworks_bang

再运行, 终于成功了!

然后删除掉supports_swift_versions,再运行,我这里没发现问题,所以这一步其实是可以忽略的

总结下

  1. 联系作者添加 swift_version 的版本号(通常是 4.0),
    1. 如果是自己的项目可以查看 xcode 获得
    2. 别人的项目
      1. 如果作者添加,皆大欢喜
      2. 作者不添加
        1. 尝试 PR
        2. 自己 fork 或 download 后魔改,使用 git 依赖或者 path 依赖
  2. 升级 cocoapod 的版本(我这里是 1.7.1)
  3. 在项目 podspec 中加入use_frameworks!

后记

本篇解决了 oc 项目使用 swift 插件的问题, 分享出来方便你我他

example 项目地址: https://github.com/CaiJingLong/flutter-ocproject-use-swift-plugin

以上

你可能感兴趣的:(flutter,ios)