mac 教程 安装cocoapod

1.首先查看ruby的版本

 ruby -v
image.png

2.查看rvm 版本

 rvm -v

2.1 控制台输出zsh: command not found: rvm问题
第一步下载rvm

  git clone https://github.com/rvm/rvm.git
image.png

下载完之后、根据路径、到rvm文件夹下、 找到 /bin/rvm-installer中 rvm-installer双击


image.png

安装完显示


image.png

2.2需要配置环境变量

vi ~/.bash_profile 

// 看有没有这一行,没有的话就在最后输入
[[ -s "HOME/.rvm/scripts/rvm" ]] && source "HOME/.rvm/scripts/rvm"

image.png

输入 :wq // 保存并退出

终端输入

source ~/.rvm/scripts/rvm
rvm -v

看到下面rvm版本说明安装成功


image.png

3.更换ruby镜像源

(gem更新)
sudo gem update --system
//查看源。
gem sources -l
//移除源。
gem sources --remove https://rubygems.org/
//更换源。
gem sources --add https://gems.ruby-china.com/
//查看是否更新成功。
 gem sources -l
(gem版本)
gem --version
image.png

4.安装CocoaPods

 sudo gem install -n /usr/local/bin cocoapods

pod安装成功之后一个首先的操作就是执行命令(不是必须的):
pod setup(安装本地库)【可以用git clone替代】
                      替代后要  pod repo update(更新本地库)
pod --version(pod版本)
pod 安装是否成功(1为安装成功,0为不成功)
pod repo list 

所有的第三方开源库的Podspec文件都托管在https://github.com/CocoaPods/Specs

我们需要把这个Podspec文件保存到本地,这样才能让我们使用命令pod search 开源库搜索一个开源库,怎样才能把github上的Podspec文件保存本地呢?那就是 pod setup

执行pod setup时,CocoaPods 会将第三方的podspec索引文件更新到本地的~/.cocoapods/repos目录下

如果没有执行过 pod setup,那用户根目录下~找不到.cocoapods/repos目录的,没有创建这个目录。

如果执行 pod setup,并且命令没有执行成功,那么会创建~/.cocoapods/repos目录,只不过目录是空的。

如果执行 pod setup,并且命令执行成功,说明把github上的Podsepc文件更新到本地,那么会创建~/.cocoapods/repos目录,并且repos目录里有一个master目录,这个master目录保存的就是github上所有第三方开源库的Podspec索引文件。

但是第一次执行pod setup时,这个github的Podspec索引文件比较大,有 300M 左右(以后会越来越大的),所以第一次更新时非常慢.要耐心等待......可以进去目录~/.cocoapods/repos使用命令du -sh *来查看下载文件的大小了

image.png

怎么能快点呢?

5.clone CocoPods仓库 ,这里用的是清华大学fork的CocoaPods仓库

cd ~/.cocoapods/repos
git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git  ~/.cocoapods/repos/trunk
image.png

clone完成后,需要cd 到trunk进行更新操作

pod repo update

然后就可以搜索AFNetworking库了

image.png

也可以用gitee上的一个私人fork的仓库,这个仓库比清华的速度更快,但是有可能更新不及时

git clone https://gitee.com/mirrors/CocoaPods-Specs.git  ~/.cocoapods/repos/trunk

官方库【用Git clone 拉取因为项目太大,只拉取深度为1的条件就好了】

pod setup   【提示:Setup completed】

cd ~/.cocoapods/repos

git clone --depth 1 https://github.com/CocoaPods/Specs.git master

image.png

上面CocoaPods仓库选择一个clone就行了
查看仓库

~/.cocoapods/repos

常用pod指令

pod install --verbose --no-repo-update
该命令只安装新添加的库,已更新的库忽略
pod update 库名--verbose --no-repo-update
该命令只更新指定的库,其它库忽略
更新所有的第三方库:pod update --verbose --no-repo-update
podfile 里面删除要删除的库
当需要在CocoaPods中删除一个我们不要的库时 可以在Podfile中直接删除相关库
个人建议使用 pod install --verbose --no-repo-update

查看Cocoapods引入第三方库的版本号
1.打开终端,cd 项目文件目录
2.执行命令:cat Podfile.lock
3.查看输出:Podfile.lock文件会跟踪每个pod的已安装版本并锁定这些版本。

执行pod outdated命令,可以查看项目中非最新的第三方库的名字、版本号以及对应的最新的版本号,项目中已经是最新的库不显示

依赖库的基本写法

pod 'JYCarousel', //不显式指定依赖库版本,表示每次都获取最新版本
pod 'JYCarousel', '0.01'//只使用0.0.1版本
pod 'JYCarousel', '>0.0.1' //使用高于0.0.1的版本
pod 'JYCarousel', '>=0.0.1' //使用大于或等于0.0.1的版本
pod 'JYCarousel', '<0.0.2' //使用小于0.0.2的版本
pod 'JYCarousel', '<=0.0.2' //使用小于或等于0.0.2的版本
pod 'JYCarousel', '~>0.0.1' //使用大于等于0.0.1但小于0.1的版本,相当于>=0.0.1&&<0.1
pod 'JYCarousel', '~>0.1' //使用大于等于0.1但小于1.0的版本
pod 'JYCarousel', '~>0' //高于0的版本,写这个限制和什么都不写是一个效果,都表示使用最新版本

--------------- 更新CocoaPods -------------

更新ruby
检查是否有安装homebrew

brew --version

查看homebrew源

# 查看 brew.git 当前源
cd "$(brew --repo)" && git remote -v

# 查看 homebrew-core.git 当前源
cd "$(brew --repo homebrew/core)" && git remote -v

# 查看 homebrew-cask.git 当前源
cd "$(brew --repo homebrew/cask)" && git remote -v
image.png

检查 Homebrew 的安装路径
Apple Silicon (M1/M2) Mac:/opt/homebrew
Intel Mac:/usr/local
运行以下命令确认路径:

brew --prefix
image.png

如果输出是 /opt/homebrew,但错误显示 /usr/local/Homebrew,说明存在路径混淆。

  1. 修复路径问题
    如果是 Apple Silicon Mac 但误装到 /usr/local:
# 卸载旧 Homebrew(Intel 路径)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

# 重新安装到正确路径
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 更新 Shell 配置
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc
source ~/.zshrc
编辑Shell配置文件:
nano ~/.zshrc

control + X 退出编辑

编辑Shell配置文件:
source ~/.zshrc

清理残留文件

sudo rm -rf /opt/homebrew /usr/local/Homebrew

使用镜像源重装

export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
/bin/bash -c "$(curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/misc/brew-install.sh)"

使用国内镜像源安装

/bin/bash -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

image.png

更新ruby、如果你在 MacOS 上,并且使用 Homebrew,可以通过以下命令升级:

brew install ruby

升级后,你可以通过运行以下命令来验证 Ruby 的版本:

ruby -v
image.png
sudo arch -x86_64 gem install ffi
arch -x86_64 pod install
image.png

可能遇到的错误提示及解决方法

错误一

error: RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: CANCEL (err 8)
fatal: the remote end hung up unexpectedly

将http.postBuffer设置的尽量大,例如
git config --global http.postBuffer 524288000

错误二

error: RPC failed; curl 16 Error in the HTTP2 framing layer
fatal: expected flush after ref listing

直接换掉Git的http版本

git config --flobal http.version HTTP/1.1
image.png

错误三

[!] CocoaPods could not find compatible versions for pod "ZLPhotoBrowser":
  In snapshot (Podfile.lock):
    ZLPhotoBrowser (= 4.1.9)

  In Podfile:
    ZLPhotoBrowser
    image_pickers (from `../hmc_flutter_module/.ios/.symlinks/plugins/image_pickers/ios`) was resolved to 0.0.1, which depends on
      ZLPhotoBrowser (~> 4.3.9)
You have either:
 * out-of-date source repos which you can update with `pod repo update` or with `pod install --repo-update`.
 * changed the constraints of dependency `ZLPhotoBrowser` inside your development pod `image_pickers`.
   You should run `pod update ZLPhotoBrowser` to apply changes you've made.

作者的开源库的ZLPhotoBrowser.podspec文件中关于deployment_target 配置写的10.0,

s,ios.deployment_target = '10.0'
image.png

解决办法:直接把自己项目的podfile中的版本改成10.0,跟开源库要求的最低系统版本一直,就可以了

错误四

[!] Error installing ZLPhotoBrowser
[!] /usr/bin/git clone https://github.com/longitachi/ZLPhotoBrowser.git /var/folders/2n/3rgg_c01349cm02gdbtsqbdr0000gn/T/d20241129-41290-i45lty --template= --single-branch --depth 1 --branch 4.5.1

Cloning into '/var/folders/2n/3rgg_c01349cm02gdbtsqbdr0000gn/T/d20241129-41290-i45lty'...
error: RPC failed; curl 18 transfer closed with outstanding read data remaining
error: 2587 bytes of body are still expected
fetch-pack: unexpected disconnect while reading sideband packet
fatal: early EOF
fatal: fetch-pack: invalid index-pack output
image.png

解决办法:

pod update ZLPhotoBrowser --verbose --no-repo-update

安装后的环境版本(小括号中 对应终端查看命令):

Homebrew 2.7.5 (brew -v)
Rvm: 1.29.12 (rvm -v)
Ruby: 2.6.3p62 (ruby -v)
Cocoapod: 1.10.1 (pod --version)

M1 MacBook Pro 下:

①使用 Rosetta 方式运行终端。应用程序 -> 实用工具 -> 终端/terminal -> 右键 -> 显示简介 -> 勾选使用 Rosetta 打开
② 终端执行 sudo gem install ffi 命令

每次执行Pod相关操作 都有“warning: Insecure world writable dir /usr/local/sbin in PATH, mode 040777”这个提示,应该是这个目录的权限问题。

执行   sudo chmod go-w /usr/local/sbin  后重新执行pod 命令

错误四

Resolving dependencies of `Podfile`
  CDN: trunk Relative path: CocoaPods-version.yml exists! Returning local
  because checking is only perfomed in repo update
[!] CocoaPods could not find compatible versions for pod "ZLPhotoBrowser":
  In Podfile:
    ZLPhotoBrowser (= 4.5.6)

    image_pickers (from `../hmc_flutter_module/.ios/.symlinks/plugins/image_pickers/ios`) was resolved to 0.0.1, which depends on
      ZLPhotoBrowser (= 4.5.1)

Specs satisfying the `ZLPhotoBrowser (= 4.5.6), ZLPhotoBrowser (= 4.5.1)` dependency were found, but they required a higher minimum deployment target.

报错原因:
直接依赖冲突:
Podfile 中直接指定了 ZLPhotoBrowser (= 4.5.5);
image_pickers 插件依赖 ZLPhotoBrowser (= 4.5.1)。 两者的版本约束互斥,导致无法同时满足。

解决方案:
iOS项目与flutter项目里面的ZLPhotoBrowser版本库要一致

iOS项目


image.png

flutter项目


image.png

cd到原生项目执行

pod update ZLPhotoBrowser --verbose --no-repo-update

错误五


image.png
[!] `FMDB` requires CocoaPods version `>= 1.12.0`, which is not satisfied by your current version, `1.10.1`.

iOS 无法忽略UserInterface.xcuserstate文件

修改sourcetree忽略文件中添加UserInterface.xcuserstate,结果提交还是忽略不了。UserInterface.xcuserstate文件


image.png
//cd 到项目目录下
git status
//会出现一个 modified(修改): xxxx/UserInterfaceState.xcuserstate 的地址   注意:xxxx需要替换项目的路径
git rm --cached  xxxx/UserInterfaceState.xcuserstate
git add .
git commit -m '删除xcuserstate'
git pull 
git push

方案二
提交的时候出现这个文件,那就是git忽略文件的问题,检查项目中是否存在.gitignore文件,发现项目目录下是没有这个文件的,那就创建.gitignore文件,把需要忽略的文件后缀添加进去,然后提交。

// *.xcuserstate
// UserInterfaceState.xcuserstate

// cd 到项目目录下
ls -la //查看项目所有文件(包括隐藏文件)
// 如果没有.gitignore文件,执行以下命令,然后在编辑.gitignore文件,把要忽略的文件类型添加进去
// 如果存在则看文件中是否包含要忽略的UserInterface.xcuserstate文件类型,没有则添加进去,保存
vim .gitignore

git status
//如果会出现一个 modified(修改): xxxx/UserInterfaceState.xcuserstate 的地址,如果没有跳过这一步
// 如果有必须执行这一步,删除git 仓库中xxxx/UserInterfaceState.xcuserstate缓存文件
// 否则提交还是会有.xcuserstate文件
git rm --cached  xxxx/UserInterfaceState.xcuserstate

git add . 
git commit -m '忽略UserInterface.xcuserstate文件类型提交'
git pull 
git push

错误六
[!] Invalid Podfile file: undefined method 'exists?' for class File.

[!] Invalid `Podfile` file: undefined method 'exists?' for class File.

 #  from /Users/apple/Desktop/Git/HDAPP/lightBeauth/Podfile:22
 #  -------------------------------------------
 #  
 >  install_all_flutter_pods(flutter_application_path)
 #  
 #  -------------------------------------------

报错原因:该错误通常是由于Ruby 3.2移除了File.exists?方法,而Flutter项目中的Podfile仍在使用此旧方法导致的兼容性问题

解决方案:
cd 到flutter项目的.iOS目录下,打开pohhelper.rb文件


image.png
return [] unless File.exists? file_path

替换成

return [] unless File.exist? file_path
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容