感悟
費(fèi)了兩天晚上的功夫安裝上了,一邊玩守望先鋒 ,一邊看CocoaPods下載的進(jìn)度.300多兆的文件,下載了3個(gè)多小時(shí),既然咱不是特別聰明,咱就多努力,多一點(diǎn)堅(jiān)持慢慢來吧.整個(gè)安裝過程中終端的內(nèi)容如下(可以略過,對我有點(diǎn)用):
<pre>
<code>
Last login: Sat Jul 23 23:07:37 on ttys001 bogon:~ liyilin$ gem source -l *** CURRENT SOURCES *** https://ruby.taobao.org/ bogon:~ liyilin$ sudo gem install cocoapods Password: ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/pod bogon:~ liyilin$ sudo gem update --system Updating rubygems-update ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/update_rubygems bogon:~ liyilin$ sudo gem update-n/usr/local/bin --system ERROR: While executing gem ... (Gem::CommandLineError) Unknown command update-n/usr/local/bin bogon:~ liyilin$ sudo gem update-n /usr/local/bin —system ERROR: While executing gem ... (Gem::CommandLineError) Unknown command update-n bogon:~ liyilin$ sudo gem update -n /usr/local/bin —system Updating installed gems Nothing to update bogon:~ liyilin$ sudo gem install -n /usr/local/bin cocoapods Successfully installed cocoapods-1.0.1 Parsing documentation for cocoapods-1.0.1 1 gem installed bogon:~ liyilin$ pod install [!] No 'Podfile' found in the project directory bogon:~ liyilin$ pod search AFNetworking Setting up CocoaPods master repo Setup completed [!] Unable to find a pod with name, author, summary, or description matchingAFNetworking`
bogon:~ liyilin$ rm ~/Library/Caches/CocoaPods/search_index.json
bogon:~ liyilin$ pod search AFNetworking
Creating search index for spec repo 'master'.. Done!
- Homepage: https://github.com/steipete/AFDownloadRequestOperation
- Source: https://github.com/steipete/AFDownloadRequestOperation.git
- Versions: 2.0.1, 2.0.0, 1.1.0, 1.0.0, 0.0.2, 0.0.1 [master repo]
-> AFgzipRequestSerializer (0.0.2)
gzip Request Serializer for AFNetworking
pod 'AFgzipRequestSerializer', '~> 0.0.2' - Homepage: https://github.com/AFNetworking/AFgzipRequestSerializer
- Source: https://github.com/AFNetworking/AFgzipRequestSerializer.git
- Versions: 0.0.2, 0.0.1 [master repo]
-> AFHARchiver (0.2.2)
An AFNetworking extension to automatically generate a HTTP Archive file of
all of your network requests.
pod 'AFHARchiver', '~> 0.2.2' - Homepage: https://github.com/mutualmobile/AFHARchiver
- Source: https://github.com/mutualmobile/AFHARchiver.git
- Versions: 0.2.2, 0.2.1, 0.2.0, 0.1.0, 0.0.2, 0.0.1 [master repo]
-> AFHTTPClientLogger (0.7.0)
AFNetworking Extension for request logging.
pod 'AFHTTPClientLogger', '~> 0.7.0' - Homepage: https://github.com/jparise/AFHTTPClientLogger
- Source: https://github.com/jparise/AFHTTPClientLogger.git
- Versions: 0.7.0, 0.6.1, 0.6.0, 0.5.0 [master repo]
-> AFHTTPFileUpdateOperation (0.0.1)
AFNetworking extenstion for updating file with HTTP header
"If-Modified-Since" and status code 304.
pod 'AFHTTPFileUpdateOperation', '~> 0.0.1' - Homepage: https://github.com/samnung/AFHTTPFileUpdateOperation
- Source: https://github.com/samnung/AFHTTPFileUpdateOperation.git
- Versions: 0.0.1 [master repo]
-> AFHTTPRequestOperationLogger (1.0.0)
:
`
</code>
</pre>
CocoaPods是什么?
CocoaPods 是開發(fā) OS X 和 iOS 應(yīng)用程序的一個(gè)第三方庫的依賴管理工具。利用 CocoaPods,可以定義自己的依賴關(guān)系 (稱作 pods),并且隨著時(shí)間的變化,以及在整個(gè)開發(fā)環(huán)境中對第三方庫的版本管理非常方便。
CocoaPods 背后的理念主要體現(xiàn)在兩個(gè)方面。
首先,在工程中引入第三方代碼會(huì)涉及到許多內(nèi)容。對開發(fā)者來說,工程文件的配置會(huì)讓人很沮喪。在配置 build phases 和 linker flags 過程中,會(huì)引起許多人為因素的錯(cuò)誤。CocoaPods 簡化了這一切,它能夠自動(dòng)配置編譯選項(xiàng)。
通過 CocoaPods,可以很方便的查找到新的第三方庫。
CocoaPods是用 Ruby 寫的,想要使用它首先要有Ruby環(huán)境.
RubyGems簡稱gems,是一個(gè)用于對Ruby組件進(jìn)行打包的Ruby打包系統(tǒng)
個(gè)人總結(jié)的安裝步驟(看了好多相關(guān)的博客)
將ruby的默認(rèn)源替換為淘寶的RubyGems鏡像
因?yàn)閲鴥?nèi)網(wǎng)絡(luò)原因,導(dǎo)致rubygems.org存放在亞馬遜上面的資源容易連接失敗
gem sources --remove https://rubygems.org/ gem sources -a https://ruby.taobao.org/
查看此時(shí)的源
bogon:~ liyilin$ gem source -l
輸出結(jié)果
*** CURRENT SOURCES *** https://ruby.taobao.org/
之后就可以更新Mac的gem,gem是管理Ruby庫和程序的標(biāo)準(zhǔn)包
bogon:~ liyilin$ sudo gem update --system
終端提示
Updating rubygems-update ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/update_rubygems
上網(wǎng)搜索后得知
Mac OS X 10.11后不能使用sudo gem update --system了
需要替換成:sudo gem update -n /usr/local/bin —system
bogon:~ liyilin$ sudo gem update -n /usr/local/bin —system
終端提示貌似沒有可以更新的內(nèi)容
Updating installed gems Nothing to update
接下來安裝CocoaPods
bogon:~ liyilin$ sudo gem install -n /usr/local/bin cocoa pods
終端提示成功了...
Successfully installed cocoapods-1.0.1 Parsing documentation for cocoapods-1.0.1 1 gem installed
使用CocoaPods查找需要的第三方框架(就拿AFNetworking試試吧)
bogon:~ liyilin$ pod search AFNetworking
終端提示如下,并卡住了!
Setting up CocoaPods master repo
上網(wǎng)搜索后得知Cocoapods在將它的信息下載到 ~/.cocoapods里
然后我就前往這個(gè)文件夾,不斷用快捷鍵預(yù)覽它的大小
下載速度太慢了,我想先去玩幾把守望先鋒吧,也不能干等著吧 玩了五局定級(jí)賽(2勝3負(fù)),還沒下載完.那我去搓幾把爐石吧.
爐石任務(wù)正刷著的時(shí)候,終端顯示下載完了,但是還有一個(gè)警告
Setup completed [!] Unable to find a pod with name, author, summary, or description matching 'AFNetworking'
上網(wǎng)搜索后得知要?jiǎng)h除一個(gè)文件
bogon:~ liyilin$ rm ~/Library/Caches/CocoaPods/search_index.json
然后我再一次搜索AFNetworking
bogon:~ liyilin$ pod search AFNetworking
終端打印如下:
<pre>
<code>
`
Creating search index for spec repo 'master'.. Done!
- Homepage: https://github.com/steipete/AFDownloadRequestOperation
- Source: https://github.com/steipete/AFDownloadRequestOperation.git
- Versions: 2.0.1, 2.0.0, 1.1.0, 1.0.0, 0.0.2, 0.0.1 [master repo]
-> AFgzipRequestSerializer (0.0.2)
gzip Request Serializer for AFNetworking
pod 'AFgzipRequestSerializer', '~> 0.0.2' - Homepage: https://github.com/AFNetworking/AFgzipRequestSerializer
- Source: https://github.com/AFNetworking/AFgzipRequestSerializer.git
- Versions: 0.0.2, 0.0.1 [master repo]
-> AFHARchiver (0.2.2)
An AFNetworking extension to automatically generate a HTTP Archive file of
all of your network requests.
pod 'AFHARchiver', '~> 0.2.2' - Homepage: https://github.com/mutualmobile/AFHARchiver
- Source: https://github.com/mutualmobile/AFHARchiver.git
- Versions: 0.2.2, 0.2.1, 0.2.0, 0.1.0, 0.0.2, 0.0.1 [master repo]
-> AFHTTPClientLogger (0.7.0)
AFNetworking Extension for request logging.
pod 'AFHTTPClientLogger', '~> 0.7.0' - Homepage: https://github.com/jparise/AFHTTPClientLogger
- Source: https://github.com/jparise/AFHTTPClientLogger.git
- Versions: 0.7.0, 0.6.1, 0.6.0, 0.5.0 [master repo]
-> AFHTTPFileUpdateOperation (0.0.1)
AFNetworking extenstion for updating file with HTTP header
"If-Modified-Since" and status code 304.
pod 'AFHTTPFileUpdateOperation', '~> 0.0.1' - Homepage: https://github.com/samnung/AFHTTPFileUpdateOperation
- Source: https://github.com/samnung/AFHTTPFileUpdateOperation.git
- Versions: 0.0.1 [master repo]
-> AFHTTPRequestOperationLogger (1.0.0)
`
</code>
</pre>
應(yīng)該是搜索出了與AFNetworking相關(guān)的內(nèi)容
我再一次重新搜索AFNetworking,終端顯示如下
<pre>
<code>
`
-> AFNetworking (3.1.0)
A delightful iOS and OS X networking framework.
pod 'AFNetworking', '~> 3.1.0'
- Homepage: https://github.com/AFNetworking/AFNetworking
- Source: https://github.com/AFNetworking/AFNetworking.git
- Versions: 3.1.0, 3.0.4, 3.0.3, 3.0.2, 3.0.1, 3.0.0, 3.0.0-beta.3,
3.0.0-beta.2, 3.0.0-beta.1, 2.6.3, 2.6.2, 2.6.1, 2.6.0, 2.5.4, 2.5.3, 2.5.2,
2.5.1, 2.5.0, 2.4.1, 2.4.0, 2.3.1, 2.3.0, 2.2.4, 2.2.3, 2.2.2, 2.2.1, 2.2.0,
2.1.0, 2.0.3, 2.0.2, 2.0.1, 2.0.0, 2.0.0-RC3, 2.0.0-RC2, 2.0.0-RC1, 1.3.4,
1.3.3, 1.3.2, 1.3.1, 1.3.0, 1.2.1, 1.2.0, 1.1.0, 1.0.1, 1.0, 1.0RC3, 1.0RC2,
1.0RC1, 0.10.1, 0.10.0, 0.9.2, 0.9.1, 0.9.0, 0.7.0, 0.5.1 [master repo] - Subspecs:
- AFNetworking/Serialization (3.1.0)
- AFNetworking/Security (3.1.0)
- AFNetworking/Reachability (3.1.0)
- AFNetworking/NSURLSession (3.1.0)
- AFNetworking/UIKit (3.1.0)
`
</code>
</pre>
說明CocoaPods確實(shí)能用了
用CocoaPods管理項(xiàng)目中的第三方框架
打開你的項(xiàng)目所在的那個(gè)文件夾,新建一個(gè)叫Podfile的文件
bogon:~ liyilin$ cd /Users/liyilin/Desktop/cocoapodsTest bogon:cocoapodsTest liyilin$ pod init
編輯Podfile文件
bogon:cocoapodsTest liyilin$ vim Podfile
終端顯示Podfile內(nèi)容如下
<pre>
<code>
`
Uncomment this line to define a global platform for your project
platform :ios, '9.0'
target 'cocoapodsTest' do
Comment this line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
Pods for cocoapodsTest
end
~
~
~
~
~
~
~
~
~
~
~
~
"Podfile" 10L, 257C
`
</code>
</pre>
其中注釋說如果使用Swift就得保留use_frameworks!
輸入i進(jìn)行編輯,終端顯示如下,注意最后一行變?yōu)?- INSERT --,此時(shí)可以編輯
<pre>
<code>
`
Uncomment this line to define a global platform for your project
platform :ios, '9.0'
target 'cocoapodsTest' do
Comment this line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
Pods for cocoapodsTest
end
~
~
~
~
~
~
~
~
~
~
~
~
-- INSERT --
`
</code>
</pre>
然后我根據(jù)搜索出的AFNetworking的信息,編輯Podfile文件如下:
(去掉了platform前面的#,插入了pod 'AFNetworking', '~> 3.1.0')
<pre>
<code>
`
Uncomment this line to define a global platform for your project
platform :ios, '9.0'
target 'cocoapodsTest' do
Comment this line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
pod 'AFNetworking', '~> 3.1.0'
Pods for cocoapodsTest
end
~
~
~
~
~
~
~
~
~
~
~
~
-- INSERT --
`
</code>
</pre>
然后點(diǎn)擊ESC,輸入:wq保存并退出
<pre>
<code>
`
Uncomment this line to define a global platform for your project
platform :ios, '9.0'
target 'cocoapodsTest' do
Comment this line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
pod 'AFNetworking', '~> 3.1.0'
Pods for cocoapodsTest
end
~
~
~
~
~
~
~
~
~
~
~
~
:wq
`
</code>
</pre>
接下來為項(xiàng)目導(dǎo)入第三方庫和相關(guān)依賴庫
bogon:cocoapodsTest liyilin$ pod install
終端提示導(dǎo)入成功,并提示你從此使用cocoapodsTest.xcworkspace這個(gè)文件來編寫項(xiàng)目
<pre>
<code>
`
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (3.1.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use cocoapodsTest.xcworkspace for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total
pod installed.
`
</code>
</pre>
使用cocoapodsTest.xcworkspace打開項(xiàng)目,目錄如下:

之后我參考了AFNetworking在github上的示例,試驗(yàn)了一下,證明AFNetworking導(dǎo)入成功而且能使用.
<pre>
<code>
`
import UIKit
import AFNetworking
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let manager = AFURLSessionManager.init(sessionConfiguration: configuration)
let URL = NSURL(string: "http://httpbin.org/get")
let request = NSURLRequest(URL: URL!)
let dataTask = manager.dataTaskWithRequest(request) { (response, responseObject, error) in
if error != nil
{
print("Error: \(error)")
}
else
{
print("response->\(response)");
print("responseObject->\(responseObject)")
}
}
dataTask.resume()
}
}
`
</code>
</pre>
調(diào)試窗口打印如下:

OVER