CocoaPods 私有库的创建流程


可以使用 Github 创建一个私有的 Specs 库,如 https://github.com/qiweipeng/Specs.git,其 ssh 地址是 git@github.com:qiweipeng/Specs.git

命令行 pod repo add qiweipeng git@github.com:qiweipeng/Specs.git 将这个源拉到本地,本地名称就是命名的 qiweipeng

使用 pod repo 可以查看本地关联的仓库可以确定否添加成功。

指定一个文件夹,在其中 pod lib create Hello 创建一个命为 Hello 的 Pod 项目,之后一系列选项,配置好即可。

qiweipeng@Weipengs-MacBook-Air Developer % pod lib create Hello
Cloning `https://github.com/CocoaPods/pod-template.git` into `Hello`.
Configuring Hello 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 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

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?
 > QWP
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 Hello (0.1.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `Hello.xcworkspace` for this project from now on.
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'Hello/Example/Hello.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`.

创建好的项目是一个 Git 仓库,里面的 Hello/Classes 文件夹用于放置代码,Hello/Assets 文件夹用于放置图片等素材(这些路径可以通过修改 Hello.podspec 文件进行修改)。外部的 Hello.podspec 是配置文件,另外还有 LICENSE 和 README 等都已经自动创建好。

#
# Be sure to run `pod lib lint Hello.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 https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'Hello'
  s.version          = '0.1.0'
  s.summary          = 'A short description of Hello.'

# 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

  s.homepage         = 'https://github.com/Weipeng Qi/Hello'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'Weipeng Qi' => 'qiweipeng@hotmail.com' }
  s.source           = { :git => 'https://github.com/Weipeng Qi/Hello.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

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

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

Hello.podspec 文件的具体配置可以参考官网

具体的可以设置第三方依赖,可以设置系统 Framework 依赖,可以设置 Info.plist 文件,可以指定具体的图片资源、静态库文件,可以指定具体的平台和版本,也可以配置 compiler_flags 等。

如果是私有库,s.source 可以指定为 git@github.com:qiweipeng/Hello.git 这样的通过 ssh 访问的地址。

代码写好,配置好后,将这个 Git 项目上传到 Gtihub 进行托管,地址和 Hello.podspec 中的应一致。

之后需要验证 Hello.podspec 文件,使用 pod spec lint Hello.podspec 进行联网验证,或使用 pod lib lint Hello.podspec 进行本地验证,建议使用联网验证。

因为创建的 Specs 库在 Github 上是私有的,使用了 SSH;对于 Hello 库,因为也是私有的,在验证时会出现警告说无法访问 https://github.com/qiweipeng/Hello,于是验证和提交都加上 --allow-warnings

这里可选参数有:

  • --allow-warnings: 允许警告;
  • --sources=‘master,privateSpecs: 指定源,比如你的私有pod同时依赖了公有库和私有库,你必须指定源才行,因为默认只会去在公有源中查找对应的依赖;
  • --use-libraries: 如果使用了静态库,记得加上它;

确定验证通过,然后将 Hello.podspec 文件提交到之前创建的私有 Specs 库中,命令为 pod repo push qiweipeng Hello.podspec,如果 Hello 是私有库,为了通过,则命令为 pod repo push qiweipeng Hello.podspec --allow-warnings

成功后就可以正常使用了,不过我们需要在 Podfile 文件中指定源:

source 'git@github.com:qiweipeng/Specs.git'
platform :ios, '9.0'

target 'Demo' do
  use_frameworks!

  pod 'Hello', '~> 0.1.0'
  
end

实际工作中也可以直接将已有项目转成 Pods,只需要加一个配置文件即可:

$ pod spec create Peanut
$ edit Peanut.podspec
$ pod spec lint Peanut.podspec

另外,我们创建的 Pod 也可以不通过任何 Specs 管理而直接使用,我们在其它项目中直接在 Podfile 文件中通过相对路径找到需要的 Pod.podspec 文件即可。如:

platform :ios, '9.0'

target 'Demo' do
  use_frameworks!

  pod 'Hello', :path => '../Hello/'
  
end