iOS開發(fā)必備:cocoapods
一、什么是cocoapods倘屹?
CocoaPods是一個用來幫助我們管理第三方依賴庫的工具银亲。
它可以解決庫與庫之間的依賴關(guān)系,下載庫的源代碼务蝠,同時通過創(chuàng)建一個Xcode的workspace來將這些第三方庫和我們的工程連接起來,供我們開發(fā)使用烛缔。
使用CocoaPods的目的是讓我們能自動化的馏段、集中的、直觀的管理第三方開源庫(比如JSONKit践瓷,AFNetWorking等)院喜。
二、如何安裝当窗?
方法一:當然是官方的文檔最權(quán)威啦够坐,這就需要去官網(wǎng)看看了寸宵。
CocoaPods是Swift和Objective-C Cocoa項目的依賴管理器崖面。 它有超過28萬個圖書館,并在超過170萬的應用程序中使用梯影。 CocoaPods可以幫助您優(yōu)化擴展項目巫员。
CocoaPods是用Ruby構(gòu)建的,可以使用OS X上提供的默認Ruby安裝甲棍。我們建議您使用默認的ruby简识。
使用默認的Ruby安裝可能需要在安裝gem時使用sudo。 更多安裝說明在指南中感猛。
https://guides.cocoapods.org/using/getting-started.html#getting-started
這是官方的安裝指南七扰。
如果你英語和我一樣不太好的話,還是用民間的傳統(tǒng)方法吧陪白。
方法二:小白新手安裝步驟詳解>弊摺!咱士!
CocoaPods是以Ruby gem包的形式被安裝的立由。Mac OS系統(tǒng)是自帶Ruby環(huán)境的。
可以先在終端(Terminal)輸入:sudo gem install cocoapods序厉,如果不出錯的話锐膜,那是萬幸了,但是基本上都是會出現(xiàn)error的弛房,因為ruby的軟件源rubygems.org因為使用亞馬遜的云服務道盏,被我天朝屏蔽了,所以需要更新一下ruby的源。
保守起見首先還是先查看下當前ruby版本:
打開終端(Terminal)輸入?
ruby -v
2.更新Ruby源荷逞,網(wǎng)上的一種解決方案是將遠替換成淘寶的牺堰,不過現(xiàn)在淘寶的 ruby 源已經(jīng)不維護了,請?zhí)鎿Q成 Ruby China颅围,
具體輸入:
gem sources --removehttps://rubygems.org/? ? ?/*去掉ruby源*/
gem sources -ahttps://gems.ruby-china.org/? ? ? ?/*添加新的源*/
gem sources -l? ? ? /*查看當前ruby源*/
3.安裝:
具體輸入:
sudo gem install cocoapods
4.如果你的gem太老伟葫,可能也會有問題,可以嘗試用如下命令升級gem:
具體輸入:
sudo gem update --system
之后的新建podfile網(wǎng)上都有流程啦院促,自己動手一下筏养,搜索。
我要說一下我遇到的問題常拓。
cocoapods安裝及使用遇到的問題合集:
1.
安裝cocoapods過程中渐溶,需要輸入電腦密碼,輸入密碼的時候弄抬,你會發(fā)現(xiàn)光標和平常不一樣茎辐,而且你輸入的時候數(shù)字會顯示不出來,不用理會掂恕,直接輸入完按enter/return回車鍵拖陆。
2.當輸入“sudo gem install cocoapods”或者“sudo gem install -n /usr/local/bin cocoapods”
出現(xiàn)以下錯誤:
ERROR:? While executing gem ... (Gem::DependencyError)
Unable to resolve dependencies: cocoapods requires cocoapods-core (= 1.2.0), cocoapods-downloader (< 2.0, >= 1.1.3), cocoapods-trunk (< 2.0, >= 1.1.2), molinillo (~> 0.5.5), xcodeproj (< 2.0, >= 1.4.1)
yangyangyangdeMacBook-Pro:~ yang$ sudo nvram boot-args="rootless=0"
nvram: Error setting variable - 'boot-args': (iokit/common) general error
解決方法:
輸入:
rvm list known
出現(xiàn)錯誤:-bash: rvm: command not found
則繼續(xù)輸入:
curl -L get.rvm.io | bash -s stable
出現(xiàn)以下:
即表示rvm安裝完成。
繼續(xù)輸入:
rvm list known
出現(xiàn)以下:
會顯示出所有的ruby的版本
yangyangyangdeMacBook-Pro:~ yang$ rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.6]
[ruby-]2.3[.3]
[ruby-]2.4[.0]
ruby-head
.
.
.
# IronRuby
ironruby[-1.1.3]
ironruby-head
繼續(xù)輸入:sudo gem install cocoa pods
出現(xiàn)以下:
yangyangyangdeMacBook-Pro:~ yang$ sudo gem install cocoa pods
Password:
Fetching: i18n-0.7.0.gem (100%)
Successfully installed i18n-0.7.0
Fetching: thread_safe-0.3.5.gem (100%)
Successfully installed thread_safe-0.3.5
Fetching: tzinfo-1.2.2.gem (100%)
Successfully installed tzinfo-1.2.2
Fetching: minitest-5.9.1.gem (100%)
Successfully installed minitest-5.9.1
Fetching: concurrent-ruby-1.0.2.gem (100%)
Successfully installed concurrent-ruby-1.0.2
Fetching: activesupport-5.0.1.gem (100%)
ERROR:? Error installing cocoa:
activesupport requires Ruby version >= 2.2.2.
ERROR:? Error installing pods:
activesupport requires Ruby version >= 2.2.2.
繼續(xù)輸入:rvm install 2.2.2? /*選擇安裝最新的Ruby*/
出現(xiàn)以下:
Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/osx/10.12/x86_64/ruby-2.2.2.tar.bz2
Checking requirements for osx.
About to install Homebrew, press `Enter` for default installation in `/usr/local`,
type new path if you wish custom Homebrew installation (the path needs to be writable for user)
:
==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew
Press RETURN to continue or any other key to abort
==> Downloading and installing Homebrew...
remote: Counting objects: 5079, done.
remote: Compressing objects: 100% (3186/3186), done.
remote: Total 5079 (delta 2798), reused 3338 (delta 1722), pack-reused 0
Receiving objects: 100% (5079/5079), 2.66 MiB | 44.00 KiB/s, done.
Resolving deltas: 100% (2798/2798), done.
From https://github.com/Homebrew/brew
...
* [new tag]? ? ? ? 1.1.9? ? ? -> 1.1.9
HEAD is now at dfad3f3 Merge pull request #2034 from reitermarkus/spec-gpg
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
remote: Counting objects: 4137, done.
remote: Compressing objects: 100% (4013/4013), done.
remote: Total 4137 (delta 30), reused 433 (delta 9), pack-reused 0
Receiving objects: 100% (4137/4137), 3.31 MiB | 87.00 KiB/s, done.
Resolving deltas: 100% (30/30), done.
Tapped 4010 formulae (4,166 files, 10.4M)
==> Cleaning up /Library/Caches/Homebrew...
==> Migrating /Library/Caches/Homebrew to /Users/yang/Library/Caches/Homebrew...
==> Deleting /Library/Caches/Homebrew...
Already up-to-date.
==> Installation successful!
==> Homebrew has enabled anonymous aggregate user behaviour analytics.
Read the analytics documentation (and how to opt-out) here:
https://git.io/brew-analytics
==> Next steps:
- Run `brew help` to get started
- Further documentation:
https://git.io/brew-docs
Installing requirements for osx.
Updating system.........
Installing required packages: autoconf, automake, libtool, pkg-config, coreutils, libyaml, readline, libksba, openssl...........
Certificates in '/usr/local/etc/openssl/cert.pem' are already up to date.
Requirements installation successful.
ruby-2.2.2 - #configure
ruby-2.2.2 - #download
% Total? ? % Received % Xferd? Average Speed? Time? ? Time? ? Time? Current
Dload? Upload? Total? Spent? ? Left? Speed
100 6854k? 100 6854k? ? 0? ? 0? 14400? ? ? 0? 0:08:07? 0:08:07 --:--:-- 17131
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.2.2 - #validate archive
ruby-2.2.2 - #extract
ruby-2.2.2 - #validate binary
ruby-2.2.2 - #setup
ruby-2.2.2 - #gemset created /Users/yang/.rvm/gems/ruby-2.2.2@global
ruby-2.2.2 - #importing gemset /Users/yang/.rvm/gemsets/global.gems.............
ruby-2.2.2 - #generating global wrappers........
ruby-2.2.2 - #gemset created /Users/yang/.rvm/gems/ruby-2.2.2
ruby-2.2.2 - #importing gemsetfile /Users/yang/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.2.2 - #generating default wrappers........
Updating certificates in '/etc/openssl/cert.pem'.
mkdir: /etc/openssl: Permission denied
mkdir -p "/etc/openssl" failed, retrying with sudo
yang password required for 'mkdir -p /etc/openssl':
and sudo mkdir worked
繼續(xù)輸入:
sudo gem install cocoapods
出現(xiàn)以下:
最終看到了:26 gems installed
就代表最終cocoapods安裝好啦
三懊亡、如何新建Podfile文件依啰?
終端輸入:
vim Podfile
點擊“i”,進入文件編輯模式
輸入:
platform :ios, '10.0'? /*設置iOS 10的版本號*/
target 'Cloud Weather' do? /*目標文件*/
pod 'AFNetworking'? ? /*輸入你要用到的第三方庫店枣,后面可以設置版本號速警,如果不設置就是默認最新版本*/
end
編輯完成后,然后按Esc鸯两,并且輸入“ :”號進入vim命令模式闷旧,然后在冒號后邊輸入wq
注意:鍵盤輸入 :后,才能輸入wq钧唐∶ψ疲回車后發(fā)現(xiàn)PodTest項目總目錄中多一個Podfile文件
繼續(xù)在終端中輸入:
pod install
如上圖所示,
打開文件夾逾柿,會出現(xiàn)以下三個文件