創(chuàng)建SDK項目
1语盈、打開終端到相應(yīng)的目錄下執(zhí)行pod lib create sdkName,sdkName是SDK名稱缰泡,執(zhí)行之后看到如下信息:
pod lib create TestSDK
Cloning `https://github.com/CocoaPods/pod-template.git` into `TestSDK `.
Configuring TestSDK 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 ]
2黎烈、選擇平臺,根據(jù)提示輸入iOS或者macOS,這里我們選擇iOS:
What platform do you want to use?? [ iOS / macOS ]
> iOS
3照棋、選擇編程語言资溃。這里我們選擇ObjC,執(zhí)行:
What language do you want to use?? [ Swift / ObjC ]
> ObjC
4烈炭、選擇是否需要demo溶锭。為了方便自測,我們需要一個demo符隙,選擇Yes趴捅、執(zhí)行:
Would you like to include a demo application with your library? [ Yes / No ]
> Yes
5、選擇測試框架霹疫。這里我們選著None拱绑,繼續(xù)執(zhí)行:
Which testing frameworks will you use? [ Specta / Kiwi / None ]
> None
6、選擇是否需要集成基于view的測試框架丽蝎。這里我們選擇No猎拨,繼續(xù)執(zhí)行:
Would you like to do view based testing? [ Yes / No ]
> No
7、設(shè)置項目代碼前綴屠阻。這里根據(jù)自己需要設(shè)置红省,也可以不設(shè)置。執(zhí)行之后就會創(chuàng)建并自動打開一個xcode工程:
What is your class prefix?
>
8国觉、工程結(jié)構(gòu)
Example是demo吧恃。SDK源碼目錄在TestSDK->Classes,資源文件TestSDK->Assets麻诀。
9痕寓、自動生成Podfile文件,并且指定了SDK的路徑到本地路徑path:
use_frameworks!
platform :ios, '9.0'
target 'TestSDK_Example' do
pod 'TestSDK', :path => '../'
target 'TestSDK_Tests' do
inherit! :search_paths
end
end
SDK開發(fā)
SDK源碼編寫
在TestSDK->Classes目錄下添加代碼文件蝇闭。當(dāng)資源文件代碼文件或者podspec有更新時直接pod install就可以了厂抽。
podspec文件編寫
Pod::Spec.new do |s|
s.name = 'TestSDK'
s.version = '0.1.0'
s.summary = 'A short description of TestSDK.'
# 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/ksxx.com/TestSDK'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { '123@126.com' => '123456@kingsoft.com' }
s.source = { :git => 'https://github.com/ksxx.com/TestSDK.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '9.0'
# 代碼文件
s.source_files = 'Classes/**/*'
# public頭文件
s.public_header_files = 'Classes/**/*.h'
# 依賴的系統(tǒng)frameworks
s.frameworks = 'UIKit', 'MapKit'
# 依賴的系統(tǒng).a文件
s.libraries = 'c++', 'resolv', 'stdc++.6.0.9'
# 通過Pod依賴的第三方庫,多個庫就多個s.dependency
s.dependency 'AFNetworking', '~> 2.3'
s.dependency 'SDWebImage'
# 手動依賴第三方framework
s.vendored_frameworks = 'Classes/b/*.framework', 'TestSDK/Classes/a/*.framework'
# 手動依賴第三方.a文件
s.vendored_libraries = 'Classes/a/*.a','Classes/b/*.a'
# 資源文件打包成bundle丁眼,避免資源文件沖突
s.resource_bundles = {
'TestResourceBundle' => ['Assets/Resource/*']
'TestImgesBundle' => ['Assets/Images/*.png']
}
# 使用resources方式引用資源文件
# spec.resources = ['Assets/Images/*.png', 'Assets/Resource/*']
# 配置xcode的other flag
s.pod_target_xcconfig = { 'OTHER_LDFLAGS' => '-lObjC' }
# 指定SDK為靜態(tài)庫
s.static_framework = true
#支持系統(tǒng)版本
s.ios.deployment_target = '9.0'
# 子模塊ModelA
s.subspec 'ModelA' do |a|
a.source_files = 'Classes/ModelA/**/*.{h,m}'
a.public_header_files = 'Classes/ModelA/*.h'
a.dependency 'CocoaAsyncSocket', '7.6.3'
a.libraries = 'c++', 'resolv'
a.frameworks = 'CoreBluetooth', 'ExternalAccessory'
a.vendored_frameworks = 'Classes/Frameworks/ModelA.framework'
a.pod_target_xcconfig = { 'OTHER_LDFLAGS' => '-lObjC' }
end
# 子模塊ModelB
s.subspec 'ModelB' do |b|
b.source_files = 'Classes/ModelB/**/*.{h,m}'
b.public_header_files = 'Classes/ModelB/*.h'
b.dependency 'HongDa', '4.8.6'
end
end
Public頭文件
這部分主要是SDK對外開放的頭文件筷凤,如果我們不指定的話,默認就是源代碼的全部頭文件苞七。所以我們要根據(jù)自己需要藐守,開放指定目錄下的頭文件:
# public頭文件
s.public_header_files = 'Classes/**/*.h'
依賴庫管理
依賴庫系統(tǒng)庫和第三方依賴庫。系統(tǒng)庫都可以通過podspec自動依賴蹂风;第三方依賴庫依賴包括手動依賴和通過Pod自動依賴卢厂。
- 依賴系統(tǒng)庫
# 依賴的系統(tǒng)frameworks
s.frameworks = 'UIKit', 'MapKit'
# 依賴的系統(tǒng).a文件
s.libraries = 'c++', 'resolv', 'stdc++.6.0.9'
- 通過Pod自動依賴第三方庫(第三方庫支持)。
# 通過Pod依賴的第三方庫惠啄,多個庫就多個s.dependency
s.dependency 'AFNetworking', '~> 2.3'
s.dependency 'SDWebImage'
- 手動依賴第三方庫
首先得為第三方庫創(chuàng)建一個目錄慎恒,然后通過podspec指定相應(yīng)的路徑進行依賴:
# 手動依賴第三方framework
s.vendored_frameworks = 'Classes/b/*.framework', 'TestSDK/Classes/a/*.framework'
# 手動依賴第三方.a文件
s.vendored_libraries = 'Classes/a/*.a','Classes/b/*.a'
資源管理
資源管理有兩種方式任内,分別是 resource_bundles 和 resources 兩種方式引用。
- 使用resources方式引用資源:
spec.resources = ['Assets/Images/*.png', 'Assets/Resource/*']
使用 resources 之后只會簡單的將資源文件 copy 到目標(biāo)工程(Example 工程)融柬,最后和目標(biāo)工程的圖片文件以及其他同樣使用 resources 的 Pod 的圖片文件死嗦,統(tǒng)一一起打包為了一個 Assets.car。使用 resources粒氧,如果出現(xiàn)同名的圖片越除,顯然是會出現(xiàn)沖突的。
resources 優(yōu)點:
可以使用 .xcassets 指定資源文件
resources 缺點:
會導(dǎo)致每個庫和主工程之間的同名資源沖突,
不需要用硬編碼方式獲取圖片:
[NSBundle bundleForClass:[self class]] compatibleWithTraitCollection:nil];
所以外盯,一般來說使用 resource_bundles 會更好摘盆,不過關(guān)于硬編碼,還可以再找找別的方式去避免饱苟。
- 使用resources方式引用資源resource_bundles:
# 資源文件打包成bundle孩擂,避免資源文件沖突
s.resource_bundles = {
'TestResourceBundle' => ['Assets/Resource/*']
'TestImgesBundle' => ['Assets/Images/*.png']
}
使用 resource_bundles 之后會為為指定的資源打一個 .bundle,.bundle包含一個 Assets.car箱熬,獲取圖片的時候要嚴格指定 .bundle 的位置类垦,很好的隔離了各個庫或者一個庫下的資源包。避免資源同名沖突坦弟。
resource_bundles 優(yōu)點:
可以使用 .xcassets 指定資源文件
可以避免每個庫和主工程之間的同名資源沖突
resource_bundles 缺點:
獲取圖片時可能需要使用硬編碼的形式來獲取:[[NSBundle bundleForClass:[self class]].resourcePath stringByAppendingPathComponent:@"/SubModule_Use_Bundle.bundle"]
子模塊subspec配置
subspec就相當(dāng)于SDK中的一個組件官地。如果我們想把SDK分層不同的模塊酿傍,使得app在引用SDK時可以按需加載,就可以使用subspec驱入。子模塊跟主的spec是一樣的赤炒,可以理解為它也是一個SDK。比如這里的ModelA:
# 子模塊ModelA
s.subspec 'ModelA' do |a|
a.source_files = 'Classes/ModelA/**/*.{h,m}'
a.public_header_files = 'Classes/ModelA/*.h'
a.dependency 'CocoaAsyncSocket', '7.6.3'
a.libraries = 'c++', 'resolv'
a.frameworks = 'CoreBluetooth', 'ExternalAccessory'
a.vendored_frameworks = 'Classes/Frameworks/ModelA.framework'
a.pod_target_xcconfig = { 'OTHER_LDFLAGS' => '-lObjC' }
end
subspec可以像主spec一樣指定自己的source_files亏较、依賴庫等等莺褒。
那么在Podfile引用的時候可以是:
pod TestSDK/ModelA
SDK提交到CocoaPods
1、注冊Trunk賬戶
注冊Trunk賬戶(郵箱地址建議寫成github注冊的郵箱雪情,用戶名寫成github對應(yīng)的用戶名)遵岩,例如在終端輸入:
pod trunk register helloworld@github.com 'helloworld' –verbose
注冊命令執(zhí)行完之后,對應(yīng)的郵箱地址會收到一封郵件巡通,“請確認您的注冊CocoaPods通過點擊以下鏈接:”尘执,打開鏈接地址完成驗證,如果地址不能點擊就直接粘貼URL到瀏覽器上執(zhí)行宴凉。
注冊完成之后可以通過pod trunk me查看注冊信息誊锭。
2、提交代碼到git弥锄,并打tag丧靡,用tag作版本號
(.podspec, LICENSE 這兩個文件必須提交到git上)
git add .
git commit -m “1.0.0”
git push
git tag 1.0.0
git push –tags
3蟆沫、提交之前先驗證.podspec文件是否合法
pod spec lint TestSDK.podspec
如果警告一般沒有影響,下面如果忽略警告提交可以用–allow-warnings忽略他們:
pod spec lint TestSDK.podspec –allow-warning
4温治、提交.podspec文件到trunk中
pod trunk push FireRadarSDK.podspec
如果不想有警告饭庞,可以用下面的:
pod trunk push FireRadarSDK.podspec –use-libraries –allow-warnings
只要驗證通過,提交時一般沒有什么問題罐盔, 一般會卡在“Updating spec repo ‘master’”, 這時不要關(guān)閉終端但绕。
5、查找提交成功的庫
先重置一下cocoapods:
pod setup
然后搜索剛才提交的SDK名稱:
pod search TestSDK
如果查不到會報錯如下 [!] Unable to find a pod with name, author, summary, or description matching惶看。
解決方案: 先刪除search_index.json文件捏顺,然后再search (文件不存在時會自動下載,根據(jù)不同的網(wǎng)絡(luò)可能要花一會時間纬黎,要等)幅骄。
rm ~/Library/Caches/CocoaPods/search_index.json
pod search TestSDK
6、從cocoapods移除剛才的SDK
pod trunk delete TestSDK 1.0.0
Pod私有庫創(chuàng)建
創(chuàng)建Pod spec 私有庫
1本今、創(chuàng)建一個spec Repo的私有遠程倉庫和一個存放pod所需的項目工程文件的遠程倉庫拆座;
比如:https://github.com/NoNameOrganazation/LNSpec.git
2、創(chuàng)建spec Repo冠息;
前往文件夾 ~/.cocoapods/repos 在終端輸入:
pod repo add LNSpec https://github.com/NoNameOrganazation/LNSpec.git
3挪凑、創(chuàng)建pod的所需的項目工程文件,并上傳到遠程私有倉庫逛艰;
git tag -a 0.0.1 -m "更新版本0.0.1"
git push --tags
git tag -d 0.0.1
pod spec lint LNCore.podspec
pod spec lint LNFeed.podspec --sources=https://github.com/CocoaPods/Specs.git,https://github.com/NoNameOrganazation/LNSpec.git
4躏碳、向私有的spec Repo遠程倉庫中提交podspec;
pod repo push LNSpec LNCore.podspec
pod repo
pod repo push LNSpec LNFeed.podspec --sources=https://github.com/CocoaPods/Specs.git,https://github.com/NoNameOrganazation/LNSpec.git --allow-warnings
5散怖、使用自己創(chuàng)建的私有pod庫菇绵;
Podfile 使用source 指定:
source 'https://github.com/NoNameOrganazation/LNSpec.git'