0%

如何制作一个CocoaPods私有库

创建私有库模版

使用命令进入到要建立私有库工程的目录,执行以下命令,CDUtils是私有库项目名。

1
$ pod lib create CDUtils

接下来终端会询问几个问题,请根据实际情况设置:

根据提示最终就会创建一个私有工程。

创建私有库Git地址,这里以Github为例

修改配置文件CDUtils.podspec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#
# Be sure to run `pod lib lint CDUtils.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 = 'CDUtils'
s.version = '0.0.1'
s.summary = 'CDUtils 是一个公共方法库。'

# 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/DavidWanderer/CDUtils.git'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { '1516715172@qq.com' => 'jsjhyp@gmail.com' }
s.source = { :git => 'https://github.com/DavidWanderer/CDUtils.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

s.ios.deployment_target = '8.0'

s.source_files = 'CDUtils/Classes/**/*'

# s.resource_bundles = {
# 'CDUtils' => ['CDUtils/Assets/*.png']
# }

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

安装依赖

命令行进入私有库工程所在目录下的Example文件夹,执行pod install,安装依赖项。

1
2
3
4
5
6
7
PoetMacBook-Pro:Example kris$ pod install
Analyzing dependencies
Downloading dependencies
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
PoetMacBook-Pro:Example kris$

添加库的源代码文件

将源码文件放入CDUtils/Classes目录下,与podspec文件保持一致。

验证私有库的正确性

使用命令验证私有库的正确性。

1
$ pod lib lint CDUtils.podspec

提交源码到github,并打Tag。

1
2
3
4
5
6
7
8
9
10
11
12
13
$ git remote add origin https://github.com/DavidWanderer/CDUtils.git

$ git add .

$ git commit -m "0.0.1"

$ git pull origin master

$ git push origin master

$ git tag 0.0.1

$ git push origin 0.0.1

创建私有库的索引库

在Github上创建私有库的索引库,并进行初始化操作。

将创建的索引库添加到本地cocoapods仓库

  • 在自己的私有库目录下执行命令,将创建的索引库克隆到本地的cocoapods的仓库中。
    1
    $ pod repo add CDSpecs https://github.com/DavidWanderer/CDSpecs.git
  • cocoapods本地仓库路径为:
    1
    $ ~/.cocoapods/repos

发布私有库

在自己的私有库目录下执行命令,把当前私有库的索引同步到本地索引库,并同步给远程索引库。

1
$ pod repo push CDUtils CDUtils.podspec

终端截图:

在自己的项目中引用私有库

Podfile如下:

1
2
3
4
5
6
7
8
9
10
11
# Uncomment the next line to define a global platform for your project
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/DavidWanderer/CDSpecs.git'
platform :ios, '8.0'

target '测试' do
# Comment the next line if you don't want to use dynamic frameworks
#use_frameworks!
pod 'CDUtils', '~> 0.0.1'

end