uts-ios平台 使用CocoaPods

2024-08-10  本文已影响0人  Hason

在 uts 插件中使用 CocoaPods 依赖

在 uts 插件中使用 CocoaPods 依赖库,需要在 config.json 中 dependencies-pods 节点做相应配置,下面是配置示例:

{
    "deploymentTarget": "9.0",   // 可选,插件支持的最低 iOS 版本  默认:9.0"
    "dependencies-pods": [ // 可选, 需要依赖的 CocoaPods 库, HBuilderX 3.8.5+ 版本支持
    {
        "name": "WechatOpenSDK",
        "version": "2.0.2"
    }, {
        "name": "Alamofire",
        "version": "5.7.3", //配置 repo 时可不指定 version
        "repo": {  // 可选,配置 pod 库为指定仓库,配置repo时,应至少指定 git。 (HBuilder X 3.8.10+ 版本新增支持)
            "git": "https://github.com/test/Alamofire.git", //指定 pod 库的仓库地址
            "tag": "5.7.3", // 指定该 pod 库仓库的 tag
            "branch": "dev", // 指定该 pod 库仓库的分支
            "commit": "b309714f3aa5091f1ce8d932094b7594ed7acad9" // 指定该 pod 库仓库的某次commit
        }
    }]
}

注意:拷贝以上代码时请手动删除注释,实际项目中config.json中不能包含注释信息

配置说明:

CocoaPods 环境配置

在Mac系统上使用标准基座真机运行时才需要配置 CocoaPods 环境, 使用自定义调试基座提交云端打包则可以不配置 CocoaPods 环境。 在Windows系统上不支持 CocoaPods 环境,只能提交云端打包使用自定义调试基座。

CocoaPods安装

正常安装 CocoaPods 需要在终端执行下述命令

gem install cocoapods

CocoaPods 安装成功

这会耗费一段时间,完成后可查看 cocoapods 的版本号

pod --version

如: 1.12.1

也可以查看 pod 的安装位置

which pod

如:/Users/dcloud/.rvm/rubies/ruby-3.0.0/bin/pod

至此 CocoaPods 已经安装完成,可以尝试 Search 下一 pod 库:

pod search Alamofire

如果执行 pod search 失败,说明当前网络不能正常访问 github 或者 cdn, 更多信息详见无法访问 github , CDN错误

CocoaPods 安装失败

ERROR:  Error installing cocoapods:
        The last version of activesupport (>= 5.0, < 8) to support your Ruby & RubyGems was 6.1.7.3\. Try installing it with `gem install activesupport -v 6.1.7.3` and then running the current command again
        activesupport requires Ruby version >= 2.7.0\. The current ruby version is 2.6.10.210.

说明缺少activesupport插件,在终端执行以下命令

sudo gem install activesupport -v 6.1.7.3

插件安装成功后再次执行 sudo gem install cocoapods 安装 CocoaPods.

Building native extensions. This could take a while...
ERROR: Error installing cocoapods:
ERROR: Failed to build gem native extension.
current directory: /Library/Ruby/Gems/2.6.0/gems/ffi-1.15.5/ext/ffi c
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby-I/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0-r./siteconf20230612-9566-nht0td.rbextconf.rb
mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/2.6/us/lib/ruby/include/ruby.h
You might have to install separate package for the ruby development environment, ruby-dev or ruby-devel for example.
extconf failed, exit code 1
Gem files will remain installed in /Library/Ruby/Gems/2.6.0/gems/ffi-1.15.5 for inspection.
Results logged to /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-22/2.6.0/ffi-1.15.5/gem make.out

下面是升级 Gem 和 Ruby 的方法

升级 Gem

Gem 是管理 Ruby 标准包,如果 Gem 版本过低可能造成无法安装 CocoaPods,升级 Gem 使用下述命令

gem -v  //查看当前gem版本
sudo gem update --system   //升级gem

升级 Ruby

MacOS 会自带 Ruby,但可能系统的Ruby版本过低,导致无法安装 CocoaPods, 可通过以下方式更新 Ruby

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Ruby 的升级有以下两种常见的方式,可根据自己的需要任选其中一种:

通过 RVM 升级 Ruby

curl -sSL https://get.rvm.io | bash -s stable

这个命令会从 RVM 官网下载安装脚本并运行。安装完成后,需要在终端中执行以下命令以便让 RVM 生效:

source ~/.rvm/scripts/rvm

rvm -v

rvm list known

rvm install ruby-3.0.0

rvm use ruby-3.0.0

通过 Homebrew 升级 Ruby

在终端执行如下命令

 brew install ruby

安装完成后,ruby 默认使用的是系统自带的版本

ruby -v

 which ruby

 echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc

升级完 Gem 和 Ruby 后,在终端重新输入如下命令安装 CocoaPods。

sudo gem install cocoapods

安装完成后,可如上文所示,查看 CocoaPods 版本号。

常见问题
MAC 环境真机运行 uts 插件时未安装 CocoaPods
错误信息:uni_module xxxx (iOS) 存在pod三方依赖库,请先安装 CocoaPods! 说明:出现此错误是因为当前环境没有安装 CocoaPods 处理方法: 请参照上述章节描述的方式安装 CocoaPods 工具。

找不到指定版本的 pod 库,或者找不到指定依赖
错误信息:CocoaPods could not find compatible versions for pod "xxx" 或者 None of your spec sources contain a spec satisfying the dependency: 报错示例:

'Analyzing dependencies\n' +
    '[!] CocoaPods could not find compatible versions for pod "HandyJSON":\n' +
    '  In Podfile:\n' +
    '    HandyJSON (= 2.0.2)\n' +
    '\n' +
    'None of your spec sources contain a spec satisfying the dependency: `HandyJSON (= 2.0.2)`.\n' +
    '\n' +
    'You have either:\n' +
    ' * out-of-date source repos which you can update with `pod repo update` or with `pod install --repo-update`.\n' +
    ' * mistyped the name or version.\n' +
    ' * not added the source repo that hosts the Podspec to your Podfile.\n',

说明:出现此错误是因为执行 pod install 时找不到指定依赖。

处理方法:

请首先确保配置的 pod 库 name 正确,配置的 version 不高于 pod 库发行的最高版本号。
确保未使用存放在私有仓库的 pod 库。
真机运行时在确保配置正确的前提下触发重新编译。
云打包时请重新打包,或者联系管理员。

上一篇下一篇

猜你喜欢

热点阅读