利用Cocoapods构建自己的私有与公有库

2018/9/5 posted in  iOS工具深入

一:Spec文件

在我们第一次安装pods时,都会主动的去拉取公有的repo,其实无论是共有的还是私有的下载后都在用户的/Users/用户名/.cocoapods/repos 目录下。每一个第三库都有一个spec文件,或者你自己的私有库也应该有这个spec文件。这些spec文件就在相应的repo里。
我将我自己创建一个私有库的spec文件贴出来

#
# Be sure to run `pod lib lint KBRCategory.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'KBRCategory'
  s.version          = '0.1.2'
  s.summary          = 'KBRCategory.'

# 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      = "a description KBRCategory"

  s.homepage         = 'https://gitee.com/BlueLegend'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'raojb@knowbox.cn' => 'raojb@knowbox.cn' }
  s.source           = { :git => 'https://gitee.com/BlueLegend/KBRCategory.git', :tag => s.version.to_s }
  #这里改成了不以tag版本来拉取,以最新的commit
    # s.source           = { :git => 'https://gitee.com/BlueLegend/KBRCategory.git'}
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

  s.source_files = 'KBRCategory/Classes/**/*'
  
  # s.resource_bundles = {
  #   'KBRCategory' => ['KBRCategory/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  s.dependency 'AFNetworking'
end

里面无非就是你要提交到Cocoapods上的名字、版本号,简介、主页、License、作者信息、最低平台信息、从哪个Git上下载、需要引入的framework、那些文件需要被引入,那些文件是资源文件以及是否需要ARC的模式。

  • license
    用MIT就可以了,一定要正确填写不然在验证的时候验证通过

  • source
    是库的地址

  • s.source_files
    是要将哪些文件放入进去

  • s.subspec 添加子模块
    其实可以添加子模块,所谓的子模块,就是在pods出来后有子文件夹

s.subspec 'Security' do |ss|  
  ss.source_files = 'AFNetworking/AFSecurityPolicy.{h,m}'
  ss.public_header_files = 'AFNetworking/AFSecurityPolicy.h'
  ss.frameworks = 'Security'
end  

二:创建公有库

  • 向Cocoapods方面注册一个账号 这个命令会收到一份邮件,让你进行会话授权。具体机制没有研究过,当你切换电脑时,需要重新进行会话授权 pod trunk register raojb@knowbox.cn 'rjb' --description='My own computer'
  • 创建spec文件
    pod spec create 'KBCategory'
    在写好代码后,在你项目的根目录下运行,会在该项目目录下生成一个spec文件。修改这个spec文件的里的相关信息。
    如果是项目已经建立好了,需要创建时,可以在根目录下执行
    pod spec create 'KBCategory'也会生成一个spec文件

  • 验证Podspec文件
    pod lib lint Name.podspec
    这一步是最坑人的地方。会有各种让你通不过的理由。比如,找不到文件,找不到Lience等。
    我遇到了,spec文件与s.source_files文件路径设置的不一致问题。
    开源协议文件也要生成。路径也要设置正确。

        - ERROR | [iOS] xcodebuild: Returned an unsuccessful exit code.
    

    遇到这样的错误,是编译没有通过的情况。如果自己在工程里编译通过,看是不是依赖库没有设置正确。别忘了依赖系统库也要填写。总之基本没有一次性验证通过的,具体问题具体查吧。

    例如
    s.framework = "UIKit"
    最终你出现passed validation就表示验证通过。验证通过表示,你给定的环境,单独编译这部分你提供的目录下的代码能自行编译通过。

  • 上传
    可上传项目到Cocoapods官方的仓库里。
    pod trunk push 项目名.podspec

    上传成功后,可以在命令行里pod search "库名字",若果没有应该是有缓存,删除这个文件
    ~/Library/Caches/CocoaPods/search_index.json
    重新pod search就可以了。

三:创建私有库

其实创建私有库的核心过程还是跟公有库是差不多的。不管是私有库还是公有库,关注点都在于Podspec文件的书写。其实我们讲到pod trunk push 项目名.podspec这条命令,其实是默认我们的Podspec文件提交到Cocoapod的仓库(Specs),然后我们之后的pod install或者pod update都是从这个仓库中提取Podspec文件,然后根据文件里面的信息去取对应的源代码。大家可以上去找找自己的开源的Podespec文件转换成json的文件

  • 建立自己的私有仓库
    pod repo add '仓库名' '仓库地址'建立好后,可以在你的cd ~/.cocoapods/repos目录下查看,是否有你的私有仓库。

  • 写代码->写Podspec文件了->检查项目和Podspec文件->打tag
    这些工作与创建公有库一样

  • 提交podspec文件到仓库
    公有库是pod trunk push 项目名.podspec
    私有库是pod repo push '私有仓库名' 项目名.podspec
    其实就都是讲podspec文件提交到相应的仓库里

特别说明

四:使用私有库

使用公有库与使用私有库的方式一样。都是在podfile文件里有一个source。这个source就是标识所要使用的源(spec文件仓库)。

#公用的私有远程索引库源
source  'git@gitee.com:BlueLegend/BlueLegendPrivateSpec.git'
#github远程索引库源
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

target ‘testdown’ do
    #pod 'lottie-ios'
    pod 'Masonry'
    pod 'SDWebImage', '~> 4.3.0'
    pod 'YYModel', '~> 1.0.4'
    pod 'MJRefresh', '~> 3.1.15.3'
    pod 'KBRCategory'
end