|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: CocoaPods 安装和使用 |
| 4 | +subtitle: 安装时间 2017/04/13, 环境macOS 12.10.1, cocoapod版本 1.2.1 |
| 5 | +date: 2017-04-13 |
| 6 | +author: BY |
| 7 | +header-img: img/post-bg-hacker.jpg |
| 8 | +catalog: true |
| 9 | +tags: |
| 10 | + - iOS |
| 11 | + - Xcode |
| 12 | + - Cocopods |
| 13 | + - ruby |
| 14 | +--- |
| 15 | + |
| 16 | +# 前言 |
| 17 | + |
| 18 | +最近换了新机器,重新搭建了开发环境,其中当然包括 **CocoaPods**。 |
| 19 | + |
| 20 | +装完顺便更新下 **CocoaPods** 安装文档。 |
| 21 | + |
| 22 | + |
| 23 | +# 正文 |
| 24 | + |
| 25 | + |
| 26 | +## 安装 |
| 27 | + |
| 28 | +CocoaPods是用Ruby实现的,要想使用它首先需要有Ruby的环境。 |
| 29 | + |
| 30 | +#### 升级ruby |
| 31 | + |
| 32 | + 查看ruby版本 |
| 33 | + $ ruby -v |
| 34 | + |
| 35 | + ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin16] |
| 36 | + |
| 37 | +CocoaPods需要**2.2.2**版本及以上的,我们先升级ruby。 |
| 38 | + |
| 39 | +使用**rvm**安装ruby |
| 40 | + |
| 41 | + curl -L get.rvm.io | bash -s stable |
| 42 | + source ~/.bashrc |
| 43 | + source ~/.bash_profile |
| 44 | + |
| 45 | +Ruby的软件源使用亚马逊的云服务被墙了,切换国内的 **ruby-china源** (<https://ruby.taobao.org/>已经停止维护,详情[查看公告](https://ruby.taobao.org/)): |
| 46 | + |
| 47 | + $ gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/ |
| 48 | + $ gem sources -l |
| 49 | + *** CURRENT SOURCES *** |
| 50 | + |
| 51 | + https://gems.ruby-china.org |
| 52 | +到此ruby升级完毕 |
| 53 | + |
| 54 | +#### 安装CocoaPods |
| 55 | + |
| 56 | +1. 安装 |
| 57 | + |
| 58 | + sudo gem install -n /usr/local/bin cocoapods |
| 59 | +2. 升级版本库 |
| 60 | + |
| 61 | + pod setup |
| 62 | + |
| 63 | + 这里需要下载版本库(非常庞大),需要等很久 |
| 64 | + |
| 65 | + Receiving objects: 72% (865815/1197150), 150.07 MiB | 190.00 KiB/s |
| 66 | + |
| 67 | + 或者直接从其他装有cocoapod的电脑中拷贝`~/.cocoapods`到你的用户目录,然后再 `pod setup`会节省不少时间 |
| 68 | + |
| 69 | +# 使用 |
| 70 | + |
| 71 | +#### 创建 `podfile` 文件 |
| 72 | + |
| 73 | +绝大多数人创建`podfile`都是用 `vim Podfile` 命令 |
| 74 | + |
| 75 | +其实pod 已经提供了创建 `podfile` 文件的命令,在工程目录下 |
| 76 | + |
| 77 | + pod init |
| 78 | + |
| 79 | +将会自动生成 `podfile` 文件,并且为你写好了格式,稍做修改就能使用 |
| 80 | + |
| 81 | +``` |
| 82 | +# Uncomment the next line to define a global platform for your project |
| 83 | +# platform :ios, '9.0' |
| 84 | +
|
| 85 | +target 'projectName' do |
| 86 | + # Comment the next line if you're not using Swift and don't want to use dynamic frameworks |
| 87 | + use_frameworks! |
| 88 | +
|
| 89 | + # Pods for projectName |
| 90 | +
|
| 91 | + target 'projectNameTests' do |
| 92 | + inherit! :search_paths |
| 93 | + # Pods for testing |
| 94 | + end |
| 95 | +
|
| 96 | + target 'projectNameUITests' do |
| 97 | + inherit! :search_paths |
| 98 | + # Pods for testing |
| 99 | + end |
| 100 | +
|
| 101 | +end |
| 102 | +``` |
| 103 | + |
| 104 | +其中的 |
| 105 | + |
| 106 | +``` |
| 107 | +target 'projectNameTests' do |
| 108 | + inherit! :search_paths |
| 109 | + # Pods for testing |
| 110 | + end |
| 111 | +
|
| 112 | + target 'projectNameUITests' do |
| 113 | + inherit! :search_paths |
| 114 | + # Pods for testing |
| 115 | + end |
| 116 | +``` |
| 117 | + |
| 118 | +是指定在单元测试和UI测试时导入的测试框架,若没有使用测试框架可以删除。 |
| 119 | + |
| 120 | +修改iOS版本,添加`Alamofire`库 |
| 121 | + |
| 122 | +``` |
| 123 | +# Uncomment the next line to define a global platform for your project |
| 124 | +# platform :ios, '8.0' |
| 125 | +
|
| 126 | +target 'projectName' do |
| 127 | + # Comment the next line if you're not using Swift and don't want to use dynamic frameworks |
| 128 | + use_frameworks! |
| 129 | +
|
| 130 | + # Pods for projectName |
| 131 | + |
| 132 | + pod 'Alamofire', '~> 4.4' |
| 133 | +
|
| 134 | +end |
| 135 | +``` |
| 136 | + |
| 137 | +#### 加载代码库 |
| 138 | + |
| 139 | +使用下面的命令,直接在本地版本库中查找对应的代码库信息,不升级版本库,节省时间 |
| 140 | + |
| 141 | + pod install --verbose --no-repo-update |
| 142 | + |
| 143 | +若找不到库,再使用下面的命令 |
| 144 | + |
| 145 | + pod install |
| 146 | + |
| 147 | +#### 版本号 |
| 148 | + |
| 149 | +对版本号的操作除了指定与不指定,你还可以做其他操作: |
| 150 | + |
| 151 | +- `\>0.1` 高于0.1的任何版本 |
| 152 | +- `\>=0.1` 版本0.1和任何更高版本 |
| 153 | +- `<0.1` 低于0.1的任何版本 |
| 154 | +- `<=0.1` 版本0.1和任何较低的版本 |
| 155 | +- `〜>0.1.2` 版本 0.1.2的版本到0.2 ,不包括0.2。 |
| 156 | +这个基于你指定的版本号的最后一个部分。这个例子等效于>= 0.1.2并且 <0.2.0,并且始终是你指定范围内的最新版本 |
| 157 | + |
| 158 | +# 结语 |
| 159 | + |
| 160 | +关于**CocoaPods**的安装和使用就这样简单的介绍完了,至于更多使用的方法(平时也用不到~)你可以用下面命令查看 |
| 161 | + |
| 162 | + $ pod |
| 163 | + |
| 164 | +若感兴趣,也可以看看我的这两篇博客 |
| 165 | + |
| 166 | +- [CocoaPods公有仓库的创建](http://qiubaiying.top/2017/03/08/CocoaPods%E5%85%AC%E6%9C%89%E4%BB%93%E5%BA%93%E7%9A%84%E5%88%9B%E5%BB%BA/) |
| 167 | +- [CocoaPods私有仓库的创建](http://qiubaiying.top/2017/03/10/CocoaPods%E7%A7%81%E6%9C%89%E4%BB%93%E5%BA%93%E7%9A%84%E5%88%9B%E5%BB%BA/) |
| 168 | + |
0 commit comments