在Cocopods上发布framework(转自Medium)
Cocopods是iOS上常用的负责管理framework的工具
Cocopods是如何工作的
分为两部分介绍
- 在App的项目里安装framework
- 在Cocopods发布framework
1. 在iOS项目里安装framework
创建Podfile
文件,里面要告诉Cocopods需要安装的framework名称,例如
1 | target 'MyApp' do |
意思是要往MyApp项目里安装FrameworkA
完成配置后,要执行pod install
执行安装步骤
pod install
会读取Podfile
里的内容,执行相应步骤
Cocopods
会从远程仓库查找相应的framework进行下载,链接到项目
注意,如果不指定版本,Cocopods
会自动下载最新版本
执行完pod install
后,Cocopods
会自动生成workspace文件,并把目标framework链接到项目中
2. 在Cocopods发布framework
这只发布的流程示例
大体分为这几个步骤
- 安装Cocopods
- 为framework创建git仓库
- 发布framework到git仓库
- 项目安装framework
安装Cocopods
1
gem install cocoapods
为framework创建git仓库
先创建一个本地仓库
1
2
3mkdir ~/MyFramework.git
cd ~/MyFramework.git
git init --bare把MyFramework的项目内容绑定到刚才的git仓库
1
2
3
4
5
6
7
8cd ~/MyFramework
git init
git remote add origin ~/MyFramework.git
git add .
git commit -m "Initial commit"
git tag -a 0.0.1 -m "Version 0.0.1"
git push origin -u master
git push origin --tags
创建本地指定的pod仓库
仓库用来发布自定义的framework,执行下列操作
1
2
3
4
5
6
7
8
9
10
11mkdir ~/MySpecs.git
cd ~/MySpecs.git
git init --bare
git clone ~/MySpecs.git ~/MySpecs
cd ~/MySpecs
touch README.md
git add README.md
git commit -m "Initial commit"
git push origin -u master
pod repo add my-specs ~/MySpecs.git
~/MySpecs.git 使用用绝对路径完成后执行
1
pod repo list
定义的my-specs的pod源就被添加进来了
添加MyFramework的pod配置文件
执行操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23cat > MyFramework.podspec <<-EOF
Pod::Spec.new do |s|
s.name = "MyFramework"
s.version = "0.0.1"
s.summary = "A brief description of MyFramework project."
s.description = <<-DESC
An extended description of MyFramework project.
DESC
s.homepage = "http://your.homepage/here"
s.license = { :type => 'Copyright', :text => <<-LICENSE
Copyright 2018
Permission is granted to...
LICENSE
}
s.author = { "$(git config user.name)" => "$(git config user.email)" }
s.source = { :git => "$HOME/MyFramework.git", :tag => "#{s.version}" } #你的Framework的路径
s.source_files = "MyFramework/**/*.swift"
s.resources = "MyFramework/**/*.xib"
s.platform = :ios
s.swift_version = "4.2"
s.ios.deployment_target = '12.0'
end
EOF完成后,上传pod配置文件到repo
1
pod repo push my-specs MyFramework.podspec
cocpods的全部流程已经完成
之后就可以在项目里的Podfile添加Myframework
的库了
1 | target 'MyApp' do |
需要标明use_frameworks!
source指定MySpecs.git位置
https://medium.com/onfido-tech/distributing-swift-frameworks-via-cocoapods-152002b41783