测试开发

CocoaPods安装和使用教程

LensNews

一、创建Xcode工程项目

1、首先是要创建一个iOS的工程项目,如下图创建的guest工程项目:

二、在线安装cocoapods

1、关于cocoapods介绍
CocoaPods是iOS最常用的第三方类库管理工具,绝大部分有名的开源类库都支持CocoaPods。

2、在线安装
CocoaPods是用Ruby实现的,要想使用它首先需要有Ruby的环境。幸运的是OS X系统默认已经可以运行Ruby了,因此我们只需执行以下命令:
wanghaifengdeiMac:guest $ sudo gem install cocoapods
Password:
Fetching: nap-1.1.0.gem (100%)
Successfully installed nap-1.1.0
Fetching: cocoapods-core-0.39.0.gem (100%)
Successfully installed cocoapods-core-0.39.0
Fetching: xcodeproj-0.28.2.gem (100%)
Successfully installed xcodeproj-0.28.2
Fetching: cocoapods-downloader-0.9.3.gem (100%)
Successfully installed cocoapods-downloader-0.9.3
Fetching: cocoapods-search-0.1.0.gem (100%)
Successfully installed cocoapods-search-0.1.0
Fetching: cocoapods-stats-0.6.2.gem (100%)
Successfully installed cocoapods-stats-0.6.2
Fetching: cocoapods-try-0.5.1.gem (100%)
Successfully installed cocoapods-try-0.5.1
Fetching: cocoapods-trunk-0.6.4.gem (100%)
Successfully installed cocoapods-trunk-0.6.4
Fetching: molinillo-0.4.2.gem (100%)
Successfully installed molinillo-0.4.2
Fetching: cocoapods-0.39.0.gem (100%)

如果要卸载cocoapods,执行下面的命令
wanghaifengdeiMac:guest $ sudo gem uninstall cocoapods

Select gem to uninstall:
1. cocoapods-0.38.2
2. cocoapods-0.39.0
3. cocoapods-1.0.0.beta.2
4. All versions
Successfully uninstalled cocoapods-0.38.2
Successfully uninstalled cocoapods-0.39.0
Remove executables:
pod, sandbox-pod

in addition to the gem? [Yn] Y
Removing pod
Removing sandbox-pod
Successfully uninstalled cocoapods-1.0.0.beta.2

3、如果Ruby环境不够新, 可能需要更新以下:
wanghaifengdeiMac:guest $ sudo gem update --system
Password:
Updating rubygems-update
Fetching: rubygems-update-2.5.1.gem (100%)
Successfully installed rubygems-update-2.5.1
Parsing documentation for rubygems-update-2.5.1
Installing ri documentation for rubygems-update-2.5.1
Installing darkfish documentation for rubygems-update-2.5.1
Installing RubyGems 2.5.1
RubyGems 2.5.1 installed
Parsing documentation for rubygems-2.5.1
Installing ri documentation for rubygems-2.5.1

4、至此安装就完成来,我们可以尝试搜索一个第三方类库:
wanghaifengdeiMac:guest $ pod search AFNetworking
-> AFNetworking (3.0.4)
A delightful iOS and OS X networking framework.
pod 'AFNetworking', '~> 3.0.4'
- Homepage: https://github.com/AFNetworking/AFNetworking
- Source: https://github.com/AFNetworking/AFNetworking.git
- Versions: 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.0.4)
- AFNetworking/Security (3.0.4)
- AFNetworking/Reachability (3.0.4)
- AFNetworking/NSURLSession (3.0.4)
- AFNetworking/UIKit (3.0.4)

5、使用CocoaPods的第一步,是在当前项目下,新建一个Podfile文件
wanghaifengdeiMac:guest $ touch Podfile

6、然后利用vim命令打开Podfile文件进行编辑,加入你想要使用的类库,格式如下
platform:ios, '9.0'
use_frameworks!
target "guest" do
pod 'Alamofire', '~> 1.2.2'
end

7、如果你是拷贝别人的项目,或是一个很久没有打开过的项目,可能需要先执行一下
wanghaifengdeiMac:guest $ pod update
Update all pods
Updating local specs repositories
Analyzing dependencies
Downloading dependencies
Using Alamofire (1.2.3)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

8、最后一步执行命令,如下,说明已经安装成功了
wanghaifengdeiMac:guest chinaskin$ pod install
Updating local specs repositories
Analyzing dependencies
Downloading dependencies
Installing Alamofire (1.2.3)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `guest.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

9、在guest目录下,就可以看到guest.xcworkspace文件了。

三、问题处理

1、如果上面因为权限问题安装失败,必须每次都要删除
rm -rf /Users/loginnanme/Library/Caches/CocoaPods/

2、如果出现[!] The dependency Alamofire (~> 1.2.2) is not used in any concrete target的错误提示,就将Podfile改为
添加:target "guest” do 到Profile文件中
其中guest为你的工程名

3、关于Podfile文件,第三方库版本号的各种写法
pod ‘AFNetworking’ //不显式指定依赖库版本,表示每次都获取最新版本
pod ‘AFNetworking’, ‘2.0’ //只使用2.0版本
pod ‘AFNetworking’, ‘>2.0′ //使用高于2.0的版本
pod ‘AFNetworking’, ‘>=2.0′ //使用大于或等于2.0的版本
pod ‘AFNetworking’, ‘<2.0′ //使用小于2.0的版本
pod ‘AFNetworking’, ‘<=2.0′ //使用小于或等于2.0的版本 pod ‘AFNetworking’, ‘~>0.1.2′ //使用大于等于0.1.2但小于0.2的版本,相当于>=0.1.2并且<0.2.0 pod ‘AFNetworking’, ‘~>0.1′ //使用大于等于0.1但小于1.0的版本
pod ‘AFNetworking’, ‘~>0′ //高于0的版本,写这个限制和什么都不写是一个效果,都表示使用最新版本

4、一些有名的第三方类库
pod 'Alamofire'
pod 'SwiftyJSON'
pod 'SDWebImage'
pod 'SCLAlertView'
pod 'RDVTabBarController'
pod 'TTTAttributedLabel'
pod 'ChameleonFramework'
pod 'MBProgressHUD'
pod 'CWPopup'
pod 'TTTAttributedLabel'
pod 'Reachability'

5、示例

(2)

本文由 小蜜蜂信息网 作者:admin 发表,转载请注明来源!

关键词:
LensNews

热评文章

发表评论