當(dāng)我們?yōu)锳pp工程導(dǎo)入第三方庫的時候經(jīng)常用到CocoaPods這個工具卧檐,這篇文章就介紹安裝CocoaPods的詳細(xì)記錄。
附:筆者介紹另一種導(dǎo)入第三方庫工具Carthage的文章焰宣,可以另外點(diǎn)擊進(jìn)入查看http://www.reibang.com/p/1de99aeedbf6
1.給Mac安裝Ruby環(huán)境
1.1 首先要安裝好RVM
$ curl -sSL https://get.rvm.io | bash -s stable
miki:~ ChenMan$ curl -sSL https://get.rvm.io | bash -s stable
Downloading https://github.com/rvm/rvm/archive/1.29.1.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.1/1.29.1.tar.gz.asc
Found PGP signature at: 'https://github.com/rvm/rvm/releases/download/1.29.1/1.29.1.tar.gz.asc',
but no GPG software exists to validate it, skipping.
Installing RVM to /Users/admin/.rvm/
Adding rvm PATH line to /Users/admin/.profile /Users/admin/.mkshrc /Users/admin/.bashrc /Users/admin/.zshrc.
Adding rvm loading line to /Users/admin/.profile /Users/admin/.bash_profile /Users/admin/.zlogin.
Installation of RVM in /Users/admin/.rvm/ is almost complete:
* To start using RVM you need to run `source /Users/admin/.rvm/scripts/rvm`
in all your open shell windows, in rare cases you need to reopen all shell windows.
# admin,
#
# Thank you for using RVM!
# We sincerely hope that RVM helps to make your life easier and more enjoyable!!!
#
# ~Wayne, Michal & team.
In case of problems: https://rvm.io/help and https://twitter.com/rvm_io
```
1.2 然后霉囚,載入 RVM 環(huán)境:
> $ source ~/.rvm/scripts/rvm
1.3 修改 RVM 下載 Ruby 的源,到 Ruby China 的鏡像:
> $ echo "ruby_url=https://cache.ruby-china.org/pub/ruby" > ~/.rvm/user/db
1.4 檢查一下是否安裝正確
> $ rvm -v
```
miki:~ ChenMan$ rvm -v
rvm 1.29.2 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io/]
```
1.5 安裝ruby
列出ruby可安裝的版本信息
> $ rvm list known
```
miki:~ ChenMan$ 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
# for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.2
# JRuby
jruby-1.6[.8]
jruby-1.7[.26]
jruby[-9.1.7.0]
jruby-head
# Rubinius
rbx-1[.4.3]
rbx-2.3[.0]
rbx-2.4[.1]
rbx-2[.5.8]
rbx[-3.71]
rbx-head
# Opal
opal
# Minimalistic ruby implementation - ISO 30170:2012
mruby-1.0.0
mruby-1.1.0
mruby-1[.2.0]
mruby[-head]
# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2012.02]
# Topaz
topaz
# MagLev
maglev[-head]
maglev-1.0.0
# Mac OS X Snow Leopard Or Newer
macruby-0.10
macruby-0.11
macruby[-0.12]
macruby-nightly
macruby-head
# IronRuby
ironruby[-1.1.3]
ironruby-head
```
1.6 安裝一個ruby版本
> $ rvm install 2.4.0
```
miki:~ ChenMan$ rvm install 2.4.0
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/10.12/x86_64/ruby-2.4.0.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx.
Installing requirements for osx.
Updating system.........................
Installing required packages: autoconf, automake, libtool, pkg-config, coreutils, libyaml, readline, libksba, openssl@1.1.........
Certificates in '/usr/local/etc/openssl@1.1/cert.pem' are already up to date.
Requirements installation successful.
Installing Ruby from source to: /Users/ChenMan/.rvm/rubies/ruby-2.4.0, this may take a while depending on your cpu(s)...
ruby-2.4.0 - #downloading ruby-2.4.0, this may take a while depending on your connection...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 11.9M 100 11.9M 0 0 496k 0 0:00:24 0:00:24 --:--:-- 458k
ruby-2.4.0 - #extracting ruby-2.4.0 to /Users/ChenMan/.rvm/src/ruby-2.4.0....
ruby-2.4.0 - #configuring..................................................................
ruby-2.4.0 - #post-configuration.
ruby-2.4.0 - #compiling.............................................................
ruby-2.4.0 - #installing.......
ruby-2.4.0 - #making binaries executable..
ruby-2.4.0 - #downloading rubygems-2.6.12
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 749k 100 749k 0 0 13229 0 0:00:58 0:00:58 --:--:-- 18271
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.4.0 - #extracting rubygems-2.6.12....
ruby-2.4.0 - #removing old rubygems.........
ruby-2.4.0 - #installing rubygems-2.6.12.........................
ruby-2.4.0 - #gemset created /Users/ChenMan/.rvm/gems/ruby-2.4.0@global
ruby-2.4.0 - #importing gemset /Users/ChenMan/.rvm/gemsets/global.gems..........................................|
ruby-2.4.0 - #generating global wrappers........
ruby-2.4.0 - #gemset created /Users/ChenMan/.rvm/gems/ruby-2.4.0
ruby-2.4.0 - #importing gemsetfile /Users/ChenMan/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.4.0 - #generating default wrappers........
ruby-2.4.0 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.4.0 - #complete
Ruby was built without documentation, to build it run: rvm docs generate-ri
```
1.7 如果想設(shè)置為默認(rèn)版本宛徊,可以用這條命令來完成
> $ rvm use 2.4.0 --default
```
miki:~ ChenMan$ rvm use 2.4.0 --default
Using /Users/ChenMan/.rvm/gems/ruby-2.4.0
```
1.8 修改 gem 鏡像
- 查看數(shù)據(jù)源:
> $ gem sources -l
```
miki:~ ChenMan$ gem sources -l
*** CURRENT SOURCES ***
https://ruby.taobao.org/
```
- 更換數(shù)據(jù)源
> $ gem sources --add https://gems.ruby-china.org/ --remove https://ruby.taobao.org/
```
miki:~ ChenMan$ gem sources --add https://gems.ruby-china.org/ --remove https://ruby.taobao.org/
https://gems.ruby-china.org/ added to sources
https://ruby.taobao.org/ removed from sources
```
- 再次查看確認(rèn)數(shù)據(jù)源:
```
miki:~ ChenMan$ gem sources -l
*** CURRENT SOURCES ***
https://gems.ruby-china.org/
```
- 更新gem
```
miki:~ ChenMan$ sudo gem update --system
Password:
Latest version currently installed. Aborting.
```
### 2.給Mac安裝CocoaPods
- 安裝CocoaPods
> $ gem install cocoapods
```
miki:~ ChenMan$ gem install cocoapods
Fetching: nanaimo-0.2.3.gem (100%)
Successfully installed nanaimo-0.2.3
Fetching: colored2-3.1.2.gem (100%)
Successfully installed colored2-3.1.2
Fetching: claide-1.0.2.gem (100%)
Successfully installed claide-1.0.2
Fetching: CFPropertyList-2.3.5.gem (100%)
Successfully installed CFPropertyList-2.3.5
Fetching: xcodeproj-1.5.0.gem (100%)
Successfully installed xcodeproj-1.5.0
Fetching: ruby-macho-1.1.0.gem (100%)
Successfully installed ruby-macho-1.1.0
Fetching: nap-1.1.0.gem (100%)
Successfully installed nap-1.1.0
Fetching: molinillo-0.5.7.gem (100%)
Successfully installed molinillo-0.5.7
Fetching: gh_inspector-1.0.3.gem (100%)
Successfully installed gh_inspector-1.0.3
Fetching: fourflusher-2.0.1.gem (100%)
Successfully installed fourflusher-2.0.1
Fetching: escape-0.0.4.gem (100%)
Successfully installed escape-0.0.4
Fetching: cocoapods-try-1.1.0.gem (100%)
Successfully installed cocoapods-try-1.1.0
Fetching: netrc-0.7.8.gem (100%)
Successfully installed netrc-0.7.8
Fetching: cocoapods-trunk-1.2.0.gem (100%)
Successfully installed cocoapods-trunk-1.2.0
Fetching: cocoapods-stats-1.0.0.gem (100%)
Successfully installed cocoapods-stats-1.0.0
Fetching: cocoapods-search-1.0.0.gem (100%)
Successfully installed cocoapods-search-1.0.0
Fetching: cocoapods-plugins-1.0.0.gem (100%)
Successfully installed cocoapods-plugins-1.0.0
Fetching: cocoapods-downloader-1.1.3.gem (100%)
Successfully installed cocoapods-downloader-1.1.3
Fetching: cocoapods-deintegrate-1.0.1.gem (100%)
Successfully installed cocoapods-deintegrate-1.0.1
Fetching: fuzzy_match-2.0.4.gem (100%)
Successfully installed fuzzy_match-2.0.4
Fetching: thread_safe-0.3.6.gem (100%)
Successfully installed thread_safe-0.3.6
Fetching: tzinfo-1.2.3.gem (100%)
Successfully installed tzinfo-1.2.3
Fetching: i18n-0.8.4.gem (100%)
Successfully installed i18n-0.8.4
Fetching: activesupport-4.2.9.gem (100%)
Successfully installed activesupport-4.2.9
Fetching: cocoapods-core-1.2.1.gem (100%)
Successfully installed cocoapods-core-1.2.1
Fetching: cocoapods-1.2.1.gem (100%)
Successfully installed cocoapods-1.2.1
Parsing documentation for nanaimo-0.2.3
Installing ri documentation for nanaimo-0.2.3
Parsing documentation for colored2-3.1.2
Installing ri documentation for colored2-3.1.2
Parsing documentation for claide-1.0.2
Installing ri documentation for claide-1.0.2
Parsing documentation for CFPropertyList-2.3.5
Installing ri documentation for CFPropertyList-2.3.5
Parsing documentation for xcodeproj-1.5.0
Installing ri documentation for xcodeproj-1.5.0
Parsing documentation for ruby-macho-1.1.0
Installing ri documentation for ruby-macho-1.1.0
Parsing documentation for nap-1.1.0
Installing ri documentation for nap-1.1.0
Parsing documentation for molinillo-0.5.7
Installing ri documentation for molinillo-0.5.7
Parsing documentation for gh_inspector-1.0.3
Installing ri documentation for gh_inspector-1.0.3
Parsing documentation for fourflusher-2.0.1
Installing ri documentation for fourflusher-2.0.1
Parsing documentation for escape-0.0.4
Installing ri documentation for escape-0.0.4
Parsing documentation for cocoapods-try-1.1.0
Installing ri documentation for cocoapods-try-1.1.0
Parsing documentation for netrc-0.7.8
Installing ri documentation for netrc-0.7.8
Parsing documentation for cocoapods-trunk-1.2.0
Installing ri documentation for cocoapods-trunk-1.2.0
Parsing documentation for cocoapods-stats-1.0.0
Installing ri documentation for cocoapods-stats-1.0.0
Parsing documentation for cocoapods-search-1.0.0
Installing ri documentation for cocoapods-search-1.0.0
Parsing documentation for cocoapods-plugins-1.0.0
Installing ri documentation for cocoapods-plugins-1.0.0
Parsing documentation for cocoapods-downloader-1.1.3
Installing ri documentation for cocoapods-downloader-1.1.3
Parsing documentation for cocoapods-deintegrate-1.0.1
Installing ri documentation for cocoapods-deintegrate-1.0.1
Parsing documentation for fuzzy_match-2.0.4
Installing ri documentation for fuzzy_match-2.0.4
Parsing documentation for thread_safe-0.3.6
Installing ri documentation for thread_safe-0.3.6
Parsing documentation for tzinfo-1.2.3
Installing ri documentation for tzinfo-1.2.3
Parsing documentation for i18n-0.8.4
Installing ri documentation for i18n-0.8.4
Parsing documentation for activesupport-4.2.9
Installing ri documentation for activesupport-4.2.9
Parsing documentation for cocoapods-core-1.2.1
Installing ri documentation for cocoapods-core-1.2.1
Parsing documentation for cocoapods-1.2.1
Installing ri documentation for cocoapods-1.2.1
Done installing documentation for nanaimo, colored2, claide, CFPropertyList, xcodeproj, ruby-macho, nap, molinillo, gh_inspector, fourflusher, escape, cocoapods-try, netrc, cocoapods-trunk, cocoapods-stats, cocoapods-search, cocoapods-plugins, cocoapods-downloader, cocoapods-deintegrate, fuzzy_match, thread_safe, tzinfo, i18n, activesupport, cocoapods-core, cocoapods after 12 seconds
26 gems installed
```
- 初始化CocoaPods的環(huán)境
> $ pod setup
```
miki:~ ChenMan$ pod setup
Setting up CocoaPods master repo
$ /usr/bin/git -C /Users/ChenMan/.cocoapods/repos/master fetch origin --progress
remote: Counting objects: 88167, done.
remote: Compressing objects: 100% (307/307), done.
remote: Total 88167 (delta 21314), reused 21457 (delta 21245), pack-reused 66588
Receiving objects: 100% (88167/88167), 8.71 MiB | 6.00 KiB/s, done.
Resolving deltas: 100% (59854/59854), completed with 5066 local objects.
From https://github.com/CocoaPods/Specs
27c8e72943f..2a4fdad64e1 master -> origin/master
$ /usr/bin/git -C /Users/ChenMan/.cocoapods/repos/master rev-parse --abbrev-ref HEAD
master
$ /usr/bin/git -C /Users/ChenMan/.cocoapods/repos/master reset --hard origin/master
HEAD is now at 2a4fdad64e1 [Add] ImageCoordinateSpace-Swift3 0.3.1-swift3
warning: inexact rename detection was skipped due to too many files.
warning: you may want to set your diff.renameLimit variable to at least 8755 and retry the command.
CocoaPods 1.3.0.beta.2 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.3.0.beta.2
Setup completed
```
### 3.使用CocoaPods
3.1 創(chuàng)建Xcode工程并切換到該工程路徑
```
$ cd /Users/ChenMan/iOSTest/008String_Test/test321
```
3.2 使用命令pod init在當(dāng)前文件夾下生成一個Podfile文件
```
$ pod init
```
3.3 編輯該文件佛嬉,在該文件中輸入如下信息:
```
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'test321' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Pods for test321
pod 'SDWebImage', '~>3.8'
pod "AFNetworking", "~> 2.5.4"
pod 'KVNProgress'
pod 'FMDB'
target 'test321Tests' do
inherit! :search_paths
# Pods for testing
end
target 'test321UITests' do
inherit! :search_paths
# Pods for testing
end
end
```
> 其中,該文件中的命令格式為:`pod '第三庫名稱', '版本號'`闸天。第三庫名稱,名稱一定要正確斜做,不然有可能安裝失敗苞氮。
- 版本號標(biāo)識區(qū)別:
> \>= 1.0 至少版本為1.0
~> 1.0 兼容1.0版本的最新版
== 1.0或1.0 都表示指定版本
3.4 安裝工程依賴的第三方庫
> $ pod install
```
miki:test321 ChenMan$ pod install
Analyzing dependencies
Downloading dependencies
Installing SDWebImage (3.8.2)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `test321.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
[!] Automatically assigning platform ios with version 10.3 on target test321 because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
```
若出現(xiàn)`pods installed`字樣表示安裝成功。
3.5 關(guān)閉Xcode工程瓤逼,打開.xcworkspace文件笼吟。
3.6 在工程中導(dǎo)入第三庫文件库物,只需要`#import <AFNetworking.h>`類似的即可,開啟CocoaPods之旅贷帮。
### 參考文獻(xiàn)
http://www.reibang.com/p/9d0885f944e3
http://www.reibang.com/p/c073e6fc01f5