CocoaPods|安装流程与使用

系统环境:macOS CataLina 10.15.2

1. 安装 RVM

  • 开始安装
curl -L get.rvm.io | bash -s stable
  • 进入目录
source ~/.bashrc
source ~/.bash_profile
  • 成功后查看版本
rvm -v

2. Ruby升级

  • 获取Ruby版本
ruby -v
  • 对比Ruby版本
rvm list known
  • 更新Ruby
rvm install 2.7.0-preview1

3. 更换Ruby源

  • 查看当前源
gem sources -l
  • 移除默认源
gem sources --remove https://ruby.taobao.org/
  • 替换源
gem sources -a https://gems.ruby-china.com/

(尾巴这条斜杆需要斟酌移除)

4. 安装CocoaPods

  1. OS X 10.11之前系统的安装cocoapods 指令
$ sudo gem install cocoapods
  1. OS X 10.11以后系统的安装cocoapods 指令
$ sudo gem install -n /usr/local/bin cocoa pods
  • 安装
pod setup
  • 完成之后
pod --version //查看版本
  • 测试一下
pod search afnetworking

使用流程

cd 目标路径
pod init
  • 然后操作Podfile文件,一般使用vim
  # Uncomment the next line to define a global platform for your project
platform :ios, '8.0'

target 'ProjectName' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  pod 'AFNetworking'

end
  • 完成它
pod install
  • 如若比较慢,可使用{🔽},无须更新本地库情况下(更新同理)
pod install --no-repo-update

打开白色工程 projectName.xcworkspace

5. 更新CocoaPods

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

6. 更新CocoaPods 1.8.+版本本地库

  • 1.8+强制安装了新的源trunk,采用cdn替换了原本的master
  • 直接通过cdn方式更新本地库
iMac:mac$ pod install
    Analyzing dependencies
    Adding spec repo `trunk` with CDN `https://cdn.cocoapods.org/`
    Downloading dependencies
    Generating Pods project
    Integrating client project
    Pod installation complete! There are 28 dependencies from the Podfile and 32 total pods installed.

附录

  • 查看已下载各Pod版本
    可直接打开Podfile.lock查看,例如:
PODS:
  - UMCAnalytics (6.0.5):
    - UMCCommon
  - UMCCommon (2.1.1)
  - UMCSecurityPlugins (1.0.6)

问题与解决


更新系统Or Xcode产生的问题
  1. 在更新OS或Xcode之后
    出现 Failed to extract git version from ·git --version· 🔽
macdeMacBook-Air:~ mac$ pod --version
Traceback (most recent call last):
    7: from /Users/mac/.rvm/rubies/ruby-2.7.0-preview1/bin/ruby_executable_hooks:24:in `<main>'
    6: from /Users/mac/.rvm/rubies/ruby-2.7.0-preview1/bin/ruby_executable_hooks:24:in `eval'
    5: from /Users/mac/.rvm/rubies/ruby-2.7.0-preview1/bin/pod:23:in `<main>'
    4: from /Users/mac/.rvm/rubies/ruby-2.7.0-preview1/bin/pod:23:in `load'
    3: from /Users/mac/.rvm/rubies/ruby-2.7.0-preview1/lib/ruby/gems/2.7.0/gems/cocoapods-1.8.4/bin/pod:55:in `<top (required)>'
    2: from /Users/mac/.rvm/rubies/ruby-2.7.0-preview1/lib/ruby/gems/2.7.0/gems/cocoapods-1.8.4/lib/cocoapods/command.rb:49:in `run'
    1: from /Users/mac/.rvm/rubies/ruby-2.7.0-preview1/lib/ruby/gems/2.7.0/gems/cocoapods-1.8.4/lib/cocoapods/command.rb:130:in `verify_minimum_git_version!'
/Users/mac/.rvm/rubies/ruby-2.7.0-preview1/lib/ruby/gems/2.7.0/gems/cocoapods-1.8.4/lib/cocoapods/command.rb:118:in `git_version': Failed to extract git version from `git --version` ("xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun\\n") (RuntimeError)
  • 解决方法
xcode-select --install
  1. 更新系统后若发生 No such file or directory🔽
xiaowen:~ natsuhirofumi$ pod --version
-bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby: bad interpreter: No such file or directory
  • 解决方法
// 指定缺失路径安装
sudo gem install -n /usr/local/bin cocoapods

本地存在旧缓存的问题,如⏬
[!] CocoaPods could not find compatible versions for pod "SDWebImage":
  In Podfile:
    SDWebImage (~> 5.8.1)

None of your spec sources contain a spec satisfying the dependency: `SDWebImage (~> 5.8.1)`.

You have either:
 * out-of-date source repos which you can update with `pod repo update` or with `pod install --repo-update`.
 * mistyped the name or version.
 * not added the source repo that hosts the Podspec to your Podfile.
  • 调用刷新命令
pod update --verbose

更新CocoaPods 1.8+CDN产生的问题
  • 早在1.7.2 CocoaPods就将源方式切换成CDN方式

CDN support was first introduced in the 1.7 release and was finalized in 1.7.2. It aims to speed up initial setup and dependency analysis dramatically.

  • 但在1.8+中CDN方式成为默认选项

CocoaPods 1.8 switches the CDN as the default spec repo source

  1. RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
    更新本地库出现报错
  • 解决方法:

git clone https://git.coding.net/CocoaPods/Specs.git ~/.cocoapods/repos/master

  • 网络欠佳可以试试别的源
///因Coding停止维护,可切别的源~
* https://code.aliyun.com/Magi/CocoaPods.git   
* https://gitclub.cn/CocoaPods/Specs.git `推荐`
  • 如还是无法解决,可参照下面DNS污染问题尝试解决(修改host)
  1. 如果pod install中发现:
You have either: 
* out-of-date source repos which you can update with `pod repo update`.
* mistyped the name or version. 
* not added the source repo that hosts the Podspec to your Podfile.

Note: as of CocoaPods 1.0, `pod repo update` does not happen on `pod install` by default.
  • 可尝试直接更新最新版本
sudo gem install cocoapods --pre
  • 如果还是不行,可移除缓存重新安装,或者
    ⌘ + ⇧ + G 进入repos目录直接删除master/trunk
// 移除本地master
sudo rm -fr ~/.cocoapods/repos/master
// 移除本地缓存
sudo rm -fr ~/Library/Caches/CocoaPods/
-------------方法分隔线-------------
// repos目录
~/.cocoapods/repos/
  • 移除本地库后
// 重新setup,如果很慢可使用问题1的解决方法(git clone)
pod setup --verbose

出现最多的连接问题
  1. 更新时,CocoaPods 1.8.0 的trunk连接失败的问题

[!] CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/ Response: 400
或者是
[!] CDN: trunk Repo update failed - 70 error(s): CDN: trunk URL couldn't be downloaded: [库信息], error: Failed to open TCP connection to raw.githubusercontent.com:443
亦或是
[!] CDN: trunk Repo update failed - 14 error(s): CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/[库路径].podspec.json Response: Couldn't connect to server

解决方法一(使用旧下载方式)
  • 打开podfile文件,顶部添加{🔽},指定旧方式下载
//source `源地址`
source 'https://github.com/CocoaPods/Specs.git'
  • 因为脱离trunk方式,亦可移除trunk
pod repo remove trunk
  • 可确认是否已移除trunk
pod repo list
解决方法二(解决DNS污染问题)
  • CDN方式以trunk代替Master
  • 编辑Podfile,将CDN设置为主要来源,1.8+已是默认项
- source 'https://github.com/CocoaPods/Specs.git'
+ source 'https://cdn.cocoapods.org/'
  • 移除本地库
pod repo remove master
  • ping 出ip 地址
ping raw.githubusercontent.com
  • 获取ip地址后 + c 退出

    捕获ip地址

  • 或使用nslookup

nslookup raw.githubusercontent.com

----------------------------------->>
Server:     192.168.31.1
Address:    192.168.31.1#53

Non-authoritative answer:
Name:   raw.githubusercontent.com
Address: 151.101.228.133
  • 修改/etc/hosts
sudo vi /etc/hosts
  • i 编辑模式
// 添加在最后一行
151.101.228.133 raw.githubusercontent.com
如图所示
  • 刷新DNS缓存
sudo killall -HUP mDNSResponder
  • 重试
pod install

---皆大欢喜---
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (3.2.1)
Installing UMCAnalytics (6.0.5)
Installing UMCCommon (2.1.1)
Installing UMCSecurityPlugins (1.0.6)
Generating Pods project
Integrating client project
Pod installation complete! There are 4 dependencies from the Podfile and 4 total pods installed.
  • 同理网络问题,可获取对应ip地址写进host直接访问

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
## github
13.250.177.223 github.com
69.171.244.12 github.global.ssl.fastly.net
199.232.28.133 raw.githubusercontent.com (真实ip)
## cocoa
157.230.35.153 cdn.cocoapods.org
~ 
/** 
* 说明:
* 未标记真实ip,直接通过nslookup获取
**/

索引文章 🐈

一、[模块化工程] | 工作空间篇 | .xcworkspace | Development Pods | 混合应用 =❤姐妹篇=

  • 关于Xcode工作区间的应用,适用于单项目多端开发,以及整合本地 Development Pod 集成使用

二、模块化工程 | CocoaPods篇|远程私有库搭建流程 [通用工具箱] =姐妹篇❤=

  • 手把手搭建远程库,以及过程中遇见的问题及解决方法

三、iOS | 关于AppDelegate瘦身的想法与实践


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容