感悟
费了两天晚上的功夫安装上了,一边玩守望先锋 ,一边看CocoaPods下载的进度.300多兆的文件,下载了3个多小时,既然咱不是特别聪明,咱就多努力,多一点坚持慢慢来吧.整个安装过程中终端的内容如下(可以略过,对我有点用):
<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 matching
AFNetworking`
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 是开发 OS X 和 iOS 应用程序的一个第三方库的依赖管理工具。利用 CocoaPods,可以定义自己的依赖关系 (称作 pods),并且随着时间的变化,以及在整个开发环境中对第三方库的版本管理非常方便。
CocoaPods 背后的理念主要体现在两个方面。
首先,在工程中引入第三方代码会涉及到许多内容。对开发者来说,工程文件的配置会让人很沮丧。在配置 build phases 和 linker flags 过程中,会引起许多人为因素的错误。CocoaPods 简化了这一切,它能够自动配置编译选项。
通过 CocoaPods,可以很方便的查找到新的第三方库。
CocoaPods是用 Ruby 写的,想要使用它首先要有Ruby环境.
RubyGems简称gems,是一个用于对Ruby组件进行打包的Ruby打包系统
个人总结的安装步骤(看了好多相关的博客)
将ruby的默认源替换为淘宝的RubyGems镜像
因为国内网络原因,导致rubygems.org存放在亚马逊上面的资源容易连接失败
gem sources --remove https://rubygems.org/ gem sources -a https://ruby.taobao.org/
查看此时的源
bogon:~ liyilin$ gem source -l
输出结果
*** CURRENT SOURCES *** https://ruby.taobao.org/
之后就可以更新Mac的gem,gem是管理Ruby库和程序的标准包
bogon:~ liyilin$ sudo gem update --system
终端提示
Updating rubygems-update ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/update_rubygems
上网搜索后得知
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
终端提示貌似没有可以更新的内容
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
上网搜索后得知Cocoapods在将它的信息下载到 ~/.cocoapods里
然后我就前往这个文件夹,不断用快捷键预览它的大小
下载速度太慢了,我想先去玩几把守望先锋吧,也不能干等着吧 玩了五局定级赛(2胜3负),还没下载完.那我去搓几把炉石吧.
炉石任务正刷着的时候,终端显示下载完了,但是还有一个警告
Setup completed [!] Unable to find a pod with name, author, summary, or description matching 'AFNetworking'
上网搜索后得知要删除一个文件
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>
应该是搜索出了与AFNetworking相关的内容
我再一次重新搜索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确实能用了
用CocoaPods管理项目中的第三方框架
打开你的项目所在的那个文件夹,新建一个叫Podfile的文件
bogon:~ liyilin$ cd /Users/liyilin/Desktop/cocoapodsTest bogon:cocoapodsTest liyilin$ pod init
编辑Podfile文件
bogon:cocoapodsTest liyilin$ vim Podfile
终端显示Podfile内容如下
<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进行编辑,终端显示如下,注意最后一行变为-- INSERT --,此时可以编辑
<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>
然后我根据搜索出的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>
然后点击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>
接下来为项目导入第三方库和相关依赖库
bogon:cocoapodsTest liyilin$ pod install
终端提示导入成功,并提示你从此使用cocoapodsTest.xcworkspace这个文件来编写项目
<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打开项目,目录如下:
之后我参考了AFNetworking在github上的示例,试验了一下,证明AFNetworking导入成功而且能使用.
<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>
调试窗口打印如下:
OVER