iOS 私有库搭建(组件化)

1.创建本地Pods库

pod lib create [私有库名]
复制代码

2.生成本地Pods文件

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

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
//是否包含demo
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 ]
 > Yes
//前缀名
What is your class prefix?
 > XY   

复制代码

3.替换ReplaceMe文件

WX20211011-104611@2x.png

ReplaceMe.m替换为自己的开发文件

4.编辑spec索引文件

Pod::Spec.new do |s|
  //工程名
  s.name             = 'BaseUtility'
  //版本号
  s.version          = '0.1.0'
  //简介概述
  s.summary          = '基础组件库'
  //描述
  s.description      = <<-DESC
                      base、category、tools
                       DESC
  //github项目网页地址
  s.homepage         = 'https://github.com/XueYangLee/BaseUtility'
  //授权许可文件 
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  //代码作者
  s.author           = { '[email protected]' => '[email protected]' }
  //git代码下载地址
  s.source           = { :git => 'https://github.com/XueYangLee/BaseUtility.git', :tag => s.version.to_s }
  //支持最低的版本号
  s.ios.deployment_target = '10.0'
  //!重要 ! 你要传到cocoapods的文件内容  
  s.source_files = 'BaseUtility/Classes/**/*.{h,m}'
  //资源文件,没有可忽略
   s.resource_bundles = {
     'BaseUtility' => ['BaseUtility/Assets/**/*.*']
   }
  //头文件
  s.public_header_files = 'BaseUtility/Classes/**/*.h'
  //框架
  # s.frameworks = 'UIKit', 'MapKit'
  //引用的第三方库
  s.dependency 'AFNetworking'
  
end
复制代码

5.远端Spec仓库创建及私有库代码远端仓库创建

  • 1.创建远端Spec仓库 PrivateSpecs 存储私有库spec索引

创建Spec仓库时需创建README文件 否则在提交的时候会报Your configuration specifies to merge with the ref错误

  • 2.创建私有库代码远端仓库存储代码

6.本地添加spec仓库

pod repo add [Spec仓库名] [Spec仓库地址]
复制代码

例:pod repo add PrivateSpecs [email protected]:XueYangLee/PrivateSpecs.git

本地校验是否添加成功

pod lib lint 
pod lib lint --allow-warnings --verbose
复制代码

远端校验

pod spec lint
复制代码

WX20211012-140556@2x.png

查看

pod repo
复制代码

本地查看 ~/.cocoapods/repos

WX20211012-160740@2x.png

删除更换pod repo

pod repo remove [Spec仓库名]
复制代码

7.远程提交索引文件

pod repo push [Spec仓库名] [私有库索引文件名(.podspec)]
复制代码

忽略警告

pod repo push [Spec仓库名] [私有库索引文件名(.podspec)]  --verbose --allow-warnings
复制代码

如果添加第三方库并包含静态包时需使用use-libraries

pod repo push [Spec仓库名] [私有库索引文件名(.podspec)]  --verbose --use-libraries --allow-warnings
复制代码
pod repo push [Spec仓库名] [私有库索引文件名(.podspec)]  --sources='[Spec私有库地址],https://github.com/CocoaPods/Specs.git' --use-libraries --allow-warnings
复制代码

例:pod repo push PrivateSpecs BaseUtility.podspec --verbose --use-libraries --allow-warnings

WX20211012-190002.png


使用

  • 调用远端私有Pods库
pod 'BaseUtility',:git => 'https://github.com/XueYangLee/BaseUtility.git'
复制代码
source '[email protected]:XueYangLee/PrivateSpecs.git'
pod 'BaseUtility'
复制代码
  • 调用本地私有库

代码clone本地,在项目本地创建LocalLibs文件以供存储本地库,并将私有库复制进去

WX20211012-190546@2x.png

pod 'BaseUtility',  :path => './LocalLibs/BaseUtility'
复制代码

在项目Development Pods文件夹中即可修改使用

WX20211012-190805@2x.png


提交常见错误

  • 1.报错:[!] The repo MySpecs at ../../../.cocoapods/repos/ZZPublicSpecs is not clean

解决方法:

1》移除本地私有索引库:pod repo remove ZZPublicSpecs

2》添加本地私有索引库:pod repo add ZZMakeSpecs https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git

3》添加本地podspec文件到远程私有索引库即可:pod repo push ZZMakeSpecs ZZMakeAVPlayer.podspec --allow-warnings

  • 2.报错:Your configuration specifies to merge with the ref 'refs/heads/master' from the remote, but no such ref was fetched.

创建远端索引库的时候没有创建readme.md文件,也就是是个空仓库。在提交的时候一直报错卡在更新索引库这步。网上找了半天也没找到原因,最后重新生成了一个仓库,添加了readme.md文件。在去提交索引库就可以了。先前那个提交一直报错的那个,我复制了个readme.md文件提交上去,然后再去提交索引库就可以了。所以创建索引库的时候一定不能是个空仓库里面有啥都行就是不能为空。

解决办法:随便往仓库放写东西。

  • 3.报错:Failed to open TCP connection to raw.githubusercontent.com:443

在safari浏览器里访问以下网址,再执行就没问题了。

  • 4.报错:failed to push some refs to

通过以下命令进行代码合并

git pull --rebase origin master
复制代码

这时候就可以在本地文件内看到README.md文件

之后再运行git push origin master 进行推送

  • 5.报错:[!] The xxx.podspec specification does not validate. 但是没有ERROR
pod repo push --sources=https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git,https://github.com/CocoaPods/Specs.git ZZMakeSpecs ZZMakeFMDBManager.podspec  --use-libraries --allow-warnings
复制代码
  • 6.报错[!] Unable to add a source with url `` named -1.

解决:给索引库都添加上路径,带上官方的路径

pod repo push --sources=https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git,https://github.com/CocoaPods/Specs.git ZZMakeSpecs ZZMakeFMDBManager.podspec
复制代码

常见错误参考文章

猜你喜欢

转载自juejin.im/post/7018136595052101639