cocoapods 私有库的搭建和使用
前言
cocoapods
是非常好用的一个iOS 依赖管理工具,使用它可以方便的管理和更新项目中所使用到的第三方库,以及将自己的项目中的公共组件交由它去管理,实现工程组件化。
创建私有 repo
介绍
什么是 repo
?他是所有的Pods的一个索引,就是一个容器,所有的Pods都在这个里面,他实际是一个git
仓库,但是当你使用了cocoapods
后他会被clone
到本地的 ~/.cocoapods/repos
目录下,可以进入到这个目录看到 master
文件夹就是这个官方的 repo。
创建 Specs git 仓库
这个 Specs
用来管理自己的私有 pods
,类似于官方 https://github.com/cocoapods/Specs,我们也需要创建一个 git
仓库,只要你有访问权限,你可以把 git
仓库建在 github
、coding
、bitbucket
等,当然也可以建立在公司自己部署的 git
服务器(可以内网,只是到时 pod install
只在内网才能用)。cocoapod 官方的 pod repo
名字叫 master
,阿里云的叫 aliyun,这里举例使用 tianxu-Specs
作为repo
名,在 git
服务器上创建 tianxu-Specs
repository
:
关联远程 git Specs repository到本地 repo
➜ cd ~/.cocoapods/repos
~/.cocoapods/repos
➜ pod repo add tianxu git@your_git_url:wangwanjie/tianxu-Specs.git
Cloning spec repo `tianxu` from `git@your_git_url:wangwanjie/tianxu-Specs.git`
~/.cocoapods/repos took 4s
此时如果成功的话进入到 ~/.cocoapods/repos
目录下就可以看到WTSpecs这个目录了。至此第一步创建私有Spec repo
完成。也可使用 pod repo list
查看。
➜ pod repo list
master
- Type: git (master)
- URL: https://github.com/cocoapods/Specs.git
- Path: /Users/VanJay/.cocoapods/repos/master
tianxu
fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
- Type: git (unknown)
- URL: git@your_git_url:wangwanjie/tianxu-Specs.git
- Path: /Users/VanJay/.cocoapods/repos/tianxu
2 repos
PS:如果有其他合作人员共同使用这个私有Spec repo的话在他有对应Git仓库的权限的前提下执行相同的命令添加这个Spec repo即可。
创建私有 pod
repo 已经建立好了,接下来就是往里面放对应的 pod,也就是我们要使用的私有 pod,比如将网络封装层从项目抽出来作为公用组件,取名 TXNetwork,
➜ cd ~/Documents/Work/Chaos
Documents/Work/Chaos
➜ pod lib create TXNetwork
Cloning `https://github.com/cocoapods/pod-template.git` into `TXNetwork`.
Configuring TXNetwork template.
------------------------------
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 ]
> Specta
Would you like to do view based testing? [ Yes / No ]
> No
What is your class prefix?
> TX
Running pod install on your new library.
Analyzing dependencies
Fetching podspec for `TXNetwork` from `../`
Downloading dependencies
Installing Expecta (1.0.6)
Installing Specta (1.0.7)
Installing TXNetwork (0.1.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `TXNetwork.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 3 dependencies from the Podfile and 3 total pods installed.
Ace! you're ready to go!
We will start you off by opening your project in Xcode
open 'TXNetwork/Example/TXNetwork.xcworkspace'
To learn more about the template see `https://github.com/cocoapods/pod-template.git`.
To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.
Documents/Work/Chaos took 3m 45s
pod
配置自身视情况而定,pod lib create
的问题根据该 pod
情况选择。
关联 pod 到远程 git 仓库
在 git
服务器上创建 TXNetworking
repository
,关联。
cd TXNetwork
TXNetwork on master [?]
➜ git remote add origin https://your_git_url/wangwanjie/TXNetworking
TXNetwork on master [?]
添加代码
把 pod
代码和资源文件对应放置到 TXNetwork/Classes
和 TXNetwork/Assets
,完成之后打 tag 并推送代码。
提交代码到远程仓库
git add .
git commit -m "Initial commit"
git push origin master
给当前的版本加上tag,提交到远程仓库
git tag -a 0.1.0 -m 'v0.1.0' HEAD
git push origin tag 0.1.0
编写 podspec
建议参考cocoapods
上面的来做,照着那些第三库的podspec
文件写就行了,官方参考
这是该 demo
的 podspec
:
#
# Be sure to run `pod lib lint TXNetwork.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 = 'TXNetwork'
s.version = '0.1.0'
s.summary = '介绍 pod 的描述.'
# 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
介绍 pod 的详细描述。
DESC
s.homepage = 'https://your_git_url/wangwanjie/TXNetworking'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'wangwanjie' => 'wangwanjie1993@gmail.com' }
s.source = { :git => 'https://your_git_url/wangwanjie/TXNetworking.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'TXNetwork/Classes/**/*.{h,m}'
# s.resource_bundles = {
# 'TXNetwork' => ['TXNetwork/Assets/*.png']
# }
s.public_header_files = 'TXNetwork/Classes/AFHTTPSessionManager+Retry.h'
# s.frameworks = 'UIKit', 'MapKit'
s.dependency 'AFNetworking', '~> 3.0'
s.requires_arc = true
end
验证 podspec
如果填写有误,根据提示修改。
pod lib lint --allow-warnings
-> TXNetwork (0.1.0)
- NOTE | [TXNetwork/ObjcAssociatedObjectHelpers] xcodebuild: note: Using new build system
- NOTE | [TXNetwork/ObjcAssociatedObjectHelpers] xcodebuild: note: Planning build
- NOTE | [TXNetwork/ObjcAssociatedObjectHelpers] xcodebuild: note: Constructing build description
- WARN | xcodebuild: /Users/VanJay/Documents/Work/Chaos/TXNetwork/TXNetwork/Classes/AFHTTPSessionManager+Retry.m:159:38: warning: this block declaration is not a prototype [-Wstrict-prototypes]
- WARN | [iOS] xcodebuild: /Users/VanJay/Documents/Work/Chaos/TXNetwork/TXNetwork/Classes/AFHTTPSessionManager+Retry.m:151:172: warning: values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead [-Wformat]
- WARN | [iOS] xcodebuild: /Users/VanJay/Documents/Work/Chaos/TXNetwork/TXNetwork/Classes/AFHTTPSessionManager+Retry.m:157:129: warning: values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead [-Wformat]
- WARN | [iOS] xcodebuild: /Users/VanJay/Documents/Work/Chaos/TXNetwork/TXNetwork/Classes/AFHTTPSessionManager+Retry.m:182:118: warning: values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead [-Wformat]
TXNetwork passed validation.
TXNetwork on master [!] took 23s
在 Example 执行验证 pod 是否可用
cd Example
pod install
向Spec repo提交podspec
向Spec repo
提交podspec
需要确定podspec必须通过验证无误。 向我们的私有Spec repo提交podspec只需要一个命令:
pod repo push tianxu TXNetwork.podspec --allow-warnings
检验 pod 是否可 search
pod search
-> TXNetwork (0.1.0)
介绍 pod 的描述.
pod 'TXNetwork', '~> 0.1.0'
- Homepage: https://your_git_url/wangwanjie/TXNetworking
- Source: https://your_git_url/wangwanjie/TXNetworking.git
- Versions: 0.1.0 [tianxu repo]
使用私有 repo
指定 Specs
的位置,自定义添加自己的podspec
。公司内部使用 cocoapods
, 官方 source
是隐式的需要的,一旦你指定了其他 source
,Podfile
里你就需要也把官方的指定上。
source 'https://github.com/cocoapods/Specs.git' # 官方库
source 'https://your_git_url/wangwanjie/tianxu-Specs.git' # 私有库
target 'NewProject' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Pods for NewProject
pod 'TXNetwork'
end