CocoaPods-安装、更新、报错等使用总结
CocoaPods简介:
CocoaPods是一个用Ruby写的、负责管理iOS项目中第三方开源库的工具,CocoaPods能让我们集中的、统一管理第三方开源库,为我们节省设置和更新第三方开源库的时间。
CocoaPods安装:
因为Mac电脑自带Ruby环境,我们就只需打开终端开始动手。然而又因为默认情况下我们mac系统自带的Ruby环境版本比较低(大概是2.0.0或者其他较高版本),但是现在安装CocoaPods需要2.2.2版本及以上的,所以我们不管三七二十一先直接先升级ruby。
-
安装
CocoaPods
:sudo gem install -n /usr/local/bin cocoapods
:可能需要输入电脑密码:执行结果:
Password: Fetching claide-1.0.3.gem Fetching CFPropertyList-3.0.4.gem Fetching atomos-0.1.3.gem Fetching xcodeproj-1.21.0.gem Fetching ruby-macho-2.5.1.gem Fetching nanaimo-0.3.0.gem Fetching colored2-3.1.2.gem Fetching nap-1.1.0.gem Fetching molinillo-0.8.0.gem Fetching gh_inspector-1.1.3.gem Fetching fourflusher-2.3.1.gem Fetching escape-0.0.4.gem Fetching cocoapods-try-1.2.0.gem Fetching netrc-0.11.0.gem Fetching cocoapods-trunk-1.6.0.gem Fetching cocoapods-search-1.0.1.gem Fetching cocoapods-plugins-1.0.0.gem Fetching cocoapods-downloader-1.5.1.gem Fetching cocoapods-deintegrate-1.0.5.gem Fetching ffi-1.15.4.gem Fetching ethon-0.15.0.gem Fetching typhoeus-1.4.0.gem Fetching public_suffix-4.0.6.gem Fetching fuzzy_match-2.0.4.gem Fetching concurrent-ruby-1.1.9.gem Fetching httpclient-2.8.3.gem Fetching algoliasearch-1.27.5.gem Fetching addressable-2.8.0.gem Fetching zeitwerk-2.4.2.gem Fetching tzinfo-2.0.4.gem Fetching i18n-1.8.10.gem Fetching activesupport-6.1.4.1.gem Fetching cocoapods-1.11.2.gem Fetching cocoapods-core-1.11.2.gem Successfully installed nanaimo-0.3.0 Successfully installed colored2-3.1.2 Successfully installed claide-1.0.3 ... ... ... Parsing documentation for activesupport-6.1.4.1 Installing ri documentation for activesupport-6.1.4.1 Parsing documentation for cocoapods-core-1.11.2 Installing ri documentation for cocoapods-core-1.11.2 Parsing documentation for cocoapods-1.11.2 Installing ri documentation for cocoapods-1.11.2 Done installing documentation for nanaimo, colored2, claide, CFPropertyList, atomos, xcodeproj, ruby-macho, nap, molinillo, gh_inspector, fourflusher, escape, cocoapods-try, netrc, cocoapods-trunk, cocoapods-search, cocoapods-plugins, cocoapods-downloader, cocoapods-deintegrate, ffi, ethon, typhoeus, public_suffix, fuzzy_match, concurrent-ruby, httpclient, algoliasearch, addressable, zeitwerk, tzinfo, i18n, activesupport, cocoapods-core, cocoapods after 12 seconds 34 gems installed
如果安装了多个Xcode使用下面的命令选择(一般需要选择最近的Xcode版本):
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
;-
安装本地库:
pod setup
:执行结果:
Setting up CocoaPods master repo $ /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress Cloning into 'master'... remote: Counting objects: 1879515, done. remote: Compressing objects: 100% (321/321), done. Receiving objects: 21% (404525/1879515), 73.70 MiB | 22.00 KiB/
然后就是漫长的等待,当然,网络好的情况下会更快。
说明:该步骤是安装本地库,不安装也没事,项目可以照常pod install。
只不过不安装的话就不能使用pod search 命令了;如果你追求完美那就继续往下看;
最新版的MacOS Catalina 及以后的系统命令行执行pod setup命令后直接结束啦;
需要手动安装本地库:
```
git clone https://github.com/CocoaPods/Specs.git ~/.cocoapods/repos/trunk
```
目录结构见下图,如果不是就自己手动移动一下:
![1974693-c13a2a46bc31e2d0.png](https://upload-images.jianshu.io/upload_images/2625770-e0fd8c469301562f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
CocoaPods版本升级
执行命令:
gem -v
,查看当前RubyGems
版本;-
执行命令:
gem sources -l
,查看RubyGems
源,确保只有gems.ruby-china.com
;执行结果:
*** CURRENT SOURCES *** https://gems.ruby-china.com
执行命令:
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
,修改RubyGems
源:删除rubygems.org
源,添加gems.ruby-china.com
源;执行命令:
gem update --system
,如果需要管理员权限就执行:sudo gem update --system
,更新RubyGems
版本,可能需要翻墙;-
执行命令:
pod --version
,查看自己的CocoaPods
版本号;执行结果:
1.8.4
; 执行命令:
sudo gem install cocoapods
,安装或者更新CocoaPods
;-
输入电脑登录密码;
执行结果:
..... ..... Installing ri documentation for cocoapods-1.9.1 Done installing documentation for ffi, ethon, typhoeus, cocoapods-core, xcodeproj, cocoapods after 33 seconds 6 gems installed
执行命令:
pod --version
,查看下CocoaPods
版本号是否更新;
到此,CocoaPods
升级完成了。
CocoaPods常用语法
// 安装最新版cocoapods
sudo gem install cocoapods --pre
// 移除本地master
sudo rm -fr ~/.cocoapods/repos/master
// 移除本地缓存
sudo rm -fr ~/Library/Caches/CocoaPods/
// 重新setup,如果很慢可使用问题1的解决方法(git clone)
pod setup --verbose
// 移除trunk
pod repo remove trunk
pod search AFNetworking
// 创建Podfile文件:
pod init
// 打开Podfile文件:
open Podfile
// 添加:
pod 'Alamofire'
// 开始下载:
pod install
CocoaPods错误处理
-
cocoapods-1.8.4
版本错误:-
错误内容:
[!] CDN: trunk Repo update failed - 7 error(s): CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/0/f/b/MJRefresh/1.3.5/MJRefresh.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443) CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/0/f/b/MJRefresh/2.4.1/MJRefresh.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443) CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/0/f/b/MJRefresh/2.4.11/MJRefresh.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443) CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/0/f/b/MJRefresh/3.0.6/MJRefresh.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443) CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/0/f/b/MJRefresh/3.1.0/MJRefresh.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443) CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/0/f/b/MJRefresh/3.1.12/MJRefresh.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443) CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/0/f/b/MJRefresh/3.1.15.3/MJRefresh.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443)
解决办法:终端运行:
pod repo remove trunk
;
-