創(chuàng)建靜態(tài)庫(kù)方式
1. Xcode
自帶項(xiàng)目模板
2. 使用CocoaPods
創(chuàng)建
使用CocoaPods
最大的好處是解決第三庫(kù)問(wèn)題。OC
的命名空間是硬傷,所以在開(kāi)發(fā)SDK
時(shí)假如用的第三方庫(kù)別人的App也用到了就會(huì)文件等沖突。也許全部重命名是個(gè)解決辦法,但是一旦多了就比較麻煩了。
使用CocoaPods自動(dòng)創(chuàng)建方式
1. 終端執(zhí)行命令,以YMinSDK
為例:
pod lib create YMinSDK
2. 確認(rèn)輸入5個(gè)問(wèn)題選項(xiàng)
第二個(gè)
demo application
請(qǐng)務(wù)必YES
,幫助極大!第三個(gè)問(wèn)題是問(wèn)用哪個(gè)測(cè)試框架,或者不用
第四個(gè)問(wèn)題是問(wèn)是否需要基于界面的測(cè)試,CocoaPods推薦的是FBSnapShotTestCase
詳細(xì)文檔參考:Using Pod Lib Create
3. 如果CocoaPods
環(huán)境沒(méi)有問(wèn)題的話應(yīng)該已經(jīng)創(chuàng)建成功以及目錄:
4. 打開(kāi)YMinSDK.podspec
文件編輯
Pod::Spec.new do |s|
s.name = 'YMinSDK'
s.version = '0.1.0'
s.summary = 'A short description of YMinSDK.'
# 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/andy90s/YMinSDK'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { '梁先華' => '909901234@qq.com' }
s.source = { :git => 'https://github.com/andy90s/YMinSDK.git', :tag => '0.1.0' }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'YMinSDK/Classes/**/*'
# s.resource_bundles = {
# 'YMinSDK' => ['YMinSDK/Assets/*.png']
# }
s.public_header_files = 'YMinSDK/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
s.dependency 'AFNetworking'
需要注意的幾個(gè):
-
s.source
資源地址,可以填寫(xiě)本地git倉(cāng)庫(kù)地址(pod 創(chuàng)建的工程本身就在git的管理之中) -
s.source_files
類庫(kù)的源文件位置 -
s.resource_bundles
資源文件 -
s.public_header_files
頭文件 -
s.dependency
依賴庫(kù) -
s.source
寫(xiě)本地git倉(cāng)庫(kù)地址即可
5.到Classes
目錄下創(chuàng)建測(cè)試類
17.7.11
補(bǔ)充,寫(xiě)其他項(xiàng)目遇到一個(gè)問(wèn)題無(wú)論我怎么pod install
,demo工程始終無(wú)法引用自己的庫(kù)文件,我是各種嘗試:降cocoapods版本(1.3.2beta),路徑修改,重新創(chuàng)建工程都無(wú)法解決。最后是想起demo中也是有podfile
文件,稍作修改解決問(wèn)題!注意:需要在example
目錄下再次執(zhí)行pod install
或者pod update
,修改如下(注釋掉部分):
# use_frameworks!
target 'TestLib_Example' do
pod 'TestLib', :path => '../'
# target 'TestLib_Tests' do
# inherit! :search_paths
# end
end
6.到Example
文件夾下執(zhí)行pod install
每次測(cè)試如果引入了第三方的依賴或者修改了Classes下面的代碼都需要pod install
或者pod update
到demo中測(cè)試:
7.提交代碼
為了方便我是直接使用Github Desktop
創(chuàng)建倉(cāng)庫(kù)并提交,推到GitHub
之后,可以看到readme
的模板內(nèi)容
8.驗(yàn)證
到項(xiàng)目目錄下
pod lib lint
這里我遇到了一個(gè)錯(cuò)誤驗(yàn)證沒(méi)有通過(guò),提示我public_header_files
沒(méi)有找到匹配的頭文件,仔細(xì)檢查了下發(fā)現(xiàn)默認(rèn)是Pod/Classes/**/*.h
。同理如果遇到source_files
路徑錯(cuò)誤大家仔細(xì)檢查下是否正確。
驗(yàn)證通過(guò)提示passed validation
,忽略警告加上--allow-warnings
。
9.使用CocoaPods-Packager
打包
sudo gem install cocoapods-packager
- 打包lib
注意需要到包含YMinSDK.podspec
文件的根目錄下執(zhí)行命令:
默認(rèn)打包成.framework
pod package YMinSDK.podspec --force
打包.a
pod package YMinSDK.podspec --library --force
關(guān)于兩者區(qū)別
打包成功會(huì)出現(xiàn)個(gè)YMinSDK-0.1.0
文件夾,這就是cocoapods
幫我們打包完畢文件存放位置,找到ios
文件,就可以看到打包好的.framework
文件。
至此打包完成。
目前遇到的問(wèn)題與解決辦法:
1. 寫(xiě)代碼的時(shí)候提示各種not found xxx
到demo目錄下執(zhí)行 pod install
2. 資源訪問(wèn)問(wèn)題
這個(gè)不能用mainbundle
,下面可參考
+ (NSString*)pathForFilename:(NSString*)filename pod:(NSString*)podName
{
NSString* bundlePath = [self bundlePathForPod:podName];
if (!bundlePath) { return nil; }
NSBundle* bundle = [NSBundle bundleWithPath:bundlePath];
NSString* extension = [filename pathExtension];
NSString* withoutExtension = [[filename lastPathComponent] stringByDeletingPathExtension];
NSString* path = [bundle pathForResource:withoutExtension ofType:extension];
return path;
}
+ (NSString*)bundlePathForPod:(NSString*)podName
{
// search all bundles
for (NSBundle* bundle in [NSBundle allBundles]) {
NSString* bundlePath = [bundle pathForResource:podName ofType:@"bundle"];
if (bundlePath) { return bundlePath; }
}
3. 打包之前一定要git commit
確定好提交的版本號(hào),提交到倉(cāng)庫(kù)。實(shí)例:
git add .
git commit -a -m '0.1.0'
git tag -a 0.1.0 -m '0.1.0'
做完這些再去打包靜態(tài)庫(kù)。(注意podsepc中的版本號(hào)一定要一致)
4. 注意第三方庫(kù)引用其他的第三方以及系統(tǒng)庫(kù)
應(yīng)該把所有需要的庫(kù)都在podspec文件中的s.dependency
標(biāo)識(shí)出來(lái),如果少了庫(kù)打出來(lái)的包會(huì)報(bào)錯(cuò)。
5. 打包生成的新podspec文件需要修改
s.ios.vendored_framework = 'xxx.framework'
注意默認(rèn)路徑是ios/xxx.framework
把ios路徑去掉
補(bǔ)上s.dependency
、s.frameworks
等依賴(自動(dòng)生成的podsepc文件沒(méi)有)
6. 應(yīng)該需要兩個(gè)git倉(cāng)庫(kù)
一個(gè)用于存放你開(kāi)發(fā)庫(kù)的源代碼倉(cāng)庫(kù)(私有)
一個(gè)用于存放打包出來(lái)的靜態(tài)庫(kù)和podspec文件的倉(cāng)庫(kù)(可公開(kāi)/私有)
其他人引用靜態(tài)庫(kù)用的是第二個(gè)倉(cāng)庫(kù)地址
7. 在開(kāi)發(fā)過(guò)程中,每引用第三方庫(kù)最好先檢查第三放庫(kù)的podspec文件,檢查所有的依賴,并打包測(cè)試。
8. 測(cè)試打出的靜態(tài)庫(kù)
每次打包上傳倉(cāng)庫(kù)之后,應(yīng)該在本地清理pod緩存 再pod install
- 緩存信息
pod cache list
- 清理所有
pod cache clean --all
- 清理指定
pod cache clean xxx
9.如果時(shí)間充足人手足夠,最好還是自己實(shí)現(xiàn)第三方的功能。
待補(bǔ)充...