注:本文是在Mac电脑中已经安装完成好Ruby环境的前提下进行的。xcode版本7.0.1
1.修改Ruby镜像
因为cocoapods.org已经被屏蔽了,使用源网站的话要翻过去。所有为了保证下载的速度,需要切换Ruby镜像。
先删除cocoapods.org镜像:
$gem sources –remove https://rubygems.org/
$gem sources –a https://ruby.taobao.org/ (注:网上很多教程使用的是http://ruby.taobao.org/,但是淘宝已经停用了http服务的协议,需要使用https协议代替)
$gem sources –l (用该命令查看ruby镜像是否是taobao.com)
显示:
*** CURRENT SOURCES ***
https://ruby.taobao.org/
2.安装CocoaPods
$sudo gem install cocoapods
输入完密码后即开始安装。
3.验证CocoaPods是否安装成功
$ pod
localhost:~ fudong$ pod
Usage:
$ pod COMMAND
CocoaPods, the Cocoa library package manager.
Commands:
+ cache Manipulate the CocoaPods cache
+ init Generate a Podfile for the current directory.
+ install Install project dependencies to Podfile.lock versions
+ ipc Inter-process communication
+ lib Develop pods
+ list List pods
+ outdated Show outdated project dependencies
+ plugins Show available CocoaPods plugins
+ repo Manage spec-repositories
+ search Search for pods.
+ setup Setup the CocoaPods environment
+ spec Manage pod specs
+ trunk Interact with the CocoaPods API (e.g. publishing new specs)
+ try Try a Pod!
+ update Update outdated project dependencies and create new Podfile.lock
Options:
--silent Show nothing
--version Show the version of the tool
--verbose Show more debugging information
--no-ansi Show output without ANSI codes
--help Show help banner of specified command
localhost:~ fudong$
localhost:~ fudong$ pod search "AFNetworking"
第一次搜索的时候会出现Setting up CocoaPods master repo,然后终端会卡住,这是正常情况,说明在下载一些信息,打开另一个终端输入:
localhost:~ fudong$ cd ~/.cocoapods
localhost:.cocoapods fudong$ du -sh *
279M repos
localhost:.cocoapods fudong$
localhost:~ fudong$ pod search "AFNetworking"
-> AFNetworking (3.0.0-beta.2)
A delightful iOS and OS X networking framework.
pod 'AFNetworking', '~> 3.0.0-beta.2'
- Homepage: https://github.com/AFNetworking/AFNetworking
- Source: https://github.com/AFNetworking/AFNetworking.git
- Versions: 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.0.0-beta.2)
- AFNetworking/Security (3.0.0-beta.2)
- AFNetworking/Reachability (3.0.0-beta.2)
- AFNetworking/NSURLSession (3.0.0-beta.2)
- AFNetworking/UIKit (3.0.0-beta.2)
-> AFNetworking+AutoRetry (0.0.5)
Auto Retries for AFNetworking requests
pod 'AFNetworking+AutoRetry', '~> 0.0.5'
- Homepage: https://github.com/shaioz/AFNetworking-AutoRetry
- Source: https://github.com/shaioz/AFNetworking-AutoRetry.git
- Versions: 0.0.5, 0.0.4, 0.0.3, 0.0.2, 0.0.1 [master repo]
localhost:~ fudong$ cd Desktop/FDCocoaPods/
localhost:FDCocoaPods fudong$ vi Podfile
localhost:FDCocoaPods fudong$
pod "AFNetworking","~> 2.0"
localhost:FDCocoaPods fudong$ pod install
Updating local specs repositories
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (2.6.3)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `FDCocoaPods.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total
pod installed.
localhost:FDCocoaPods fudong$
6.在xcode中添加配置
在项目配置中找到Build Settings-->User Header Search Paths中双击添加一个新的项目 添加上“#(PODS_ROOT)”,第二个选项选中recursive
添加完成之后到文件中就可以导入AFNetworking头文件了。
至此,CocoaPods安装与使用结束。