创建自己的私有远程Pod库

2,146 阅读10分钟

参考来源1:cloud.tencent.com/developer/a…
参考来源2:juejin.cn/post/703119…

私有远程库是为了防止别人访问,但是又能支持pod导入的一种很好的解决方案,一般公司项目或自己的私有项目模块采用这个方式可以实现组件化。

准备工作

私有仓库需要准备:

  • 1、支持cocoaPods的电脑(自行百度)

  • 2、一个存储私有仓库索引的仓库地址
    目的是在进行pod的时候,让pod在这个仓库中去找

  • 3、一个存储组件(代码)的仓库
    pod在私有仓库中找到索引之后,到这个仓库来down源码

创建索引仓库并将其添加到本地

查看本地的所有仓库索引

pod repo

% pod repo    


aliyun-aliyun-specs

- Type: git (master)

- URL:  https://github.com/aliyun/aliyun-specs.git

- Path: /Users/jasonOs/.cocoapods/repos/aliyun-aliyun-specs


cocoapods

- Type: git (remotes/origin/master)

- URL:  https://github.com/CocoaPods/Specs.git

- Path: /Users/jasonOs/.cocoapods/repos/cocoapods


trunk

- Type: CDN

- URL:  https://cdn.cocoapods.org/

- Path: /Users/jasonOs/.cocoapods/repos/trunk


3 repos

此时能看到,本地一共有3个索引仓库,这这个都很熟悉,是公共索引仓库。我们要讲自己的索引仓库也加进来

创建自己的仓库索引

这个没什么特别,就是和创建一个远程仓库而已,这里以gitee为例:

创建一个仓库.png

添加自己的仓库索引到本地

上面创建了一个仓库,将仓库的地址复制出来(建议用https的方式,如果用ssl,需要配置)。

pod repo add 自己定义的仓库名字和远程仓库那个名字无关 远程仓库地址

// eg:
pod repo add MYSpec https://gitee.com/xxx/test-spec.git

此时再执行pod repo,发现自己的远程仓库加到本地了

pod repo


..........

MYSpec

- Type: git (unknown)

- URLhttps://gitee.com/xxxx/test-spec.git

- Path: /Users/98-elden/.cocoapods/repos/MYSpec

  
..........



4 repos

删除本地仓库索引

删除本地repo
$ pod repo remove name

更新本地的Pod 索引库的缓存信息
$ pod repo update [--verbose]

远程仓库加好了, 这个先放一边

创建工程仓库

这个仓库是用来存储源码的

自己创建一个远程工程仓库

仓库地址:https://gitee.com/xxx/product.git

我习惯用sourceTree将远程仓库导入本地。
通过sourceTree 将远程仓库导入本地后,进入这个仓库目录

创建一个索引和示例工程

这个仓库目录,直接用pod命令创建

pod lib create KitSource

如果报错,可多次尝试,或尝试挂梯子。 如果还是不行,走自己的模版仓库,具体操作点这里

$ pod lib create KitSource

Cloning `https://github.com/CocoaPods/pod-template.git` into `KitSource`.

Configuring KitSource template.

security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

  


------------------------------

  


To get you started we need to ask a few questions, this should only take a minute.

  


If this is your first time we recommend running through with the guide: 

 - https://guides.cocoapods.org/making/using-pod-lib-create.html

 ( hold cmd and double click links to open in a browser. )

  


  


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

 > iOS

  


What language do you want to use?? [ Swift / ObjC ]

 > ObjC

  

// 是否创建一个实例工程 这里输入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?

 > Kit

security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

  


Running pod install on your new library.

  


Analyzing dependencies

Downloading dependencies

Installing KitSource (0.1.0)

Generating Pods project

Integrating client project

  


[!] Please close any current Xcode sessions and use `KitSource.xcworkspace` for this project from now on.

Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

  


[!] Your project does not explicitly specify the CocoaPods master specs repo. Since CDN is now used as the default, you may safely remove it from your repos directory via `pod repo remove master`. To suppress this warning please add `warn_for_unused_master_specs_repo => false` to your Podfile.

  


 Ace! you're ready to go!

 We will start you off by opening your project in Xcode

  open 'KitSource/Example/KitSource.xcworkspace'

  


To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.

To learn more about creating a new pod, see `https://guides.cocoapods.org/making/making-a-cocoapod`.

jasonOs@MacdeMacBook-Pro PodProduct %

此时会自动打开示例工程,打开工程目录,找到这个目录

image.png

把东西放在对应的目录之后,打开工程的podfile,可以看到在这个文件里,已经有pod加入

pod 'KitSource', :path => '../'

cd到对应的podfile上一级目录 ,进行 pod install
此时工程里面就引入了我们加入的代码

对工程进行修改和测试,保证项目可用。

修改索引信息

找到工程中的KitSource.podspec文件,主要修改一下信息

s.name = 'KitSource' s.version = '0.1.0'
s.summary = 'KitSource.'
s.description = <<-DESC 
            一段描述 
            DESC 
s.homepage = '主页:一般可以用仓库地址去掉后面那段 ,比如:https://gitee.com/xxx' 
s.source = { :git => '工程仓库地址,比如:https://gitee.com/xxx/product.git', :tag => s.version.to_s } 
s.source_files = 'KitSource/Classes/**/*'

更多注释参照:

Pod::Spec.new do |s|
# pod 库名称
s.name             = 'PodName'
# pod库 版本
s.version          = '0.1.0'
# pod 简述 pod search 的时候显示
s.summary          = 'A short description of PodName.'
# 详细介绍
s.description      = <<-DESC
TODO: Add long description of the pod here.
DESC

# 项目主页 要填写可以访问到的地址,不然验证不通过
s.homepage         = 'https://github.com/zhaixingxing/PodName'
# 开源协议 必须要有
s.license          = { :type => 'MIT', :file => 'LICENSE' }
# 作者信息
# Pod 屏幕截图,支持单个或者数组,主要适用于UI类的pod库。cocoapods推荐使用gif
# s.screenshot  = '图片URL'
# s.screenshots = [ '图片URL','图片URL' ]
s.author           = { 'zhaixingxing' => 'xxx@126.com' }
# 项目地址 不支持ssh的地址,验证不通过,只支持HTTP和HTTPS,最好使用HTTPS
s.source           = { :git => 'https://github.com/zhaixingxing/PodName.git', :tag => s.version.to_s }
# 多媒体介绍地址
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
# 说明文档地址
# s.documentation_url  =  'http://www.example.com/docs.html’

# 支持的版本
s.ios.deployment_target = '9.0'
# 支持的swift版本
# s.swift_version = ‘5.0'
# 是否使用静态库。如果podfile指明了use_frameworks!命令,但是pod仓库需要使用静态库则需要设置
# s.static_framework = true
#是否使用ARC,如果指定具体文件,则具体的文件使用ARC
# s.requires_arc = true   

# 资源文件
s.source_files = 'PodName/Classes/**/*'
#  s.source_files = 'Classes/**/*.{h,m}', 'More_Classes/**/*.{h,m}'
# 资源库文件
# s.resource_bundles = {
#   'PodName' => ['PodName/Assets/*']
# }

# 依赖的系统库 多个用逗号隔开
# s.frameworks = 'UIKit', 'MapKit'
# 依赖的第三方framework 后面是路径 
# s.vendored_frameworks = ['Module/*.framework']
# 依赖的第三方 .a 后面是路径
# s.vendored_library = 'Module/Classes/SDK/*.a'
# 依赖的 .a 多个用逗号隔开 
s.libraries = ['xml2.2','sqlite3.0']
# 依赖的第三方库
# s.dependency 'AFNetworking', '~> 2.3'
# s.dependency 'SDWebImage'

# 公开的头文件
# s.public_header_files = 'Pod/Classes/**/*.h'
# 指定不公开的头文件
# s.private_header_files = 'Pod/Classes/**/*.h'

# 自定义前缀文件 默认为系统 头文件
# s.prefix_header_file = false
# s.prefix_header_file = 'iphone/include/prefix.pch'
# 向系统 pch 文件中添加头文件 多个用逗号隔开 
# s.prefix_header_contents  =  '#import <UIKit/UIKit.h>' , '#import "Test.h"'
end

 #子模块
 s.subspec 'TestSub' do |ss|
     # 子模块文件, 文件信息如上
    ss.source_files = 'podName/Classes/TestSub/**/*'
end

详细podspec配置参照

修改好spec之后,将代码提交到远程仓库

1、将pod创建的工程整个丢到刚才创建的原厂仓库地址下。

方式1、通过sourceTree上传

方式2、通过命令上传

git add . 
git commit -m '一段描述' 
git remote add origin https://gitee.com/xxx/product.git 
// 第一次push如果报错的话可以加上-f 
// git push -f origin master 
git push origin master

2、 打tag 需要与spec中的version中一致

git tag '0.1.0' 
git push --tags

此时在看远程仓库中,标签就有了

image.png

提交podspec到远端索引仓库

先校验

在上传spec文件前我们可以做一个验证来节省时间,不然每次都推送很久结果还是验证失败,会气死人的~

本地验证Spec的必填字段

// 本地验证不会验证 s.source 中的tag
pod lib lint

远端验证

pod spec lint
  • 验证私有库提示 如果验证的是私有库,则在后面加上--private,否则会有警告,你可以选择--allow-warnings来忽略该警告
pod lib lint --private
pod spec lint --private

在校验时,如果报错,
参考这里
参考这里
参考这里

提交仓库索引

将修改编辑好的spec提交到仓库索引

pod repo push 索引仓库 工程名.podspec

// eg:
// pod repo push MYSpec KitSource.podspec

项目中引用

创建新项目中,在podfile中的最前面加入库索引

source 'https://gitee.com/xxx/test-spec.git'

引入库

pod 'KitSource'

此时通过pod install。就可以将代码导入到项目中了

(1)--verbose:表示显示全部的日志信息,建议加上这个,方便判断错误信息。
(2)--sources:如果我们在podspec里面依赖到一些私有的库之后,直接进行校验是会报错的提示找不到,这里加上我们的Spec仓库的地址告诉CocoaPods找不到的时候去哪里找。
(3)--allow-warnings:表示允许警告.
(4)--use-libraries:表示使用静态库或者是framework,这里主要是解决当我们依赖一些framework库后校验提示找不到库的时候用到。

快捷上传仓库

由于上述方式比较费时间,可以采用如下方式:

命令方式

-   确保 **podspec** 文件正确,`pod lib lint --allow-warnings`**如果确信podspec没问题,亦可省略该步骤**-   克隆私有仓库至本地,`git clone 仓库地址`

-   将要上传库 **podspec** 文件按正确格式要求放入私有仓库

    -   Ex: 版本0.0.2则文件夹名命名为`0.0.2`

-   然后正常上传提交代码至远端仓库

-   添加本地标签,`git tag -a 0.0.1 -m 'release 0.0.1'`

-   上传标签至远端,`git push origin --tags`

-   最后更新本地私有库 **repo** 索引,`pod repo update PrivatePod`

sourcetree操作

将自己的私有库科隆到本地,按照固定格式添加文件并上传

image.png

当然,源代码工程也需要新增标签

image.png

异常问题

1、Your configuration specifies to merge with the ref 'refs/heads/main' from the remote, but no such ref was fetched.

用的是自己服务器部署的git ,在创建远程仓库的时候, 默认没有创建任何分支,用工程库直接pull的时候,远程pod仓库没有任何分支。导致无法pull。

解决办法: 步骤一、将远程仓库初始化。使远程仓库有一个master分支 步骤二、master有分支之后,push到远程,同时将本地的自己仓库删掉pod repo remove xxSpec 再重新添加pod repo add xxSpec http://xxx.git

2、具体错误信息忘记了 意思就是提示文件为空 (pod中没有任何文件)

正常是有的,但是pull的时候,缺提示为空。

解决办法:把工程仓库删掉重新创建,在重新处理一次

3、引入第三方framework SDK ,报错 :The 'Pods-xxx_Example' target has transitive dependencies that include statically linked binaries:
Downloading dependencies

Installing Ads-Fusion-CN-Beta (6.0.0.5)

Installing CSJMBaiduAdapter (5.34.0)

Installing CSJMGdtAdapter (4.14.63.0)

Installing CSJMKsAdapter (3.3.61.0.0)

Installing xxxx 0.1.0

[!] The 'Pods-xxxx_Example' target has transitive dependencies that include statically linked binaries: (/Users/98-elden/Documents/98du/NEDPods/xxxx/Example/Pods/Ads-Fusion-CN-Beta/SDK/BUAdSDK.xcframework, /Users/98-elden/Documents/98du/NEDPods/xxxx/Example/Pods/Ads-Fusion-CN-Beta/SDK/CSJMediation/CSJMediation.xcframework, /Users/98-elden/Documents/98du/NEDPods/xxxx/Example/Pods/CSJMBaiduAdapter/CSJMBaiduAdapter/CSJMBaiduAdapter.xcframework, /Users/98-elden/Documents/98du/NEDPods/xxxx/Example/Pods/CSJMGdtAdapter/CSJMGdtAdapter/CSJMGdtAdapter.xcframework, and /Users/98-elden/Documents/98du/NEDPods/xxxx/Example/Pods/CSJMKsAdapter/CSJMKsAdapter/CSJMKsAdapter.xcframework)

解决办法:

xx.podspec文件中 加入s.static_framework = true

4、新建工程编译出现SDK does not contain 'libarclite' at the path '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_iphoneos.a'; try increasing the minimum deployment target

解决办法:

podfile中加入

def pod_setting

 post_install do |installer|

   installer.pods_project.build_configurations.each **do** |config|
     config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"
   end

  
   installer.pods_project.targets.each do |target|
     target.build_configurations.each do |config|
       config.build_settings['EXPANDED_CODE_SIGN_IDENTITY'] = ""
       config.build_settings['CODE_SIGNING_REQUIRED'] = "NO"
       config.build_settings['CODE_SIGNING_ALLOWED'] = "NO"
       config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '11.0'
     end
   end
 end
end
5、提交远程仓库时提示:xcrun: error: unable to find utility "simctl", not a developer tool or in PATH) during validation.
- ERROR | [iOS] unknown: Encountered an unknown error (/usr/bin/xcrun simctl list -j devices

xcrun: error: unable to find utility "simctl", not a developer tool or in PATH
) during validation.
  • 原因是xcode模拟器找不着报错
  • 解决方法:在终端中继续输入 Developer的路径 即可
  • 比如我的Xcode路径就在application里,sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
6、ERROR | [iOS] unknown: Encountered an unknown error (Unable to find a specification for '私有库引用' depended upon by '自己准备制作的库'

原因:模拟器运行成功、真机页运行没问题,但是在进行本地校验pod lib lint 即将制作的库.podspec的时候就是一直报错,因为引用了私有的库,工程中的podfile有指定source,所以能运行成功,但是push的时候找不到,需要加上仓库源地址--source=a仓库地址,b仓库地址,多个仓库用逗号隔开 比如:

pod repo push 自己的仓库名称 制作的库名.podspec --verbose --use-libraries --allow-warnings --sources=私有仓库地址,https://github.com/CocoaPods/Specs.git
7、 pod spec lint 报错

本地校验成功,但是远端校验失败。 因为之前多次push失败,这次lib成功,准备push上去,因为最新的一次没有更改版本号。猜测可能是远端没有进行代码变更。

处理办法:远端的代码清空(隐藏文件.git不删)。本地代码重新提交