|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: CocoaPods公有仓库的创建 |
| 4 | +subtitle: 手把手教你创建 CocoaPods 公有仓库 |
| 5 | +date: 2017-03-08 |
| 6 | +author: BY |
| 7 | +header-img: img/post-bg-ios10.jpg |
| 8 | +catalog: true |
| 9 | +tags: |
| 10 | + - iOS |
| 11 | + - CocoaPods |
| 12 | + - Git |
| 13 | +--- |
| 14 | + |
| 15 | +> 手把手教你创建 CocoaPods 公有仓库 |
| 16 | +
|
| 17 | +当你有一个CocoaPods的公有仓库时,别人要使用你的框架时只需要在 `Podflie` 添加一句 `pod BYPhoneNumTF` 就能轻松的导入,是不是非常棒呢。 |
| 18 | + |
| 19 | +下面我们将一步步把我封装的这个简单的textFiled控件 [BYPhoneNumTF](https://github.com/qiubaiying/BYPhoneNumTF) 上传到Cocoapods 公有仓库。 |
| 20 | + |
| 21 | +#### 注册 CocoaPods 账号 |
| 22 | +想创建开源的Pod库,就要注册一个CocoaPods账号,我们使用终端注册, `email` 用你的 `GitHub` 邮箱 |
| 23 | + |
| 24 | + $ pod trunk register GitHub_email 'user_name' --verbose |
| 25 | + |
| 26 | +等终端出现下面文字,CocoaPods 会发一个`确认邮件`到你的邮箱上,登录你的邮箱进行确认。 |
| 27 | + |
| 28 | + [!] Please verify the session by clicking the link in the verification email that has been sent to you_email@163.com |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +注册成功! |
| 33 | + |
| 34 | +确认后再终端输入 |
| 35 | + |
| 36 | + pod trunk me |
| 37 | + |
| 38 | +可以看到你的注册信息 |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +#### 创建Git仓库 |
| 43 | + |
| 44 | +在 [GitHub](https://github.com) 上创建一个公开项目,项目中必须包含这几个文件 |
| 45 | + |
| 46 | +- `LICENSE`:开源许可证 |
| 47 | +- `README.md`:仓库说明 |
| 48 | +- 你的代码 |
| 49 | +- `BYPhoneNumTF.podspec`: CocoaPods 的描述文件,这个文件**非常重要** |
| 50 | + |
| 51 | +如下图: |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +`BYPhoneNumTF` 文件夹下是我存放代码的地方 |
| 56 | + |
| 57 | +`BYPhoneNumTF_Demo` 是代码使用样例(不是必须的) |
| 58 | + |
| 59 | + |
| 60 | +#### 创建`.podspec` |
| 61 | +`.podspec` 是用 Ruby 的配置文件,描述你项目的信息。 |
| 62 | + |
| 63 | +在你的仓库目录下,使用终端命令创建 |
| 64 | + |
| 65 | + $ pod spec create BYPhoneNumTF |
| 66 | + |
| 67 | +这时就会在你的仓库下生成 `BYPhoneNumTF.podspec` 文件 |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +修改里面的配置就可以发布了~当然,没这么简单。 |
| 72 | + |
| 73 | +配置文件中的注释很多,而且很多配置都不是必须的,写多了等下验证还不让过~ |
| 74 | + |
| 75 | +so~**强烈建议**,直接拷贝下面的主要配置进行修改 |
| 76 | + |
| 77 | +```ruby |
| 78 | +Pod::Spec.new do |s| |
| 79 | + s.name = "BYPhoneNumTF" # 项目名称 |
| 80 | + s.version = "1.0.0" # 版本号 与 你仓库的 标签号 对应 |
| 81 | + s.license = "MIT" # 开源证书 |
| 82 | + s.summary = "A delightful TextField of PhoneNumber" # 项目简介 |
| 83 | + |
| 84 | + s.homepage = "https://github.com/qiubaiying/BYPhoneNumTF" # 你的主页 |
| 85 | + s.source = { :git => "https://github.com/qiubaiying/BYPhoneNumTF.git", :tag => "#{s.version}" }#你的仓库地址,不能用SSH地址 |
| 86 | + s.source_files = "BYPhoneNumTF/*.{h,m}" # 你代码的位置, BYPhoneNumTF/*.{h,m} 表示 BYPhoneNumTF 文件夹下所有的.h和.m文件 |
| 87 | + s.requires_arc = true # 是否启用ARC |
| 88 | + s.platform = :ios, "7.0" #平台及支持的最低版本 |
| 89 | + s.frameworks = "UIKit", "Foundation" #支持的框架 |
| 90 | + # s.dependency = "AFNetworking" # 依赖库 |
| 91 | + |
| 92 | + # User |
| 93 | + s.author = { "BY" => "qiubaiyingios@163.com" } # 作者信息 |
| 94 | + s.social_media_url = "http://qiubaiying.github.io" # 个人主页 |
| 95 | + |
| 96 | +end |
| 97 | +``` |
| 98 | +最最关键的步骤的到了,验证 `.podspec` 文件的格式是否正确, |
| 99 | + |
| 100 | + $ pod lib lint |
| 101 | + |
| 102 | +验证会出现成功出现 |
| 103 | + |
| 104 | + -> BYPhoneNumTF (1.0.0) |
| 105 | + |
| 106 | + BYPhoneNumTF passed validation. |
| 107 | + |
| 108 | +但是很多情况没这么顺利,比如: |
| 109 | + |
| 110 | + -> BYPhoneNumTF (1.0.0) |
| 111 | + - WARN | url: There was a problem validating the URL http://qiubaiying.github.io. |
| 112 | + |
| 113 | + [!] BYPhoneNumTF did not pass validation, due to 1 warning (but you can use `--allow-warnings` to ignore it) and all results apply only to public specs, but you can use `--private` to ignore them if linting the specification for a private pod. |
| 114 | + [!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a `.swift-version` file to set the version for your Pod. For example to use Swift 2.3, run: |
| 115 | + `echo "2.3" > .swift-version`. |
| 116 | + You can use the `--no-clean` option to inspect any issue. |
| 117 | + |
| 118 | +提示我们需要加`--allow-warnings`这么一句话,命令改为 |
| 119 | + |
| 120 | + $ pod lib lint --allow-warnings |
| 121 | + |
| 122 | +若还是提示什么`'echo "2.3" > .swift-version'`的,就加这么一个东西。 |
| 123 | + |
| 124 | + $ echo "2.3" > .swift-version |
| 125 | +然后在进行验证,这是应该就可以了。若还是不行,回到配置文件中检查有没有写错配置信息~ |
| 126 | + |
| 127 | +#### 给仓库打标签 |
| 128 | + |
| 129 | +验证成功后,将仓库提交到远程,然后给仓库打上标签并将标签也推送到远程。 |
| 130 | + |
| 131 | +标签相当于将你的仓库的一个压缩包,用于稳定存储当前版本。标签号与你在 `s.version = "1.0.0"`的版本号一致 `1.0.0` |
| 132 | + |
| 133 | + 创建标签 |
| 134 | + $ git tag -a 1.0.0 -m '标签说明' |
| 135 | + 推送到远程 |
| 136 | + $ git push origin --tags |
| 137 | + |
| 138 | +#### 发布`.podspec` |
| 139 | + |
| 140 | +最后一步,发布项目的描述的文件 `BYPhoneNumTF.podspec` |
| 141 | + |
| 142 | +在仓库目录下执行 |
| 143 | + |
| 144 | + pod trunk push BYPhoneNumTF.podspec |
| 145 | + |
| 146 | +将你的 `BYPhoneNumTF.podspec` 发布到公有的speecs上,这一步其实做了很多操作,包括 |
| 147 | + |
| 148 | +1. 更新本地 pods 库 `~/.cocoaPods.repo/master` |
| 149 | +- 验证`.podspec`格式是否正确 |
| 150 | +- 将 `.podspec` 文件转成 JSON 格式 |
| 151 | +- 对 `master` 仓库 进行合并、提交.[master仓库地址](https://github.com/CocoaPods/Specs) |
| 152 | + |
| 153 | + |
| 154 | +成功后将会出现下列信息: |
| 155 | + |
| 156 | + Updating spec repo `master` |
| 157 | + Validating podspec |
| 158 | + -> BYPhoneNumTF (1.0.0) |
| 159 | + |
| 160 | + Updating spec repo `master` |
| 161 | + |
| 162 | + -------------------------------------------------------------------------------- |
| 163 | + 🎉 Congrats |
| 164 | + |
| 165 | + 🚀 BYPhoneNumTF (1.0.0) successfully published |
| 166 | + 📅 March 7th, 01:39 |
| 167 | + 🌎 https://cocoapods.org/pods/BYPhoneNumTF |
| 168 | + 👍 Tell your friends! |
| 169 | + |
| 170 | +说明发布成功,你就可以通过上面的URL: <https://cocoapods.org/pods/BYPhoneNumTF> 进入的Pods查看自己的仓库信息了. |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | +#### 使用仓库 |
| 175 | + |
| 176 | +发布到Cocoapods后,在终端更新本地pods仓库信息 |
| 177 | + |
| 178 | + $ pod setup |
| 179 | + |
| 180 | +查询仓库 |
| 181 | + |
| 182 | + $ pod search BYPhoneNumTF |
| 183 | +--- |
| 184 | + -> BYPhoneNumTF (1.0.0) |
| 185 | + A delightful TextField of PhoneNumber |
| 186 | + pod 'BYPhoneNumTF', '~> 1.0.0' |
| 187 | + - Homepage: https://github.com/qiubaiying/BYPhoneNumTF |
| 188 | + - Source: https://github.com/qiubaiying/BYPhoneNumTF.git |
| 189 | + - Versions: 1.0.0, 0.0.1 [BYPhoneNumTF repo] |
| 190 | + (END) |
| 191 | + |
| 192 | +若出现仓库信息说明已经成功了,这时候你就可以在 `Podfile` 添加、使用自己的仓库了 `pod 'BYPhoneNumTF', '~> 1.0.0'` |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | +#### 更新维护 |
| 197 | + |
| 198 | +当你的代码更新维护后,就需要重写发布,流程是: |
| 199 | + |
| 200 | +- 更新`BYPhoneNumTF.podspec`中的版本号 |
| 201 | +- 打上标签推送远程 |
| 202 | +- `pod trunk push BYPhoneNumTF.podspec` 推送到pods仓库 |
| 203 | + |
| 204 | +更新后你就可以在 [CocoaPods Master Repo](https://github.com/CocoaPods/Specs) 仓库上看到自己的提交记录了。 |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + |
0 commit comments