最新CocoaPods安装以及使用

  • 目录
  • 介绍
  • 日期版本
  • 安装
  • 使用
  • Podfile
  • 卸载
  • 安装和使用时候遇到的问题

CocoaPods是比较出名的类库管理工具
开源:https://github.com/CocoaPods/CocoaPods


当前日期:2018.05.03
笔者系统版本:


安装

  1. 安装Ruby环境
    mac自带Ruby,可以升级一下
    $ sudo gem update --system
192:~ fengfeng$ $sudo gem update --system
Updating installed gems
Nothing to update
192:~ fengfeng$ 
  1. 请尽可能用比较新的RubyGems版本,建议2.6.x以上,需翻墙
    $ gem update --system
    $ gem -v
192:~ fengfeng$ sudo gem update --system
Updating rubygems-update
Fetching: rubygems-update-2.6.13.gem (100%)
Successfully installed rubygems-update-2.6.13
Parsing documentation for rubygems-update-2.6.13
Installing ri documentation for rubygems-update-2.6.13
Installing darkfish documentation for rubygems-update-2.6.13
Done installing documentation for rubygems-update after 34 seconds
Parsing documentation for rubygems-update-2.6.13
Done installing documentation for rubygems-update after 0 seconds
Installing RubyGems 2.6.13
RubyGems 2.6.13 installed
Parsing documentation for rubygems-2.6.13
Installing ri documentation for rubygems-2.6.13

=== 2.6.13 / 2017-08-27

Security fixes:

* Fix a DNS request hijacking vulnerability.
  Fix by Samuel Giddins.
* Fix an ANSI escape sequence vulnerability.
  Fix by Evan Phoenix.
* Fix a DOS vulernerability in the `query` command.
  Fix by Samuel Giddins.
* Fix a vulnerability in the gem installer that allowed
  a malicious gem to overwrite arbitrary files.
  Fix by Samuel Giddins.

=== ...

------------------------------------------------------------------------------

RubyGems installed the following executables:
    /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/gem

Ruby Interactive (ri) documentation was installed. ri is kind of like man 
pages for ruby libraries. You may access it like this:
  ri Classname
  ri Classname.class_method
  ri Classname#instance_method
If you do not wish to install this documentation in the future, use the
--no-document flag, or set it as the default in your ~/.gemrc file. See
'gem help env' for details.

RubyGems system software updated
192:~ fengfeng$ 
192:~ fengfeng$ gem -v
2.6.13
192:~ fengfeng$ 
  1. 更换镜像
    安装时需要访问 cocoapods.org,需要翻墙
    淘宝RubyGems镜像 https://ruby.taobao.org/ 目前已经不再维护,使用 https://gems.ruby-china.org/
    执行
    $ gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
    或使用下面两命令
    $ gem sources --remove https://rubygems.org/
    $ gem sources -a https://gems.ruby-china.org/

2019.02.13
因为ruby的软件源 https://gems.ruby-china.org/ 无效了
需要更新一下ruby的源为 https://gems.ruby-china.com/

查看$ gem sources -l

192:~ fengfeng$ gem sources -l
*** CURRENT SOURCES ***
# 确保只有 https://gems.ruby-china.org/
https://gems.ruby-china.org/
  1. 安装CocoaPods
    macOS 10.11前:$ sudo gem install cocoapods
    macOS 10.11后:$ sudo gem install -n /usr/local/bin cocoapods
192:~ fengfeng$ sudo gem install cocoapods
Password:
Fetching: concurrent-ruby-1.0.5.gem (100%)
Successfully installed concurrent-ruby-1.0.5
Fetching: i18n-0.9.5.gem (100%)
Successfully installed i18n-0.9.5
Fetching: thread_safe-0.3.6.gem (100%)
Successfully installed thread_safe-0.3.6
Fetching: tzinfo-1.2.5.gem (100%)
Successfully installed tzinfo-1.2.5
Fetching: activesupport-4.2.10.gem (100%)
Successfully installed activesupport-4.2.10
Fetching: nap-1.1.0.gem (100%)
Successfully installed nap-1.1.0
Fetching: fuzzy_match-2.0.4.gem (100%)
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /usr/bin directory.
192:~ fengfeng$ pod -version
-bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby: bad interpreter: No such file or directory
192:~ fengfeng$ 

出错的情况可以执行

192:~ fengfeng$ sudo gem install -n /usr/local/bin cocoapods
Successfully installed fuzzy_match-2.0.4
Fetching: cocoapods-core-1.5.0.gem (100%)
Successfully installed cocoapods-core-1.5.0
Fetching: claide-1.0.2.gem (100%)
Successfully installed claide-1.0.2
...
Done installing documentation for fuzzy_match, cocoapods-core, claide, cocoapods-deintegrate, cocoapods-downloader, cocoapods-plugins, cocoapods-search, cocoapods-stats, netrc, cocoapods-trunk, cocoapods-try, molinillo, atomos, CFPropertyList, colored2, nanaimo, xcodeproj, escape, fourflusher, gh_inspector, ruby-macho, cocoapods after 9 seconds
22 gems installed
192:~ fengfeng$

成功后查看版本

192:~ fengfeng$ pod --version
1.5.0
192:~ fengfeng$ 
  1. pod setup
    成功安装后,执行pod setup
    所有的项目的podspec文件都托管在https://github.com/CocoaPods/Specs
    第一次执行pod setup时,CocoaPods会将这些podspec索引文件更新到本地~/.cocoapods/目录下,这个索引文件比较大
    所以......超慢......
192:~ fengfeng$ pod setup
Setting up CocoaPods master repo
  $ /usr/bin/git -C /Users/fengfeng/.cocoapods/repos/master fetch origin --progress
  remote: Counting objects: 1296676, done.        
  remote: Compressing objects: 100% (319/319), done.        
  Receiving objects:   2% (37212/1296676), 7.40 MiB | 13.00 KiB/s     

查看文件大小$ du -sh *
在未完全下载的时候执行

192:~ fengfeng$ pod repo

0 repos
192:~ fengfeng$ 

一晚上,断断续续的还没好,现在都青年节了🤣🤣🤣
最后我是使用git clone --depth=1 https://github.com/CocoaPods/Specs.git master
--depth用于指定克隆深度,=1即表示只克隆最近一次commit
参考问题2
目前最新的大概50+M,断断续续的好久,才执行完毕
然后

192:repos fengfeng$ pod repo

master
- Type: git (master)
- URL:  https://github.com/CocoaPods/Specs.git
- Path: /Users/fengfeng/.cocoapods/repos/master

1 repo
192:repos
  1. 下载完毕后,重新执行pod setup,又是漫长的等待...
    但是这一步不执行即可
192:master fengfeng$ pod setup
Setting up CocoaPods master repo
Performing a deep fetch of the `master` specs repo to improve future performance

  1. 完毕

使用

  1. cd到项目根目录
  2. 创建Podfile文件,$ touch Podfile,也可$ pod init(建议)
  3. 可以先$ pod search ***,找到想要的版本,记着红色标记里面的,需要写在Podfile
  1. $ vim Podfile,除了vim,也可以使用其他,vim语法暂不表述
使用 pod init 创建的,对应添加即可
touch 的全部需自己写
  1. $ pod install
192:runtime fengfeng$ pod install
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (3.2.0)
Installing SDWebImage (3.8.2)
Generating Pods project
Integrating client project

# 看这句
[!] Please close any current Xcode sessions and use `runtime.xcworkspace` for this project from now on. 
Sending stats
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.
192:runtime fengfeng$ 
未执行前
执行后

$ pod install后会有👇这句提示:
Please close any current Xcode sessions and use `runtime.xcworkspace` for this project from now on.
以后打开项目,必须使用,*****.xcworkspace,不可使用,*****.xcodeproj

  1. 有时下载的一些开源项目的时候,会发现是使用pod来管理的,并且会有Podfile文件存在,此时只需要cd ***到下载好的项目根路径,然后执行pod install即可

基本使用如此~


Podfile

  1. https://guides.cocoapods.org/syntax/podfile.html#podfile
  2. 项目根路径,初始的时候是没有Podfile这个文件的,需要手动创建($ touch Podfile)或者使用$ pod init
    建议使用$ pod init,会帮你创建好基本格式
使用 pod init 创建的,对应添加即可
  1. Podfile.lock用来记录着上一次下载的版本,如下图
    $ vim Podfile.lock

卸载

  1. 安装路径$ which pod
192:~ fengfeng$ which pod
/usr/local/bin/pod
192:~ fengfeng$ 
  1. $ sudo rm -rf /usr/local/bin/pod,回车,输入密码
  2. $ gem list
    移除RubyGems中的Cocoapods程序包,RubyGems(简称gems)是一个用于对Ruby组件进行打包的Ruby打包系统。 它提供一个分发Ruby程序和库的标准格式,还提供一个管理程序包安装的工具
    查看gems中本地程序包
192:~ fengfeng$ gem list

*** LOCAL GEMS ***

...
cocoapods (1.5.0)
cocoapods-core (1.5.0)
cocoapods-deintegrate (1.0.2)
cocoapods-downloader (1.2.0)
cocoapods-plugins (1.0.0)
cocoapods-search (1.0.0)
cocoapods-stats (1.0.0)
cocoapods-trunk (1.3.0)
cocoapods-try (1.1.0)
...
192:~ fengfeng$ 
  1. 依次删除上面对应的包和版本
    $ sudo gem uninstall cocoapods -v 1.5.0
    $ sudo gem uninstall cocoapods-core -v 1.5.0
    ...
  2. 可以执行,$ pod search ...来验证是否成功删除
  3. 完成

安装和使用时候遇到的问题

  1. pod setup超慢,一脸懵逼,google到的结果
    方法2不行,缺少.git文件
    实际解决方法,为问题2,虽然还是很慢,但毕竟目前才50+M
    有正确解决的办法,还请指点~
  • 方法1:如果同事mac上已经装好了,从同事电脑上copy一份到~/.cocoapods/repos/即可
  • 方法2:从https://github.com/CocoaPods/Specs (github所有的第三方开源库的podspec文件都托管在这里,这也是你pod setup所要下载的),dodwnload或者什么方式下载到本地,然后解压,将解压后的文件移动到~/.cocoapods/repos(这个路径是在执行pod setup生成的,如果没有此路径,先pod setup下,然后取消control l即可),然后重命名为master即可
192:runtime fengfeng$ pod setup
Setting up CocoaPods master repo
fatal: Not a git repository (or any of the parent directories): .git
[!] The `master` repo is not a git repo.
192:runtime fengfeng$ 
  1. 报错:RPC failed; curl 18 transfer closed with outstanding read data remaining fatal: The remote end hung up unexpectedly
    由于pod setup超慢,所以想直接下载最近的一次提交,结果报错
    git clone --depth=1 https://github.com/CocoaPods/Specs.git master
    --depth用于指定克隆深度,=1即表示只克隆最近一次commit
192:repos fengfeng$ git clone --depth=1 https://github.com/CocoaPods/Specs.git master
Cloning into 'master'...
remote: Counting objects: 518394, done.
remote: Compressing objects: 100% (330152/330152), done.
error: RPC failed; curl 18 transfer closed with outstanding read data remaining
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
192:repos fengfeng$

因为,curl的postBuffer默认值太小的原因,重新在终端配置下即可
git config --global http.postBuffer 10485760001GB)(也可以把--global去掉)
查看git config --list
但是,使用这种方法,如果在更新三方的时候使用$ pod update会卡在Updating local specs repositories,需要使$ pod install

192:runtime fengfeng$ pod update
Update all pods
Updating local specs repositories
Performing a deep fetch of the `master` specs repo to improve future performance

  1. 执行$ pod install会停在Analyzing dependencies
    执行$ pod update会停在Updating local specs repositories
    这是因为这两个命令会升级CocoaPodsspec仓库
    可使用👇命令:
    $ pod install --verbose --no-repo-update
    $ pod update --verbose --no-repo-update

不定期更新 不合适的地方 还请指点~ 感激不尽

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

推荐阅读更多精彩内容

  • 一. CocoaPods的介绍 什么是CocoaPods?CocoaPods是一个负责管理iOS项目中第三方开源库...
    辉712阅读 3,905评论 0 7
  • CocoaPods 是什么? CocoaPods 是一个负责管理 iOS 项目中第三方开源库的工具。CocoaPo...
    朝洋阅读 25,593评论 3 51
  • 项目组件化、平台化是技术公司的共同目标,越来越多的技术公司推崇使用pod管理第三方库以及私有组件,一方面使项目架构...
    swu_luo阅读 21,201评论 0 39
  • 夫妻之间吵吵闹闹也是生活的一部分,但争吵之后,互相谦让才是根本,为爱妥协不丢人,为家忍受不委屈。 ...
    钝角阅读 683评论 0 36
  • 01 大学里最后一个暑假,打着学车的旗号在家里得过且过。 晚上八点黄金档,我在电视机前对每个台的节目进行扫射。微信...
    一只螃蟹367阅读 308评论 0 0