1.創(chuàng)建Specs的git路徑
可以理解為庫的頭文件集合先舷,該git里面儲存了組件的.podspec文件
1.github創(chuàng)建空路徑
image.png
示例地址為: ++https://github.com/Avanlanching/Specs.git++
2.將Specs 克隆到本地.cocoapod/repo/目錄下
注意:/User/用戶名/.cocoapods目錄隱藏目錄届惋,需要"shift+command+."來顯示;
可能存在訪問權(quán)限的問題概作,需要先獲取訪問權(quán)限:
# 這里路徑是.cocoapod里面
$sudo chmod -R 777 ../.cocoapods
1.SourceTree可視化界面克隆
image.png
2.終端指令
$pod repo add [name] [url]
e.g $pod repo add Specs https://github.com/Avanlanching/Specs.git
2.1 查看是否添加成功
$pod repo list
image.png
2.創(chuàng)建組件倉庫(用于存放組件代碼的倉庫)
1.創(chuàng)建一個存放倉庫的git路徑
image.png
實(shí)例地址為:https://github.com/Avanlanching/cocoapodAvaDemo.git
2.創(chuàng)建組件
$pod lib create [組件名]
e.g pod lib create cocoapodAvaDemo
image.png
根據(jù)需求選擇配置
iOS, ObjC, Yes, None, Yes/No, AA
3.代碼編寫
1.工程目錄
image.png
2.文件目錄
image.png
3.代碼編寫
image.png
注意:編碼完成測試通過了,需要將書寫的代碼移動到Classes目錄下面
3.1編寫代碼:
組件1.gif
**注意:這里需要對事例工程中的Podfile進(jìn)行pod install操作才會出現(xiàn)在Pod列表里面 **
3.2上傳到git
A.指令上傳
# 初始化git上傳
$git init
$git add .
$git commit -m "init project"
$git remote add origin https://github.com/liuzhao/LZDemo.git
# 新增tag
$git tag '0.1.0' # 與.podspec文件中的s.version一致
$git push tags
B.SourceTree可視化上傳
image.png
新增tag
image.png
3.創(chuàng)建組件.podspec文件管理
1. .podspec基本格式
#
# Be sure to run `pod lib lint cocoapodAvaDemo.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 = 'cocoapodAvaDemo'
s.version = '0.1.0'
s.summary = 'A short description of cocoapodAvaDemo.'
# 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/avanlanching/cocoapodAvaDemo'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'avanlanching' => '18269285634@163.com' }
s.source = { :git => 'https://github.com/avanlanching/cocoapodAvaDemo.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'cocoapodAvaDemo/Classes/**/*'
# s.resource_bundles = {
# 'cocoapodAvaDemo' => ['cocoapodAvaDemo/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
注意,每次進(jìn)行修改需要對其進(jìn)行update操作
$pod update (更新他人使用這個,防止混淆善延,可以一直采用此指令)
或
$pod update --no-repo-update
1.1 驗證.podspec的合法性
1.本地驗證
$pod lib lint --allow-warnings
可能遇到一個錯誤,解決方案
- ERROR | [iOS] unknow: Encountered an nuknown error (Malformed version number string ) during validation.
[!] xxxxxx did not pass validation, due to 1 error.
You can use the '--no-clean' option to inspect any issue.
注意這里可能是配置問題城侧,解決方案如下:
$sudo gem install -n /usr/local/bin cocoapods
2.網(wǎng)絡(luò)驗證并推送到Spec遠(yuǎn)端
$pod repo push Spec --allow-warnings
e.g:$pod repo push Spec AvaDemo.podspec --verbose --allow-warnings
2.添加依賴
組件中依賴其他組件易遣,不提倡在實(shí)例工程中的Podfile去添加依賴的SDK
提倡的操作,以AF為例子
在.podspec文件中添加
s.dependency 'AFNetworking', '~> 2.3'
// 多個SDK
s.dependency 'SDWebImage'
// 不指定版本號赞庶,為最新版本训挡,如果沒有依賴第三方的SDK,需要把s.dependency字段刪除或者注釋
添加完成后需要update
#針對.podspec文件
$pod update
#重復(fù)上述本地驗證和推送到Specs的操作
4.主工程內(nèi)引入
1.在編輯Podfile文件
image.png
添加兩個Source
source 'https://github.com/CocoaPods/Specs.git' //公有組件
# source 'git@github.com:Avanlanching/Spec.git' // 私有組件
source 'https://github.com/Avanlanching/Specs.git'
pod 'AvaDemo' //常規(guī)語法
在過程中出現(xiàn)了一些問題歧强,感謝FlowerSea_5534澜薄,有興趣可以查看這位博主的文章