下午看见群里有人问 swift 插件导入 oc 项目报错的问题, 我之前解决过, 但是忘了解决过程是怎么样的了, 这里记录下方便以后备查
没兴趣追踪原因的直接查看总结下章节
flutter create oc_project
cd oc_project
flutter create -i swift -t plugin swift_plugin
swift_plugin:
path: ./swift_plugin
```
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
看新闻最新的是 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, 因为 swift 可不是什么稳定的语言, 4.2 的语法 5.0 未必认,反之亦然
所以我们要查询当前 swift 的文件是用哪个版本编写的,以便于对号入座
用 xcode 打开 swift_plugin/example/ios, 就像这样
如果打开后只有 Runner,那说明 pod 相关的文件夹没生成,如下图
先关闭 xcode
然后我们需要在命令行这样做:
cd swift_plugin/example/ios;pod install
接着查看 swift 语言
设置给刚刚那个 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
,再运行,我这里没发现问题,所以这一步其实是可以忽略的
use_frameworks!
本篇解决了 oc 项目使用 swift 插件的问题, 分享出来方便你我他
example 项目地址: https://github.com/CaiJingLong/flutter-ocproject-use-swift-plugin
以上