美文网首页
自定义framework上传CocoaPods

自定义framework上传CocoaPods

作者: angle_杰 | 来源:发表于2022-08-09 15:39 被阅读0次

关于自定义framework的文章可参考上一篇写的内容自定义framework
本篇文章主要讲已经打包好的自定义framework上传到cocoapods,让别人可以方便的通过pods引入

一、确定是否有CocoaPods账号

有的话请跳过步骤一

1.1、注册CocoaPods账号
# pod trunk register 邮箱  昵称
$ pod trunk register 1111111@qq.com 'mark'
#注册之后,会收到验证链接,点击进行验证即可
1.2、查看注册信息
#查看账号信息,执行指令后,会得到相应的信息,Pods中会显示你已发布的库
$ pod trunk me 
#内容类似下面这样
# 查询到的内容
 - Name:     sherry
  - Email:    405275657@qq.com
  - Since:    August 5th, 02:40
  - Pods:
    - LKLPaymentSdk
  - Sessions:
    - August 5th, 02:40 -           Unverified. IP: 180.165.96.46
    - August 5th, 02:41 - December 15th, 00:01. IP: 180.165.96.46
    Description: 昵称

二、创建git远程仓库

2.1 github创建工程

1>设置为public仓库
2>License设置为MIT License

2.2 clone工程到本地
# 切换到指定位置
$ cd ~/pay_git
$ git clone https://github.com/englishsister/PaymentSdk.git

# 下载完成后 在工徎目录下创建一个与工程名相同的目录  PaymentSdk
# 将静态库PaymentSdk.framework放到新创建的PaymentSdk目录中去
# 目录结构应该是: ~/pay_git/PaymentSdk/PaymentSdk/PaymentSdk.framework

三. 配置.podspec文件

3.1创建podspec文件
#名字随意取,但是要和podspec文件中的spec.name保持一致
# ~/pay_git/PaymentSdk
$ pod spec create LKLPaymentSdk

# 执行结束后,生成一个LKLPaymentSdk.podspec 文件
# ~/pay_git /PaymentSdk/LKLPaymentSdk.podspec
161660027898_.pic.jpg
3.2 修改podspec文件参数
# 名称 podspec文件名保持一致
spec.name         = "LKLPaymentSdk"
# 版本号,要与 git tag 保持一致,每次提交要递增 
spec.version      = "1.0.0"
# 简介
spec.summary      = "收银台XXXX"
# 详细描述的言语字要多写一点,文字只能在DESC中间,最好不要与summary一样,会提示警告
spec.description  = <<-DESC
    SDK是xxx用来xxxx的工具包,其中提供大量的API用于帮助用户开发设备的各项功能....尽量写长一点。
                   DESC
                   
#  项目主页如果有些项目主页,没有写 github上创建的工程地址
spec.homepage     = "https://github.com/englishsister/PaymentSdk.git"
# 协议 MIT, 创建github工程的协议
spec.license      = "MIT"

# 作者
spec.author             = { "Owner" => "mark@163.com" }

# 指定运行平台,iOS 9.0以上
spec.platform     = :ios, "9.0"

# 源代码位置 git里与hoepage保持一致就可以了, tag不要动,会自动获取变量
spec.source       = { :git => "https://github.com/englishsister/PaymentSdk.git", :tag => "#{spec.version}" }
#在上传之前可以进行本地测试,稍后另起文章写
spec.source = {:path=> '.'}

# 开放的头文件,从工程根目录下开始写起
spec.source_files  = "PaymentSdk/lakalaPaySDK.framework/Headers/*.{h,m}"

# 排除文件,没有排除文件可不写
# spec.exclude_files = "Example"

# SDK的路径 写上库的位置 (自动生成没有这一项,不写会出错)
spec.vendored_frameworks = 'PaymentSdk/lakalaPaySDK.framework'

# 依赖系统动态库,有就写,没有就不要写 
 spec.frameworks = "CFNetwork", "SystemConfiguration"
# 依赖系统静态库,省略其名称的lib前缀,以及.后缀
spec.library   = "z"

# 第三方依赖 有几个就写几个,
# spec.dependency "JSONKit", "~> 1.4"

# 项目配置,注意这里的SDK不支持模拟器
# 如果多个字段就用逗号分开 { 'VALID_ARCHS' => 'x86_64 armv7 arm64', 'ENABLE_BITCODE' => 'NO' }
spec.pod_target_xcconfig = { 'VALID_ARCHS' => 'armv7 arm64' }

四、 项目提交到远程仓库

# 提交后要打标签 
$ git add --all 或者 $ git add .
$ git commit -m '代码描述'
$ git push

# tag要与 spec.version 字段保持一致
$ git tag '1.0.0'
$ git push origin --tags

五、校验 podspec文件

校验过程中如果出错,可百度进行搜索改正,保存再次校验

# 校验 pod spec lint xxx.podspec
$ pod spec lint LKLPaymentSdk.podspec

# 如果出错 就执行下面的命令,可以查看详细信息
$ pod spec lint LKLPaymentSdk.podspec --verbose

# 如果只是警告可以加上 --allow-warnings 选项
$ pod spec lint LKLPaymentSdk.podspec --allow-warnings

我校验过程中出现过如下错误

file patterns: The `source_files` pattern did not match any file.
意思是source_files没有匹配到文件,是因为刚开始忘记应用vendored_frameworks(自己添加的framework了),导致找不到文件

修改之后,重新验证

六、发布

# 发布
$ pod trunk push LKLPaymentSdk.podspec

# 执行后出现如下结果就表示成功发布了

Updating spec repo `trunk`

🎉  Congrats

 🚀 LKLPaymentSdk (1.0.0) successfully published
 📅  March 3rd, 19:49
 🌎  https://cocoapods.org/pods/LKLPaymentSdk
 👍  Tell your friends!

发布时,碰到以下问题
问题1

pod spec lint LKLPaymentSdk.podspec --allow-warnings 时
 RPC failed; curl 16 Error in the HTTP2 framing layer

解决方法:
方法1.直接重新执行$ pod trunk push LKLPaymentSdk.podspec
方法2.
#设置git配置,尝试强制git使用HTTP 1.1
先git config --global http.version HTTP/1.1
再重新执行$ pod trunk push LKLPaymentSdk.podspec
方法3.
#设置git配置到HTTP2
git config --global http.version HTTP/2
再重新执行$ pod trunk push LKLPaymentSdk.podspec

七、使用

上传成功后,使用之前,先本地搜索,看是否能搜到

pod search LKLPaymentSdk

如果搜索不到,更新本地库,更新成功在重新搜索,

pod repo update 

#更新成功之后,还搜索不到,先删除本地索引,再更新
rm~/Library/Caches/CocoaPods/search_index.json
pod repo update

#Note
如果pod repo update之后,pod search 能搜索到库的最新版本,但是pod install 还是找不到,报错类似下面这样

CocoaPods could not find compatible versions for pod "LKLPaymentSdk":
  In Podfile:
    LKLPaymentSdk (~> 1.1.1)

None of your spec sources contain a spec satisfying the dependency: `LKLPaymentSdk (~> 1.1.1)`.

You have either:
 * mistyped the name or version.
 * not added the source repo that hosts the Podspec to your Podfile.

在Podfile 的顶部增加source 'https://github.com/CocoaPods/Specs.git'
就可以了

能够成功搜索到后,引入到项目中进行使用

# 和使用其它第三方库一样
platform :ios, '10.0'

target 'Your app' do 
    use_frameworks!
    pod 'HYManager-Swift'
end

八、更新

SDK有新的变化后,将新生成的静态库导入到当前工程中。
修改 podspec中的version字段为新的版本号,
提交到远程仓库(第四步),注意tag要与新的版本号保持一致,
并上传发布到pod上(第六步)

本文章参考一步一步上传framework到CocoaPods,并做了部分修改

如有错误欢迎指出

相关文章

网友评论

      本文标题:自定义framework上传CocoaPods

      本文链接:https://www.haomeiwen.com/subject/eddnwrtx.html