【iOS】開發(fā)并上傳靜態(tài)庫到CocoaPods

1、基于pod自動創(chuàng)建項目(演示項目名:XHLib)

執(zhí)行命令pod lib create XHLib。創(chuàng)建過程中會詢問以下幾個問題:

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 ]
 > None

Would you like to do view based testing? [ Yes / No ]
 > NO

What is your class prefix?
 > XH

第二個問題詢問是否提供一個demo項目,通常選擇Yes,其他的可以根據(jù)需要選擇。命令執(zhí)行完后,就會創(chuàng)建好一個通過cocoapods管理依賴關(guān)系的基本類庫框架。

按照默認配置,類庫的源文件將位于XHLib/Classes文件夾下,資源文件位于XHLib/Assets文件夾下,Demo文件位于Example文件夾下

Demo位置
待封裝代碼位置

2、創(chuàng)建遠端倉庫,將本地倉庫與遠端倉庫進行關(guān)聯(lián)

3、編輯 XHLib.podspec文件

打開XHLib.podspec文件,修改類庫配置信息

Pod::Spec.new do |s|

  s.name             = 'XHLib'
  s.version          = '0.0.1'
  s.summary          = 'A short description of XHLib.'
  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://github.com/XHJCoder/XHLib'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'XHJCoder' => '1149949564@qq.com' }
  s.source           = { :git => 'https://github.com/XHJCoder/XHLib.git', :tag => s.version.to_s }
  s.ios.deployment_target = '8.0'

  # s.ios.vendored_frameworks = 'XHLib.framework'
  s.source_files = 'XHLib/Classes/**/*'

  s.dependency 'SVProgressHUD'

  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
  # s.resource_bundles = {
  #   'XHLib' => ['XHLib/Assets/*.png']
  # }
  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.requires_arc = true

end
podspec文件的簡單說明:

s.name:名稱,pod search 搜索的關(guān)鍵詞,注意這里一定要和.podspec的名稱一樣,否則報錯
s.version:版本號
s.ios.deployment_target:支持的pod最低版本
s.summary: 簡介
s.description: 詳細描述
s.homepage:項目主頁地址
s.license:許可證
s.author:作者
s.social_media_url:社交網(wǎng)址
s.source:項目地址
s.source_files:需要包含的源文件
s.resources: 資源文件
s.requires_arc: 是否支持ARC
s.dependency:依賴的三方庫
s.frameworks:依賴的系統(tǒng)的框架
s.ios.vendored_frameworks:自己的framework

podspec文件的詳細說明:Podspec Syntax Reference。

4、進入Example文件夾下,執(zhí)行pod install,讓demo項目安裝依賴項并更新配置。

pod install

5、添加需要封裝的代碼

將需要封裝的代碼存放到XHLib/Classes目錄下:

存放目錄

在項目中的位置:

注意??:只要XHLib/Classes目錄有變動就需要重新執(zhí)行Pod install或者切換到XHLib “command+B”一下 來更新配置。

(如果上圖這個位置沒有XHLib,進入Manage Schemes進行添加)

前面操作完成后可以在demo項目中調(diào)用測試。

6、庫上傳前相關(guān)配置

靜態(tài)庫

1)制作靜態(tài)庫
進入Pods -> TARGETS -> XHLib,配置靜態(tài)庫。具體操作請看:靜態(tài)庫(.framework)制作

2)找到生成的.framework文件,移動到你想要放的位置(該位置必須與podspec文件里s.ios.vendored_frameworks的位置一樣)

我的項目里將.framework文件放在與podspec文件同一級目錄下

.framework位置

3)修改podspec文件

s.ios.vendored_frameworks = 'XHLib.framework'
開源庫

1)修改podspec文件

s.source_files = 'XHLib/Classes/**/*'

7、進行上傳

1)提交修改并給這次提交打上 tag,上傳到git

git commit -a -m 'v0.0.1'
git tag '0.0.1'
git push --tags
git push origin master

2)驗證類庫(可以通過添加--allow-warnings忽略一些警告)

pod lib lint XHLib.podspec

pod lib lint XHLib.podspec --allow-warnings

出現(xiàn) xxxxx passed validation表示驗證通過.
3)上傳類庫
pod trunk需要注冊 具體做法請看Getting setup with Trunk

pod trunk push
上傳成功

注意??:剛上傳成功后,可能通過pod search無法找到,可以過幾天再看看。

8、測試上傳到cocoapods上的類庫是否可用

1)修改Podfile文件

Podfile文件修改

2)進入Example目錄下 執(zhí)行pod install

3)在demo中調(diào)用,看是否可調(diào)用

其他

刪除本地tag

git tag -d 0.0.1 

刪除遠端tag

git push origin :0.0.1   
//也可以這樣  
git push origin --delete tag 0.0.1   

刪除上傳到cocoapods上的代碼

pod trunk delete XHLib 0.0.1

參考:

http://www.cnblogs.com/brycezhang/p/4117180.html
http://www.itdecent.cn/p/f841e248bc4f

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容