本文主要讲解使用CocoaPods创建属于自己的私有组建库或者公有库的具体操作。
1、利用cocoapods创建pod私有库模板
1.1 使用命令生成模板
终端输入:pod lib create LXExtension
<LXExtension> : 为你自定义库名
///平台
What platform do you want to use?? [ iOS / macOS ]
> ios
//语言选择
What language do you want to use?? [ Swift / ObjC ]
> swift
//是否生成演示demo
Would you like to include a demo application with your library? [ Yes / No ]
> yes
//测试框架
Which testing frameworks will you use? [ Quick / None ]
> none
//是否添加基础测试页面
Would you like to do view based testing? [ Yes / No ]
> yes
如果上面寓言选择是的Objc,则选择项会多出一个
//为class增加前缀
what is your class prefix?
> LX
1.2 文件介绍
- Podfile文件
use_frameworks!
platform :ios, '10.0'
target 'LXExtension_Example' do
pod 'LXExtension', :path => '../'
target 'LXExtension_Tests' do
inherit! :search_paths
pod 'FBSnapshotTestCase' , '~> 2.1.4'
end
end
这里的 pod 'LXExtension', :path => '../'
其实就是上层目录下的LXExtension文件,LXExtension文件中包含:类文件Classes
、资源文件Assets
,后面需要添加的类文件及资源文件就在这里
- podspec文件介绍
Pod::Spec.new do |s|
#库的名字
s.name = 'LXExtension'
#版本号,当私有库需要更新的时候只要修改这个值
s.version = '0.1.0'
#库的简介,pod search 显示在上面的介绍
s.summary = 'A short description of LXExtension.'
# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!
#库的详细描述
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
#主页地址,pod search 会显示,一般填写仓库的地址就行了
s.homepage = 'https://github.com/xxxx/LXExtension'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
#开源协议,项目文件目录下需要有一个MIT开源协议文件,创建的时候默认就创建了这个,具体内容可以打开 LICENSE 查看
s.license = { :type => 'MIT', :file => 'LICENSE' }
#作者名字、邮箱
s.author = { 'xxx' => 'xxxxxxx@163.com' }
#资源地址,pod install 的时候会根据这个地址去下载你的想要库,以及下载的版本,必须要跟s.version一致。
s.source = { :git => 'https://github.com/xxxx/LXExtension.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
#库最低支持的系统版本
s.ios.deployment_target = '10.0'
#指定swift版本,需要与progect、targets中swift指定版本一致,否则本地库校验时:pod lib lint,可能会有警告,
s.swift_version = '5.0'
# s.swift_versions = ['5.0','5.2']
#这个很重要,指定资源文件,前缀就是 .podspec 文件当前路径,只用写之后的路径,
#如 Classes/* 是指 Classes 文件夹下的所有文件,但不包括子文件夹里面的文件、
#Classes/**/* 是指包含所有 Classes 文件夹下的文件,包括子文件、
#Classes/**/*.{h,m} 是指包含所有 Classes 文件夹下的后缀为 .h 和 .m 的文件,也可以指定文件。
s.source_files = 'LXExtension/Classes/**/*'
#资源图片,如果有资源文件需打开该配置
# s.resource_bundles = {
# 'LXExtension' => ['LXExtension/Assets/*.png']
# }
#公开的头文件,如果没有公开,用户在用的时候可能引不到响应的头文件
# s.public_header_files = 'Pod/Classes/**/*.h'
#需要依赖的框架
# s.frameworks = 'UIKit', 'MapKit'
#需要依赖的三方库
# s.dependency 'AFNetworking', '~> 2.3'
end
1.3 添加相关类及资源文件
讲述完上面两个文件信息后,我们来说一下如何配置,在1.1步骤执行完后,命令行工具会自动为您打开示例工程。
-
首先我们把LXExtension文件添加到Example下,方便添加Class及资源文件
image.png 删除Classes、Assets文件夹下原有文件ReplaceMe、.gitkeep文件
-
添加私有库中的Classes及资源文件
image.png -
终端进入/LXExtension/Example文件下,注意是
/LXExtension/Example
目录下,执行pod install,
此时就已经把Classes中文件导入至Pods文件下,但是Assest中文件并没有导入进去原因
:
前面提到的.podspec文件中的资源文件引入没有打开,需进入.podspec文件中,打开或增加一下内容,
并重新执行pod install
,此时资源文件就已经被加载进来了#资源图片 参考 s.source_files s.resource_bundles = { 'LXExtension' => ['LXExtension/Assets/**/*'] }
1.4 添加测试代码
因为Pods中已经导入了Classes、Assest文件,此时可以移除掉工程文件中引入的LXExtension目录,在Controller中添加代码进行验证,是否该库可正常使用
-
示例:
showSuccess("成功了")是添加的扩展MBProgress+Extension文件。
演示.png
至此已经初步完成
2. 生成私有/公有库
准备工作
在你的git下创建两个仓库,分别命名为LXPodSpec、LXExtension。如下2.0png所示,后面会用到。
2.1 代码关联远程仓库
- 关联远程仓库
此步骤是在LXExtension
目录下,而非LXExtension>Example
git add . : 添加git文件
git commit -m 'init' :提交
git remote add origin https://github.com/your-username/your-repo.git : 关联到远程仓库
git push origin master :推送至远程仓库
2.2 本地库、远程库校验
- 本地库校验
pod lib lint
:本地组件库校验
pod lib lint AFXXXX.podspec --allow-warnings
:本地组件库校验允许出现警告,也就是可以忽略警告
警告.png
这里是因为我的.podspec
文件中.summary描述没有修改
s.summary = 'A short description of LXExtension.'
解决办法
1、修改podspec中的描述
2、通过增加 --allow-warnings 忽略警告信息
- 远程库校验
pod spec lint
:远程组件库校验
pod spec lint --allow-warnings
: 远程组件库校验允许出现警告,也就是可以忽略警告
pod spec lint --verbose --allow-warnings
:验证过程中显示更详细的输出信息该命令需要先把组件发布到github,
//关联到远程仓库
git remote add origin https://github.com/your-username/your-repo.git
//推送到远程仓库 | 如果本地仓库与远程仓库名称不一致:
git push origin master | git push origin main:master
2.3 生成私有库、公有库
生成私有库、公有库前面几步都是一样的,只有最后推送位置不同。一个是推送至个人的私有仓库、一个是推送至pod公共仓库
-
创建私有库
pod repo add LXPodSpec https://github.com/xxx/LXPodSpec.git
添加本地私有索引库到本地repo中,并与远程索引库进行关联https://github.com/xxx/LXPodSpec.git- 这个命令用于向 CocoaPods 添加一个新的私有仓库。
- LXPodSpec 是你为私有仓库取的名字,你可以根据实际情况自定义。
- https://github.com/xxx/LXPodSpec.git 是远程索引库的地址,用于指示 CocoaPods 去哪里找到这个仓库。
pod repo push LXPodSpec LXExtension.podspec --allow-warnings
推送到 CocoaPods 服务器上,其他开发者可通过CocoaPods 安装和使用这个 Pod,但是需要指定source为https://github.com/xxx/LXPodSpec.git- 这个命令用于将一个 Pod 库推送到你刚刚添加的私有仓库中。
- LXPodSpec 是你之前添加的私有仓库的名称。
- LXExtension.podspec 是你要推送的 Pod 的描述文件(Podspec 文件)的名称,包含了该 Pod 的信息和依赖。
注意:
假如后面你又增加了LXCategory,则不需要再次添加索引库,直接使用命令 pod repo push LXCategory LXExtension.podspec --allow-warnings
,将LXCategory 推送至 LXPodSpec索引库中。
-
创建公有库
pod trunk me
:检查是否已注册 CocoaPods 账户
pod trunk register 你的邮箱 '你的用户名' --description='这里是描述'
: 注册cocoaPods账号,执行该命令后,对应邮箱会收到一份邮件,去邮件内进行验证。
pod trunk push LXExtension.podspec --allow-warnings
:推送至pod公共仓库在推送至pod公共仓库这一步,我这里出现了个错误,参考下面问题4,所以这里只能修改
LXExtension
为LXXExtension
推送成功后,你也可以在 cocoaPods spec索引库中进行查找,查找方法为对库名进行md5加密,按照md5加密结果进行查找,例如 LXXExtensions 加密后的结果为B3144AA406E7BBA931FB1BF25CE3BE91,前三位为B31,即该库在Specs>b>3>1目录下。
3、使用方法
在Podfile文件中添加下面示例,并执行pod install
方式1:
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/Aliffter/LXPodSpec.git'
platform :ios, '11.0'
target 'LXPodModules' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
pod 'LXExtension'
end
方式2:
单独指定某一个库的来源
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'
target 'LXPodModules' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
#pod 'SnapKit'
# 指定库来源
pod 'LXExtension', :source => 'https://github.com/Aliffter/LXPodSpec.git'
end
我通过这种指定source的方式,结果报错如下所示,但是在podfile中增加了一个公有的三方库(pod 'SnapKit'
)后就恢复正常,不知道什么原因😂
[!] Unable to find a specification for `MBProgressHUD (~> 1.2.0)` depended upon by `LXExtension`
You have either:
* out-of-date source repos which you can update with `pod repo update` or with `pod install --repo-update`.
* mistyped the name or version.
* not added the source repo that hosts the Podspec to your Podfile.
方式3:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'
target 'LXPodModules' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
#指定库的 Git 仓库地址,以及版本
pod 'LXExtension', :git => 'https://github.com/Aliffter/LXExtension.git' , :tag => '1.0.0'
end
4、更新私有库、公有库
- 4.1 编辑Pod项目 更新 version
- 4.2 提交Git ,并标记tag,与上面的 version 一致
- 4.3 验证Pod项目
pod lib lint AFXXXX.podspec --allow-warnings //验证本地库
pod spec lint --allow-warnings //验证远程库 - 4.4 推送 .podspec
pod repo push LXPodSpec LXMyTools.podspec --allow-warnings:更新私有库
pod trunk push LXExtension.podspec --allow-warnings:更新公有库
相关终端命令总结
生成pod私有库模板
pod lib create xxx
: 创建私有组件库模板
添加对应的Classes、Assets关联远程仓库
git add .
: 添加git文件
git commit -m 'init'
:提交
git remote add origin https://github.com/your-username/your-repo.git
: 关联到远程仓库
git push origin master
:推送至远程仓库
-
创建tag
git tag 1.0.0
: 增加1.0.0tag
git tag
: 查看本地tag
git ls-remote
: 查看远程所有分支及taggit push origin 1.0.4
: 推送特定tag到远程仓库
git push origin --tags
: 推送所有tags到远程仓库git tag -d 1.0.0
: 删除本地tag
git push origin :refs/tags/1.0.3
: 删除远程tag
本地库校验
pod lib lint
:本地组件库校验
pod lib lint AFXXXX.podspec --allow-warnings
:本地组件库校验忽略警告
pod lib lint --sources=https://github.com/xxxx/xxxxspecs.git --use-libraries --allow-warnings
:如果你的库同时依赖了私有库,可通过上面方式指定source索引
pod lib lint --sources=https://github.com/xxxx/xxxxspecs.git,https://github.com/CocoaPods/Specs.git --use-libraries --allow-warnings
:如果你的库同时依赖了私有库以及公有库,可通过指定多个source索引,中间用逗号分隔远程库校验
pod spec lint
:远程组件库校验
pod spec lint --allow-warnings
: 远程组件库校验忽略警告
-
生成私有库
pod repo add LXPodSpec https://github.com/xxx/LXPodSpec.git
添加私有索引库到本地repo中pod repo push LXPodSpec LXMyTools.podspec --allow-warnings
推送到 CocoaPods 服务器上,其他开发者可通过CocoaPods 安装和使用这个 Pod,但是需要指定source为https://github.com/xxx/LXPodSpec.git
pod repo push LXPodSpec LXMyTools.podspec -skip-import-validation --allow-warnings
如果你的库同时依赖了私有库以及公有库,可通过上面方式跳过验证,谨慎使用 -
生成共有库
pod trunk me
:检查是否已注册 CocoaPods 账户
pod trunk push LXExtension.podspec --allow-warnings
:推送至pod公共仓库 -
一些其他命令
pod spec cat SnapKit
: 查看SnapKit 的 podspec 文件的内容。
出现的一些问题
- 问题1:git push origin master 时报错
error: src refspec master does not match any
error: failed to push some refs to 'https://github.com/xxxx/LXExtension.git'
原因:
本地分支与远程分支名称不一致,本地分支名称为main,远程分支名称为master,
使用git push origin main:master
进行推送
注意
git push 指令的格式,正确格式为:
git push [remote-name(通常为 origin) ] [branch-name]
push过程中如果本地分支与远端分支同名时,branch-name 只需要写一个分支名就可以;
但是push 过程中如果远端分支名不同于本地分支名时,则需要将两边名称都写上:
git push origin [本地分支名:远端分支名]
- 问题2:输入git用户名密码后报错
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/Aliffter/LXExtension.git/'
解决办法:
参考链接:https://www.cnblogs.com/helios-fz/p/15903517.html
-问题3
remote: Repository not found.
fatal: repository 'https://github.com/xxxxx.git/' not found
- 问题3:远程组件库校验时报错 pod spec lint
warning: Could not find remote branch 1.0.0 to clone.
fatal: Remote branch 1.0.0 not found in upstream origin
原因:没有对应的1.0.0 tag
解决办法:
git tag 1.0.0 : 增加1.0.0tag
git push origin --tags: 推送所有tags到远程仓库
- 问题4:执行
pod trunk push LXExtension.podspec --allow-warnings
报错
[!] You (xxxxxx1991@163.com) are not allowed to push new versions for this pod.
The owners of this pod are yyyy@163.com and lx@yoc.tech.
原因:pod仓库已经存在该命名的仓库
解决办法:
更换仓库名
- 跟换LXExtension.podspec 为 LXXExtension.podspec
- podspec文件中 s.name = >'LXXExtension'