正在加载今日诗词....
5 min read

iOS组件化(基于Cocoapods)

iOS组件化(基于Cocoapods)
  • 组件化前提先确保已经 安装Cocoapod
  • 组件化前提: 先将 git权限问题搞明白
  • 组件库实际就是一个git代码库
  • 私有源库实际也是一个git代码库, 不过里面只有以.podspec结尾的文件

注册 trunk

pod trunk register xxxx@gmail.com manajay

创建本地私有库

  • 创建 pod lib create xxx
  • Class 中添加代码文件
  • Assets 中添加图片等资源 : 注意获取资源使用 [NSBundle bundleForClass:self], 注意图片要全名, 如果使用了资源,那么 podfile文件中添加描述use_frameworks!

更改本次的版本描述

  • podspec文件
  • 注意 version 一定要对应 git tag
  • source_files
  • dependency
  • resource_bundles

验证本地的组件库是否有效

pod lib lint

测试一

pod lib lint --no-clean LJNetwork.podspec

 -> LJNetwork (1.0.0)
    - ERROR | attributes: Missing required attribute `summary`.
    - ERROR | [iOS] unknown: Encountered an unknown error (The `LJNetwork` pod failed to validate due to 1 error:
    - ERROR | attributes: Missing required attribute `summary`.

) during validation.

Pods workspace available at `/var/folders/86/kvr1rbk53zgb0xpkljyv2vn80000gn/T/CocoaPods-Lint-20180308-31173-4h194x-LJNetwork/App.xcworkspace` for inspection.

[!] LJNetwork did not pass validation, due to 2 errors.

修改 summary
测试二

pod lib lint --no-clean LJNetwork.podspec

 -> LJNetwork (1.0.0)
    - WARN  | [iOS] swift: The validator used Swift 3.2 by default because no Swift version was specified. To specify a Swift version during validation, add the `swift_version` attribute in your podspec. Note that usage of the `--swift-version` parameter or a `.swift-version` file is now deprecated.

Pods workspace available at `/var/folders/86/kvr1rbk53zgb0xpkljyv2vn80000gn/T/CocoaPods-Lint-20180308-31243-i7bisy-LJNetwork/App.xcworkspace` for inspection.
[!] LJNetwork did not pass validation, due to 1 warning (but you can use `--allow-warnings` to ignore it).

注意如果出现WARN字样 可以使用pod repo push XXXSpec xxx.podspec --allow-warnings 忽略警告

测试三

pod lib lint --no-clean --allow-warnings  LJNetwork.podspec

 -> LJNetwork

 -> LJNetwork (1.0.0)
    - WARN  | xcodebuild:  ReachabilitySwift/Reachability/Reachability.swift:95:42: warning: plaform condition appears to be testing for simulator environment; use 'targetEnvironment(simulator)' instead

Pods workspace available at `/var/folders/86/kvr1rbk53zgb0xpkljyv2vn80000gn/T/CocoaPods-Lint-20180308-32416-1hp85wj-LJNetwork/App.xcworkspace` for inspection.

LJNetwork passed validation.
  • 出现ERROR的话, 则必须解决

创建自己的私有远程管理库

  • 创建一个私有的远程库
  • 本地添加私有远程库的索引库 pod repo add XXXSpec https://git.XXX/XXXSpec.git
  • 本地的索引库检查 pod repo ,除了'https://github.com/CocoaPods/Specs.git' 还有自己的

关联远程库, 设置tag 并推送到 远程库

git tag xxx
git push -u origin master // 首次推送
git push --tags

如果tag和私有库的version不匹配, 注意删除tag,修改正确.

推送组件的 podspec文件到自己的私有远程管理库

  • cd 到 组件podspec文件目录下
  • pod repo push XXXSpec xxx.podspec 推送该组件的podspec文件
  • 远程私有管理库中查看是否将xxx.podspec文件推送到了远程
pod trunk --allow-warnings push LJNetwork.podspec
Updating spec repo `master`
Validating podspec
 -> LJNetwork (1.0.0)
    - WARN  | xcodebuild:  ReachabilitySwift/Reachability/Reachability.swift:95:42: warning: plaform condition appears to be testing for simulator environment; use 'targetEnvironment(simulator)' instead

Updating spec repo `master`

--------------------------------------------------------------------------------
 🎉  Congrats

 🚀  LJNetwork (1.0.0) successfully published
 📅  March 8th, 02:04
 🌎  https://cocoapods.org/pods/LJNetwork
 👍  Tell your friends!

如果发现有授权失败的问题, 很有可能是你的私有源库XXXSpec的授权有问题, 最好检查一下.

私有库在Mac的地址为 ~/.cocoapods/repos/ ,在这个目录下查找XXXSpec即可.

而这里说的授权是根据访问方式确定的. 分为: HttpsSSH访问

如果是Https:

最好查看一下git全局配置 git config --global --list

配置全局的用户名和 密码

git config --global user.name "username"
git config --global user.email username@copany.com

然后再确认一下私有源库的config配置

cat ~/.cocoapods/repos/XXXSpec/.git/config

确保 url 的路径要有username. 例如: url = https://username@xxxx.git

如果是SSH

~/.ssh 的路径下, 有当前私有库的私钥文件(一般以 rsa 结尾). 具体可以询问运维同学, 这里就不讲了.

使用 私有组件

  • 首次使用组件, 先确保添加了私有仓库的管理源地址 pod repo add XXSpec https://xxx/XXSpec.git // 注意 这里因为是私有库 所以需要权限来获取私有管理库的资源
  • 搜索自己的私有库组件 pod search
  • 没有使用Cocoapods的要先在 项目根目录创建 Podfile文件 执行 pod init
  • 编辑Podfile文件
# 表示先去找私有,再找公有的 master
source 'https://git.XXX/XXXSpec.git'
source 'https://github.com/CocoaPods/Specs.git'

pod 'xxx'
  • 打开生成的xxx.xcworkspace文件

组件升级

  • 私有源远程库的升级
# 更新 podspec索引文件
pod repo update
  • 项目中的组件升级 , 推荐指定 pod 升级
# 升级所有组件 , 子命令 --no-repo-update为 不更新索引
pod update --no-repo-update
# 指定某个pod 升级 , 不更新索引
pod update xxxPod --no-repo-update

<补充> Cocoapods公有仓库推送

登录

pod trunk register mailname@gmail.com 'username' --description='xxxx'
[!] Please verify the session by clicking the link in the verification email that has been sent to manajay.dlj@gmail.com

用于已有用户的登录 获取session

  • 检验登录状态 pod trunk me
  - Name:     Manajay
  - Email:    manajay.dlj@gmail.com
  - Since:    xxxx xxth, xxxx xx:xx
  - Pods:
    - NJAlertView
    - LJNetwork
  - Sessions:
    - August xxth, xxxx xx:xx  - December xxth, xxx xx:xx. IP: xxxxx  Description: custom alertView
    - June xxth, xxxx xx:xx   -  November xxth, xxxx xx:xx. IP: xxxx
    - August xxth, xxx xx:xx - December xxth, xxxx xx:xx. IP: xxxxxxx
    - March xxth, xx:xx        -          July xxth, xx:xx. IP: xxxxx Description: xx xx

链接