iOS项目cocoapod 环境配置及pod install相关报错及解决

4,765 阅读2分钟

cocoapod环境配置

如果当前ruby版本是2.6,直接安装pod的话有可能会报错

# [ERROR: While executing gem ... (Gem::FilePermissionError)]

ruby、rvm

安装pod需要ruby3.0.0 目前我的Mac自带的ruby版本是2.6,所以需要升级下ruby,推荐使用rvm来做ruby的版本管理,首先安装下rvm:

curl -L get.rvm.io | bash -s stable
source ~/.bashrc
source ~/.bash_profile

修改 RVM 的 Ruby 安装源到 Ruby China 的 Ruby 镜像服务器,这样能提高安装速度

echo "ruby_url=https://cache.ruby-china.com/pub/ruby" > ~/.rvm/user/db
#查看所有可用版本
rvm list known
#安装3.0.0
rvm install 3.0.0
#切换ruby版本
rvm use 2.2.0

老电脑可以参考

搜解决方法的时候发现有人之前安装其他工具的时候也遇到过类似的问题

$ gem install passenger
Fetching: passenger-4.0.5.gem (100%)
ERROR:  While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195 directory.
$ ls -ld /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195
drwxrwsr-x  8 root  rvm  272 21 May 16:07 /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195

不过这是8年前的一个问题了,rvm已经修复

rvm get head
rvm fix-permissions

最终发现有效的方法还是升级到ruby3.0.0

homebrew

第一步

export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.ustc.edu.cn/brew.git"

第二步

export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.ustc.edu.cn/homebrew-core.git/"

第三步

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

Tips:安装好brew后推荐安装wget,很多好用的插件都是wget安装方式安装的

brew install wget

React Native 初始化后 pod install报错: SDK "iphoneos" cannot be located

应该是在 Xcode 没有同意协议前就安装了 pod

解决办法就是

  1. 输入下面的命令检查下错误所在

    xcrun -k --sdk iphoneos --show-sdk-path
    

    如果出现下面的内容,则说明我们的 XCode 路径错误

    xcrun:_ error: SDK "iphoneos" cannot be located
    xcrun: error: SDK "iphoneos" cannot be located
    xcrun: error: unable to lookup item 'Path' in SDK 'iphoneos'
    
  2. 输入以下命令检查系统默认的 XCode 路径

    xcode-select --print-path
    

    输出结果一般如下

    /Applications/Xcode.app/Contents/Developer
    
  3. 如果不是这个则运行下面的命令变更 XCode 路径

    xcode-select --switch /Applications/Xcode.app/Contents/Developer/
    

二、Warning:Your project does not explicitly specify the CocoaPods master specs repo.

[!] Your project does not explicitly specify the CocoaPods master specs repo. Since CDN is now used as the default, you may safely remove it from your repos directory via pod repo remove master. To suppress this warning please add warn_for_unused_master_specs_repo => false to your Podfile.

这个警告的原因应该是你用了默认的CocoaPods CDN:

source 'https://cdn.cocoapods.org/'

如果是这样,您可以使用建议的命令 pod repo remove master 从计算机中安全地删除主repo

我们也可以只针对当前项目在 Podfile 的顶部添加一个source来解决此警告:

source 'https://github.com/CocoaPods/Specs.git'