CocoaPods的原理
CocoaPods的原理是将所有的依赖库都放到另一个名为Pods的项目中,然后让主项目依赖Pods项目,这样,源码管理工作都从主项目移到了Pods项目中。Pods项目最终会编译成一个名为libPods.a的文件,主项目只需要依赖这个.a文件即可。
CocoaPods的安装
- 打开终端 - 移除ruby的默认源:gem sources --remove https://rubygems.org/
- 添加新的源:gem sources -a https://ruby.taobao.org/ 查看是否已经替换成功:gem sources -l
- 安装CocoaPods:sudo gem install -n /usr/local/bin cocoapods; 后输入mac用户的密码进行安装,成功后输入:pod setup
- 更新gem:sudo gem update --system
CocoaPods-引入第三方库
- cd到工程目录后新建Podfile:vim Podfile
- 编辑Podfile导入你想要的第三方库和一些配置
- 提交:pod install
看下Podfile的样例:
platform :ios, '8.0' use_frameworks! target '你的工程名字' do pod '第三方库', '版本号' end
导入第三方库时 版本号控制:
- pod '第三方库' :不指定版本号,表示每次都是用最新的版本
- pod '第三方库', '2.0' :只使用2.0的版本
- pod '第三方库', '> 2.0' :使用大于2.0的版本
- pod '第三方库', '>= 2.0' :使用大于等于2.0的版本
- pod '第三方库', '< 2.0' :使用小于2.0的版本
- pod '第三方库', '<= 2.0' :使用小于等于2.0的版本
- pod '第三方库', '~> 2.0.1' :从指定版本到倒数第二位版本号升1为止,如:~> 2.0.1 是指 2.0.1 <= 版本号 < 2.1.0