文章內(nèi)容轉(zhuǎn)移到個人博客日后不會在簡書中更新文章。
上一篇文章《使用私有Cocoapods倉庫》講解了創(chuàng)建私有pod的初步教程,接下來我們提升一下難度。
需求如下:
- 使用私有spec倉庫
- 創(chuàng)建私有pods
- 私有pods依賴第三方靜態(tài)庫(.a , .framework)
- 私有pods依賴第三方pods
- 私有pods依賴另外的私有pods
依賴關(guān)系如下:
- NCKUIKit(私有pod)
- SDWebImage(依賴的第三方pod)
- BaiduMap.framework(手動引入的第三方framework)
- NCKFoundation(依賴的私有pod)
- AFNetworking(依賴的第三方pod)
- libWechat.a(手動引入的第三方framework)
Github上的倉庫列表如下:
- NCKSpecs
- NCKUIKit
- NCKFoundation
為了方便讀者的調(diào)試,此次的倉庫都是基于github的。
創(chuàng)建3個倉庫
1.創(chuàng)建/使用已有NCKSpecs倉庫
如果沒有在github上新建一個倉庫,
如果有該倉庫則直接在終端輸入一下命令即可。
pod repo add NCKSpecs https://github.com/NicolasKim/NCKSpecs.git
以上命令會把github上的NCKSpecs倉庫克隆到本地。執(zhí)行一下命令查看本地的pods倉庫:
pod repo
2.創(chuàng)建NCKFoundation倉庫
在github創(chuàng)建NCKFoundation倉庫
3.創(chuàng)建NCKUIKit倉庫
在github創(chuàng)建NCKUIKit倉庫
編寫 NCKFoundation pods
1. 創(chuàng)建NCKFoundation pods以及DEMO工程
切換到需要的目錄下執(zhí)行一下命令:
pod lib create NCKFoundation
以上命令會在你指定的目錄下,根據(jù)cocoapods的pods模板生成一個pods工程,包含pods以及demo工程,先不要著急打開工程。文件目錄如下:
在終端進(jìn)入到Example目錄下
pod install
這時會看到Example目錄下會生成一個workspace文件
雙擊打開工程,工程目錄如下:
把replaceme刪除掉,Classes下新建一個類NCKRootObject
編輯NCKFoundation.podspec文件,里面有一堆東西,先全部刪除掉,并粘貼如下代碼:
Pod::Spec.new do |spec|
spec.name = 'NCKFoundation'
spec.version = '1.0'
spec.summary = 'Guide for private pods :'
spec.description = <<-DESC
Guide for private pods
DESC
spec.homepage = 'https://github.com/NicolasKim/'
spec.license = { :type => 'MIT', :file => 'LICENSE' }
spec.author = { 'NicolasKim' => 'jinqiucheng1006@live.cn' }
spec.source = { :git => 'https://github.com/NicolasKim/NCKFoundation.git', :tag => spec.version.to_s }
spec.ios.deployment_target = '8.0'
spec.source_files = 'NCKFoundation/Classes/*.{h,m}'
end
到podfile的目錄下執(zhí)行命令
pod install
他會根據(jù)spec文件,將NCKRootObject跟demo工程進(jìn)行鏈接
先做一下參數(shù)說明:
spec可以看做是一個對象實(shí)例,有很多屬性如下:
name:顧名思義就是pod的名字,當(dāng)我們使用
pod search xxxx
時,xxx
就是name。
version : pod 的版本 也就是我們再podfile里指定的那個版本。
summary:概要說明
description:pod的描述。
source:遠(yuǎn)程倉庫的地址以及tag版本
ios.deployment_target:ios部署版本
osx.deployment_target:osx部署版本
source_files: 需要編譯文件的文件目錄,注意是文件目錄而不是工程目錄
2.檢查
在上傳到git倉庫之前,做一下檢查:
在NCKViewcontroller中
#import "NCKRootObject.h"
不報錯說明source_files指定的目錄沒有問題。
接下來用pod的命令檢查一下。
后退到podspec文件存在的目錄下,輸入一下命令:
pod lib lint
如果因?yàn)橛袩o關(guān)緊要的警告而未通過檢查,則輸入一下命令:
pod lib lint --allow-warnings
檢查時pod會忽略警告。如果出現(xiàn)一下提示,你懂得,已經(jīng)成功一大半了。
NCKFoundation passed validation.
接下來遠(yuǎn)程檢查pods,這次cocoapods會根據(jù)podspec文件里的source 檢查對應(yīng)的倉庫里有沒有指定tag的倉庫。顯然我們還沒有將NCKFoundation 上傳到github的NCKFoundation倉庫里,可定會報錯,執(zhí)行試一下。
pod spec lint
-> NCKFoundation (1.0)
- WARN | description: The description is shorter than the summary.
- ERROR | [iOS] unknown: Encountered an unknown error ([!] /Applications/Xcode.app/Contents/Developer/usr/bin/git clone https://github.com/NicolasKim/NCKFoundation.git /var/folders/6x/8pyfnj_n4573cq4206yjz41c0000gn/T/d20161017-14130-148x77b --template= --single-branch --depth 1 --branch 1.0
Cloning into '/var/folders/6x/8pyfnj_n4573cq4206yjz41c0000gn/T/d20161017-14130-148x77b'...
fatal: Remote branch 1.0 not found in upstream origin
) during validation.
Analyzed 1 podspec.
[!] The spec did not pass validation, due to 1 error and 1 warning.
根據(jù)錯誤得出,對應(yīng)的倉庫里沒有1.0的分支。
3.上傳NCKFoundation
那我們就將工程上傳的github上,并創(chuàng)建1.0的tag。
上傳方式自行解決,網(wǎng)上有很多可視化工具,比如sourcetree。
上傳結(jié)束后再檢查一下:
pod spec lint --allow-warnings
這次應(yīng)該沒有問題了吧?
好,還沒完,接下來我們把podspec文件上傳的我們自己的spec倉庫里,而不是cocoapods的倉庫。
pod repo push NCKSpecs NCKFoundation.podspec --allow-warnings
cocoapods會把podspec文件上傳到NCKSpecs倉庫中。
好現(xiàn)在去你的github看看spec倉庫。
4.使用私有pods
新建一個工程,TestNCKFoundationPod
終端進(jìn)入該目錄,新建Podfile。
在第一行加入如下代碼:
source 'https://github.com/NicolasKim/NCKSpecs.git'
意思就是當(dāng)搜索或者下載pods的時候默認(rèn)從NCKSpecs.git中獲得信息。
也就是說,我們從Cocoapods的spec倉庫移到我們自有的spec倉庫。之后就是正常的pods使用方法。
target 'TestNCKFoundationPod' do
pod 'NCKFoundation', '~> 1.0'
end
pod install
試圖引入NCKRootObject.h并運(yùn)行測試工程。
5. 添加afnetworking 和 微信的依賴
先從微信開放平臺下載微信sdk
在NCKFoundation/NCKFoundation/Classes 目錄下創(chuàng)建ThirdParty文件夾,并將.a和.h文件拖到ThirdParty文件夾下。(注意不要拖到工程目錄下,而是文件目錄)
在podspec文件里修改source_file 為
spec.source_files = 'NCKFoundation/Classes/*.{h,m}', 'NCKFoundation/Classes/ThirdParty/*.{h}'
添加.a靜態(tài)庫的依賴,.a依賴的系統(tǒng)framework以及l(fā)ibrary
spec.vendored_libraries = 'NCKFoundation/Classes/ThirdParty/*.{a}'
spec.frameworks = 'SystemConfiguration','CoreGraphics','CoreTelephony','Security','CoreLocation','JavaScriptCore'
spec.libraries = 'iconv','sqlite3','stdc++','z'
參數(shù)說明
vendored_libraries: 第三方.a文件
frameworks: 該pod依賴的系統(tǒng)framework
libraries: 該pod依賴的系統(tǒng)library
添加AFNetworking的依賴
spec.dependency 'AFNetworking', '~> 3.1.0'
參數(shù)說明
dependency : 該pod依賴的pod
pod install
之后看一下工程目錄
在Example里的NCKViewController里#import "WXApi.h"
運(yùn)行工程看看報不報錯。
6.制作subspec
先說一下subspec是什么
在終端搜索一下sdwebimage,結(jié)果如下
也就是說我們再使用sdwebimage時不用下載全部的組件,喜歡用哪個就下哪個,由此得出subspec可以生成一個子模塊兒,當(dāng)然這需要我們再編碼的過程中減少模塊兒之間的依賴,使各個模塊兒可以獨(dú)立運(yùn)行。
好!接下來我們試著做一個子模塊兒
在工程目錄下 新建文件夾NCKSub(并生成對應(yīng)的文件目錄) 新建一個類NCKChild,并在podspec文件里添加如下代碼:
spec.subspec 'NCKSub' do |cs|
cs.source_files = 'NCKFoundation/Classes/NCKSub/*.{h,m}'
end
參數(shù)說明:
NCKSub : subspec的名字 可以隨意起名
cs : 為變量名 與spec類似 擁有spec用的所有屬性 可以隨意起名
執(zhí)行
pod install
觀察一下工程目錄,并運(yùn)行。還順利嗎?
接下來我們準(zhǔn)備升級pods的版本
將podspec的version 改為2.0
然后執(zhí)行
pod lib lint --allow-warnings
將NCKFoudation push至遠(yuǎn)程倉庫并新建2.0的tag
將podspec push至私有的spec倉庫中
pod repo push NCKSpecs NCKFoundation.podspec --allow-warnings
搜索一下
pod search NCKFoundation
結(jié)果如下:
我們?nèi)estNCKFoundationPod 這個工程中修改一下Podfile為
source 'https://github.com/NicolasKim/NCKSpecs.git'
source 'https://github.com/CocoaPods/Specs.git'
target 'TestNCKFoundationPod' do
pod 'NCKFoundation', '~> 2.0'
end
因?yàn)樵谒接械膒od中依賴了另外的pod(afnetworking)afnetworking的spec文件是在cocoapods的倉庫里 所以要添加一個cocoapods的spec倉庫地址。
pod install
看一下工程目錄:
NCKFoundation pods制作完畢!
編寫 NCKUIKit pods
新建NCKUIKit pods 步驟與NCKFoundation一樣 進(jìn)入指定目錄
直接在終端里輸入
pod lib create NCKUIKit
到Example目錄下pod install
打開工程編輯podspec文件
Pod::Spec.new do |spec|
spec.name = 'NCKUIKit'
spec.version = '1.0'
spec.summary = 'A short description of NCKUIKit.'
spec.description = <<-DESC
Add long description of the pod here.
DESC
spec.homepage = 'https://github.com/NicolasKim/NCKUIKit'
spec.license = { :type => 'MIT', :file => 'LICENSE' }
spec.author = { 'NicolasKim' => 'jinqiucheng1006@live.cn' }
spec.source = { :git => 'https://github.com/NicolasKim/NCKUIKit.git', :tag => spec.version.to_s }
spec.ios.deployment_target = '8.0'
spec.source_files = 'NCKUIKit/Classes/*.{h,m}'
spec.dependency 'SDWebImage', '~> 3.8.2'
end
關(guān)聯(lián)Baidumap.framework
去百度地圖官網(wǎng)下載framework
目錄如下
將整個文件夾拖到Classes目錄下
然后編輯podspec如下
Pod::Spec.new do |spec|
spec.name = 'NCKUIKit'
spec.version = '1.0'
spec.summary = 'A short description of NCKUIKit.'
spec.description = <<-DESC
Add long description of the pod here.
DESC
spec.homepage = 'https://github.com/NicolasKim/NCKUIKit'
spec.license = { :type => 'MIT', :file => 'LICENSE' }
spec.author = { 'NicolasKim' => 'jinqiucheng1006@live.cn' }
spec.source = { :git => 'https://github.com/NicolasKim/NCKUIKit.git', :tag => spec.version.to_s }
spec.ios.deployment_target = '8.0'
spec.source_files = 'NCKUIKit/Classes/*.{h,m,mm}'
spec.dependency 'SDWebImage', '~> 3.8.2'
spec.vendored_frameworks = ['NCKUIKit/Classes/BaiduMap_IOSSDK_v3.0.0_Lib/BaiduMapAPI_Base.framework',
'NCKUIKit/Classes/BaiduMap_IOSSDK_v3.0.0_Lib/BaiduMapAPI_Location.framework',
'NCKUIKit/Classes/BaiduMap_IOSSDK_v3.0.0_Lib/BaiduMapAPI_Map.framework',
'NCKUIKit/Classes/BaiduMap_IOSSDK_v3.0.0_Lib/BaiduMapAPI_Search.framework',
'NCKUIKit/Classes/BaiduMap_IOSSDK_v3.0.0_Lib/BaiduMapAPI_Utils.framework']
spec.resource_bundles = {'Resources' => 'NCKUIKit/Classes/BaiduMap_IOSSDK_v3.0.0_Lib/BaiduMapAPI_Map.framework/Resources/mapapi.bundle'}
spec.frameworks = 'CoreLocation','QuartzCore','OpenGLES','SystemConfiguration','CoreGraphics','Security','CoreTelephony'
spec.libraries = 'sqlite3.0', 'stdc++.6.0.9'
#spec.user_target_xcconfig = {'OTHER_LDFLAGS' => ['-lObjC','-all_load']}
end
參數(shù)說明:
vendored_frameworks: 第三方framework
pod_target_xcconfig: build setting的配置
關(guān)聯(lián)私有pod NCKFoundation
繼續(xù)編輯podspec文件,如下:
Pod::Spec.new do |spec|
spec.name = 'NCKUIKit'
spec.version = '1.0'
spec.summary = 'A short description of NCKUIKit.'
spec.description = <<-DESC
Add long description of the pod here.
DESC
spec.homepage = 'https://github.com/NicolasKim/NCKUIKit'
spec.license = { :type => 'MIT', :file => 'LICENSE' }
spec.author = { 'NicolasKim' => 'jinqiucheng1006@live.cn' }
spec.source = { :git => 'https://github.com/NicolasKim/NCKUIKit.git', :tag => spec.version.to_s }
spec.ios.deployment_target = '8.0'
spec.source_files = 'NCKUIKit/Classes/*.{h,m,mm}'
spec.dependency 'SDWebImage', '~> 3.8.2'
spec.vendored_frameworks = ['NCKUIKit/Classes/BaiduMap_IOSSDK_v3.0.0_Lib/BaiduMapAPI_Base.framework',
'NCKUIKit/Classes/BaiduMap_IOSSDK_v3.0.0_Lib/BaiduMapAPI_Location.framework',
'NCKUIKit/Classes/BaiduMap_IOSSDK_v3.0.0_Lib/BaiduMapAPI_Map.framework',
'NCKUIKit/Classes/BaiduMap_IOSSDK_v3.0.0_Lib/BaiduMapAPI_Search.framework',
'NCKUIKit/Classes/BaiduMap_IOSSDK_v3.0.0_Lib/BaiduMapAPI_Utils.framework']
spec.resource_bundles = {'Resources' => 'NCKUIKit/Classes/BaiduMap_IOSSDK_v3.0.0_Lib/BaiduMapAPI_Map.framework/Resources/mapapi.bundle'}
spec.frameworks = 'CoreLocation','QuartzCore','OpenGLES','SystemConfiguration','CoreGraphics','Security','CoreTelephony'
spec.libraries = 'sqlite3.0', 'stdc++.6.0.9'
#spec.user_target_xcconfig = {'OTHER_LDFLAGS' => ['-lObjC','-all_load']}
spec.dependency 'NCKFoundation'
end
podspec文件編輯完畢,先不要著急 pod install
現(xiàn)在pod install 肯定找不到 NCKFoundation 所以我們需要在里Podfile一下 source
編輯Podfile如下:
use_frameworks!
source 'https://github.com/NicolasKim/NCKSpecs.git'
source 'https://github.com/CocoaPods/Specs.git'
target 'NCKUIKit_Example' do
pod 'NCKUIKit', :path => '../'
target 'NCKUIKit_Tests' do
inherit! :search_paths
pod 'FBSnapshotTestCase'
end
end
pod install
這是會從私有倉庫https://github.com/NicolasKim/NCKSpecs.git 下載NCKFoundation 并配置到工程。
把NCKUIKit push到私有倉庫里。(請自行解決)
接下來檢查一下podspec文件
注意 : NCKUIKit的檢查與NCKFoundation的檢查稍微有點(diǎn)兒區(qū)別,因?yàn)镹CKUIKit 依賴了NCKFoundation,同時NCKFoundation 是私有倉庫里的pod ,所以我們檢查是需要指定source。一個為cocoapods的source 另一個為私有spec倉庫的source
pod lib lint --sources=https://github.com/NicolasKim/NCKSpecs.git,https://github.com/CocoaPods/Specs.git --allow-warnings
檢查通過后直接把spec文件push到私有spec倉庫里。
注意:push時默認(rèn)會檢查 spec文件 所以同樣需要提供source
pod repo push NCKSpecs NCKUIKit.podspec --sources=https://github.com/NicolasKim/NCKSpecs.git,https://github.com/CocoaPods/Specs.git --allow-warnings
將 TestNCKFoundationPod 的podfile改成如下:
target 'TestNCKFoundationPod' do
pod 'NCKUIKit'
end
pod install
NCKFoundation pods制作完畢!
感謝您的耐心觀看,如果您覺得這篇文章有幫助,請您高臺貴指點(diǎn)一下喜歡就可以了。