从零开始创建CocoaPods私有库

2,216 阅读2分钟

为什么要创建CocoaPods私有库?

  • 避免重复的造轮子
  • 节约时间,方便管理自己的代码
  • 精益求精

创建CocoaPods私有库

1.创建私有仓库工程

执行命令pod lib create SmartBeeKit,然后根据实际情况回答问题,本文以创建SmartBeeKit为例。

2.将编写好的源码文件拷贝到SmartBeeKit/Classes目录下

3.在SmartBeeKit/Example目录下执行pod install,然后打开SmartBeeKit.xcworkspace工程,编写测试代码,run😁,验证代码是否导入成功

4.在GitHub或者其他代码托管平台上,创建SmartBeeKit远程仓库https://github.com/xxx/SmartBeeKit.git

5.修改SmartBeeKit.podspec文件,里面 包含了大量的注释说明以及每个参数的含义及用法

s.name:名称,pod search 搜索的关键词
s.version:版本
s.summary:简介,pod search 搜索的关键词
s.homepage:主页地址,例如Github地址
s.license:许可证
s.author:作者
s.social_media_url:社交网址
s.platform:平台
s.source:Git仓库地址,例如在Github地址后边加上 .git 就是Git仓库地址,常见写法如下
s.source_files:需要包含的源文件,常见的写法如下
s.resources:需要包含的图片等资源文件
s.dependency:依赖库,不能依赖未发布的库
s.dependency:依赖库,如有多个可以这样写
s.requires_arc:是否要求ARC

6.运行pod lib lint SmartBeeKit.podspec验证私有库正确性,出现SmartBeeKit passed validation.表示验证成功。

7.将包含配置好的 .podspec 的项目提交到第4步创建的仓库上,并给这次提交打上 tag,到此,私有库就创建好了,good luck!!!

如何引用自己创建的私有库?

有两种方式引用自己创建的私有库

1.直接引用,不使用索引库,所见即所得😁

platform :ios, '9.0'
target 'TestPod' do
    # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
    # use_frameworks!

    # Pods for TestPod
    pod 'SmartBeeKit', :git => 'https://github.com/xxx/SmartBeeKit.git'
end

2.创建私有的cocoapods索引库,然后在引用它

2.1 在GitHub码云等代码托管开发平台上建立一个空的仓库,我创建了一个Rbbin的空仓库,见下图

2.2 将本地索引库与远程索引库做关联

pod repo add Rbbin https://github.com/xxx/Rbbin.git

可以用下面的命令,来查看是否关联成功

pod repo

可以用下面的命令,来删除索引库

pod repo remove [索引库名字]

2.3 将私有仓库push到索引库

pod repo push Rbbin SmartBeeKit.podspec

2.4 配置podfile文件如下,然后pod install
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/xxx/Rbbin.git'

platform :ios, '9.0'

target 'TestPod' do
    # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
    # use_frameworks!

    # Pods for TestPod
    # pod 'SmartBeeKit', :git => 'https://github.com/xxx/SmartBeeKit.git'
    pod 'SmartBeeKit', '~> 0.0.1’
end
2.5 如何更新维护?

如果有新版本了,比如1.0.0,就需要再次执行命令 pod repo push Rbbin SmartBeeKit.podspec,就可以更新上去.

2.6 如何删除podspec索引库中的私有仓库呢?

其实很简单,只需要cd到~/.cocoapods/repos/Rbbin目录下,删掉库目录

rm -rf SmartBeeKit/

然后push到远端仓库

git add .
git commit -m "delete SmartBeeKit"
git push origin master

创建自己的公有库(未完待续...)