最新CocoaPods安裝以及使用

  • 目錄
  • 介紹
  • 日期版本
  • 安裝
  • 使用
  • Podfile
  • 卸載
  • 安裝和使用時(shí)候遇到的問題

CocoaPods是比較出名的類庫管理工具
開源:https://github.com/CocoaPods/CocoaPods


當(dāng)前日期:2018.05.03
筆者系統(tǒng)版本:


安裝

  1. 安裝Ruby環(huán)境
    mac自帶Ruby,可以升級(jí)一下
    $ sudo gem update --system
192:~ fengfeng$ $sudo gem update --system
Updating installed gems
Nothing to update
192:~ fengfeng$ 
  1. 請(qǐng)盡可能用比較新的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. 更換鏡像
    安裝時(shí)需要訪問 cocoapods.org,需要翻墻
    淘寶RubyGems鏡像 https://ruby.taobao.org/ 目前已經(jīng)不再維護(hù),使用 https://gems.ruby-china.org/
    執(zhí)行
    $ 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
因?yàn)?code>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$ 

出錯(cuò)的情況可以執(zhí)行

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
    成功安裝后,執(zhí)行pod setup
    所有的項(xiàng)目的podspec文件都托管在https://github.com/CocoaPods/Specs
    第一次執(zhí)行pod setup時(shí),CocoaPods會(huì)將這些podspec索引文件更新到本地~/.cocoapods/目錄下,這個(gè)索引文件比較大
    所以......超慢......
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 *
在未完全下載的時(shí)候執(zhí)行

192:~ fengfeng$ pod repo

0 repos
192:~ fengfeng$ 

一晚上,斷斷續(xù)續(xù)的還沒好,現(xiàn)在都青年節(jié)了??????
最后我是使用git clone --depth=1 https://github.com/CocoaPods/Specs.git master
--depth用于指定克隆深度,=1即表示只克隆最近一次commit
參考問題2
目前最新的大概50+M,斷斷續(xù)續(xù)的好久,才執(zhí)行完畢
然后

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. 下載完畢后,重新執(zhí)行pod setup,又是漫長的等待...
    但是這一步不執(zhí)行即可
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到項(xiàng)目根目錄
  2. 創(chuàng)建Podfile文件,$ touch Podfile,也可$ pod init(建議)
  3. 可以先$ pod search ***,找到想要的版本,記著紅色標(biāo)記里面的,需要寫在Podfile
  1. $ vim Podfile,除了vim,也可以使用其他,vim語法暫不表述
使用 pod init 創(chuàng)建的,對(duì)應(yīng)添加即可
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$ 
未執(zhí)行前
執(zhí)行后

$ pod install后會(huì)有??這句提示:
Please close any current Xcode sessions and use `runtime.xcworkspace` for this project from now on.
以后打開項(xiàng)目,必須使用,*****.xcworkspace,不可使用,*****.xcodeproj

  1. 有時(shí)下載的一些開源項(xiàng)目的時(shí)候,會(huì)發(fā)現(xiàn)是使用pod來管理的,并且會(huì)有Podfile文件存在,此時(shí)只需要cd ***到下載好的項(xiàng)目根路徑,然后執(zhí)行pod install即可

基本使用如此~


Podfile

  1. https://guides.cocoapods.org/syntax/podfile.html#podfile
  2. 項(xiàng)目根路徑,初始的時(shí)候是沒有Podfile這個(gè)文件的,需要手動(dòng)創(chuàng)建($ touch Podfile)或者使用$ pod init
    建議使用$ pod init,會(huì)幫你創(chuàng)建好基本格式
使用 pod init 創(chuàng)建的,對(duì)應(yīng)添加即可
  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)是一個(gè)用于對(duì)Ruby組件進(jìn)行打包的Ruby打包系統(tǒng)。 它提供一個(gè)分發(fā)Ruby程序和庫的標(biāo)準(zhǔn)格式,還提供一個(gè)管理程序包安裝的工具
    查看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. 依次刪除上面對(duì)應(yīng)的包和版本
    $ sudo gem uninstall cocoapods -v 1.5.0
    $ sudo gem uninstall cocoapods-core -v 1.5.0
    ...
  2. 可以執(zhí)行,$ pod search ...來驗(yàn)證是否成功刪除
  3. 完成

安裝和使用時(shí)候遇到的問題

  1. pod setup超慢,一臉懵逼,google到的結(jié)果
    方法2不行,缺少.git文件
    實(shí)際解決方法,為問題2,雖然還是很慢,但畢竟目前才50+M
    有正確解決的辦法,還請(qǐng)指點(diǎn)~
  • 方法1:如果同事mac上已經(jīng)裝好了,從同事電腦上copy一份到~/.cocoapods/repos/即可
  • 方法2:從https://github.com/CocoaPods/Specs (github所有的第三方開源庫的podspec文件都托管在這里,這也是你pod setup所要下載的),dodwnload或者什么方式下載到本地,然后解壓,將解壓后的文件移動(dòng)到~/.cocoapods/repos(這個(gè)路徑是在執(zhí)行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. 報(bào)錯(cuò):RPC failed; curl 18 transfer closed with outstanding read data remaining fatal: The remote end hung up unexpectedly
    由于pod setup超慢,所以想直接下載最近的一次提交,結(jié)果報(bào)錯(cuò)
    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$

因?yàn)?,curl的postBuffer默認(rèn)值太小的原因,重新在終端配置下即可
git config --global http.postBuffer 10485760001GB)(也可以把--global去掉)
查看git config --list
但是,使用這種方法,如果在更新三方的時(shí)候使用$ pod update會(huì)卡在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. 執(zhí)行$ pod install會(huì)停在Analyzing dependencies
    執(zhí)行$ pod update會(huì)停在Updating local specs repositories
    這是因?yàn)檫@兩個(gè)命令會(huì)升級(jí)CocoaPodsspec倉庫
    可使用??命令:
    $ pod install --verbose --no-repo-update
    $ pod update --verbose --no-repo-update

不定期更新 不合適的地方 還請(qǐng)指點(diǎn)~ 感激不盡

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一. CocoaPods的介紹 什么是CocoaPods?CocoaPods是一個(gè)負(fù)責(zé)管理iOS項(xiàng)目中第三方開源庫...
    輝712閱讀 4,113評(píng)論 0 7
  • CocoaPods 是什么? CocoaPods 是一個(gè)負(fù)責(zé)管理 iOS 項(xiàng)目中第三方開源庫的工具。CocoaPo...
    朝洋閱讀 25,995評(píng)論 3 50
  • 項(xiàng)目組件化、平臺(tái)化是技術(shù)公司的共同目標(biāo),越來越多的技術(shù)公司推崇使用pod管理第三方庫以及私有組件,一方面使項(xiàng)目架構(gòu)...
    swu_luo閱讀 22,858評(píng)論 0 39
  • 夫妻之間吵吵鬧鬧也是生活的一部分,但爭(zhēng)吵之后,互相謙讓才是根本,為愛妥協(xié)不丟人,為家忍受不委屈。 ...
    鈍角閱讀 825評(píng)論 0 45
  • 01 大學(xué)里最后一個(gè)暑假,打著學(xué)車的旗號(hào)在家里得過且過。 晚上八點(diǎn)黃金檔,我在電視機(jī)前對(duì)每個(gè)臺(tái)的節(jié)目進(jìn)行掃射。微信...
    一只螃蟹367閱讀 395評(píng)論 0 0

友情鏈接更多精彩內(nèi)容