iOS 组件化 从0到1 创建私有组件库

一、创建远程私有索引库(远程仓库都是基于码云)

1、创建远程私有索引库MyLib(和创建远程工程一样,注意要空白的),并复制仓库地址(点击克隆/下载),后面需要关联到本地。
image.png
2、打开终端,将远程私有库关联到本地
cd /Users/jamace/.cocoapods/repos
pod repo add MyLib https://gitee.com/jamace/MyLib.git

1.https://gitee.com/jamace/MyLib.git是上面仓库MyLib点击克隆/下载按钮复制下来即可

  1. 打开前往-前往文件夹-/Users/jamace/.cocoapods/repos会发现多了一个MyLib文件夹(索引库)。

二、创建私有仓库以及组件工程

1、前往码云创建远程仓库项目https://gitee.com创建项目XJTestComponent。(注意要空白项目,不用其他操作)
image.png
2、创建组件工程
(1)在桌面创建文件夹MyDemo
(2)本地私有代码库
# cd到MyDemo文件夹目录
cd /Users/jamace/MyDemo
# 这里的XJTestComponent是组件名称(自由创建)
pod lib create XJTestComponent

如果提示要输入码云账号,按照提示要求操作就行
执行pod lib create XJTestComponent操作后稍等一会会出现一些组件工程的配置,按照以下配置即可

What platform do you want to use?? [ iOS / macOS ]
>iOS

//开发语言设置,根据自己而定,这里为ObjC
What language do you want to use?? [ Swift / ObjC ]
>ObjC 

//是否需要创建一个demo用来测试你的组件,这里选择Yes,是为了之后对写好的组件进行测试
Would you like to include a demo application with your library? [ Yes / No ]
>Yes 

//测试框架
Which testing frameworks will you use? [ Specta / Kiwi / None ]
>None 

//是否要做基础的视图测试
Would you like to do view based testing? [ Yes / No ]
>No 

//文件前缀
What is your class prefix?
>XJ 
(3)、创建完成过后,我们的工程会自动打开,创建完成后,工程的目录如下
image.png

a、XJTestComponent.podspec这个是仓库配置文件;
b、找到Classes这个文件夹
1.删除Classes文件夹中的ReplaceMe.m文件。
2.Classes文件夹中放置自己需要的代码。
如下图:

image.png
(4)创建成功后配置XJTestComponent项目的.podspec文件,文件位置如上图XJTestComponent.podspec并做以下修改,如下图:
image.png

0.1.0这个版本在后面更新组件的时候会更改,要跟tag一起更改

(5)更新本地组件代码到码云上
cd /Users/jamace/MyDemo/XJTestComponent
git remote add origin https://gitee.com/jamace/XJTestComponent.git #添加远程仓库
git push -u origin master #第一次可能会报错可尝试用 git push -u origin master -f 可能会覆盖远程的修改
git add . #记得后面一定要有 .
git commit -m "创建测试组件"
git push -u origin master
git tag '0.1.0' #注意:这里的tag号必须和.podSpec文件的版本号一致
git push --tags

执行上面操作后前往码云就能看到代码已经提交到XJTestComponent仓库

(6)对文件进行本地验证和远程验证(在工程目录下)

a.从本地验证你的pod能否通过验证

pod lib lint --use-libraries --allow-warnings

1.--verbose:有些非语法错误是不会给出错误原因的,这个时候可以使用--verbose来查看详细的验证过程来帮助定位错误。
2.--use-libraries:表示使用静态库或者是framework,这里主要是解决当我们依赖一些framework库后校验提示找不到库的时候用到。
3.--allow-warnings:表示允许警告。


image.png

b.从本地和远程验证的pod能否通过验证

pod spec lint --use-libraries --allow-warnings

image.png

c.将spec 文件提交到本地的私有仓库,然后再push到远程仓库

pod repo push MyLib XJTestComponent.podspec --use-libraries --allow-warnings 
image.png

d.此时打开/Users/jamace/.cocoapods/repos/MyLib,发现下面多出XJTestComponent文件

image.png

f.查看远程私有索引库
image.png

e.使用终端查看自己的私有组件

pod search XJTestComponent

如果提示
[!] Unable to find a pod with name, author, summary, or description matching XJTestComponent
没有找到的话可以删除search_index.json,如下:

rm ~/Library/Caches/CocoaPods/search_index.json

重新搜索

pod search XJTestComponent

三、如何在项目中使用组件库

新建项目XJTestComponentDemo,cd到该项目目录下添加Podfile文件,其中的仓库远程地址指定去哪个资源去搜索资源

cd /Users/jamace/Desktop/XJTestComponentDemo #项目目录
vim Podfile #创建Podfile
pod install #安装组件库
image.png

四、如何更新组件库

(1)我们在之前的Classes文件夹目录下新增XJTestComponentView1文件夹并且添加新的代码。如下图:
image.png
(2)更新代码到远程仓库

a.修改XJTestComponent.podspec中的版本号为0.1.1,如下图:

image.png

b.cd到组件库的位置

cd /Users/jamace/MyDemo/XJTestComponent
git add .
git commit -m '新的更新描述'
git pull  #在更新之前先拉下代码
git push origin master #提交代码
(3)仓库版本更新
git tag -a '0.1.1' -m '这里需要与上面XJTestComponent.podspec中修改的版本一致'
git push --tags

可以看到我们新的0.1.1的tag

image.png
(4)更新索引库

a.从本地验证你的pod能否通过验证

pod lib lint --use-libraries --allow-warnings

b.从本地和远程验证的pod能否通过验证

pod spec lint --use-libraries --allow-warnings

c.将spec 文件提交到本地的私有仓库,然后再push到远程仓库

pod repo push MyLib XJTestComponent.podspec --use-libraries --allow-warnings 

执行这一步如果报错[!] The repo MyLib at ../../../.cocoapods/repos/MyLib is not clean,执行以下步骤

pod repo remove MyLib    #移除本地私有库
pod repo add MyLib https://gitee.com/jamace/MyLib.git      #在添加本地私有库
pod repo push MyLib XJTestComponent.podspec --use-libraries --allow-warnings    #再执行推送

更新成功:


image.png

搜索看一下有没有我们新的0.1.1版本库

pod search XJTestComponent

如果报错[!] An unexpected version directory Classes was encountered for the /Users/jamace/.cocoapods/repos/gitee-jamace-xjtestcomponent/XJTestComponent Pod in the XJTestComponent repository.

解决方法:去/Users/jamace/.cocoapods/repos删除gitee-jamace-xjtestcomponent这个目录在执行pod search XJTestComponent就好了

可以看到已经更新成功:

image.png

五、设置第三方库依赖

如下图:


image.png

如果我们只要求某个文件夹里的类依赖,而其他类不依赖,那就要如下设置子库:

s.subspec '子库名称' do |别名|
end
image.png

a.注意上面的deployment_target要设置成9.0,因为AFNetworking4.0只支持ios9.0+,这里设置的意思是XJTestComponentView这个文件夹里面的文件依赖AFNetworking。
b.XJTestComponentView1这个文件夹里的文件则不设置依赖。这样设置我们就可以分开安装了,比如在我们的新工程XJTestComponentDemo的Podfile中配置,这样就给我们分开安装需要的库了。

Podfile分开设置如下图:


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容