[iOS模塊式開發]例講如何製做一個CocoaPods私有庫

參考資料:http://www.jianshu.com/p/9e2c7a7c1211html

 

私有庫A與私有庫B之間的依賴處理:
例如私有庫B依賴於私有庫A,在私有庫B本地驗證時:ios

pod lib lint

會報錯,提示找不到私有庫A。
執行命令爲:git

pod lib lint --sources='http://[privateLibName]/cocoaspecs.git,https://github.com/CocoaPods/Specs.git'

關鍵詞:庫、模塊式開發、CocoaPods、私有庫、
工具:終端、git、CocoaPods
步驟總結:
一、打開終端,進入要創建私有庫項目工程的路徑,並執行pod庫工程建立命令行
二、進入咱們的Example項目工程,執行安裝CocoaPods項目命令行
三、添加庫源碼文件
四、書寫CocoaPods配置文件LATAlert.podspec(建議使用Atom + cocoa-pods-ruby-snippets插件來編輯該文件)
五、建立私有庫git地址,並完善.podspec配置文件
六、再次進入咱們的Example文件,執行pod更新指令,安裝本地庫源碼
七、添加樣例代碼,運行樣例測試
八、本地pod配置文件驗證
九、項目工程發佈tag 0.0.1
十、私有庫發佈
十一、驗證發佈的私有庫
涉及到的命令行:
Git:github

Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ git remote add origin https://git.oschina.net/KKLater/LATAlert.git Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ git add . Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ git commit -a -m "0.0.1" Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ git pull origin master Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ git push origin master Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ git tag 0.0.1 Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ git push origin 0.0.1

Pod:ruby

--allow-warningsLater@localhost:~/Desktop/私有庫建立教程$ pod lib create LATAlert Later@localhost:~/Desktop/私有庫建立教程/LATAlert/Example$ pod install --no-repo-update Later@localhost:~/Desktop/私有庫建立教程/LATAlert/Example$ pod update --no-repo-update Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ pod repo add LATSpecs https://git.oschina.net/KKLater/LATPodspecs.git Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ pod lib lint Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ pod lib lint --verbose Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ pod repo push LATSpecs LATAlert.podspec Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ pod repo push LATSpecs LATAlert.podspec --verbose

//忽略警告

Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ pod lib lint
--allow-warningsLater@localhost:~/Desktop/私有庫建立教程/LATAlert$ pod repo push LATSpecs LATAlert.podspec 
Later@localhost:~$ pod repo remove LATSpecs

下面咱們經過一個例子來具體講解整個步驟流程。
例子裏面,咱們建立一個私有庫LATAlert,導入LATAlert私有庫的項目,能夠調用私有庫的方法來執行彈出一個Alert顯示信息!服務器

一、打開終端,進入要創建私有庫項目工程的路徑,並執行pod庫工程建立命令行

先建立個文件夾來放置  這個例子的文件夾名字:私有庫建立教程
Later@localhost:~$ cd /Users/Later/Desktop/私有庫建立教程 Later@localhost:~/Desktop/私有庫建立教程$ pod lib create LATAlert


注意:cocoapods要升級到最新版本。  我以前電腦是舊版本,是不會出現下面的界面的。  至於舊版本怎麼設置下面這些屬性我的沒研究,有興趣的能夠本身試試。框架

終端獲得如下界面:
這裏會詢問幾個問題(答案根據實際狀況設置),分別是:
一、語言選擇
—— 教程選擇Objc,若是要作Swift私有庫,請選擇輸入Swift
二、是否是須要一個demo項目工程
​ —— 教程選擇Yes,須要建立一個demo工程,建議建立一個demo工程
三、測試框架使用哪個
​ —— 教程選擇None
四、是否是須要作基本的測試
​ —— 教程選擇Yes
五、類前綴是什麼
​ —— 教程輸入LATtcp


PodTest安裝pod

若是出現上面的界面,說明,私有庫工程建立成功了。ide

二、進入咱們的Example項目工程,執行安裝CocoaPods項目命令行

Later@localhost:~$ cd /Users/Later/Desktop/私有庫建立教程/LATAlert/Example Later@localhost:~/Desktop/私有庫建立教程/LATAlert/Example$ pod install --no-repo-update

終端界面以下:工具


私有庫Example安裝pod項目.png


雙擊LATAlert.xcworkspace運行項目Example工程,驗證工程的正確性。

三、添加庫源碼文件

將源碼文件複製到文件夾路徑:LATAlert/LATAlert/Classes下。

資源文件放到Assets下。

注:1.若是尚未源碼,則能夠在Example下直接建立源碼文件,實際測試經過後,再按照下面的方式來添加源碼文件。2.方便起見這裏直接將以前使用的源碼文件拷貝過來,其餘根據實際狀況,看官自行添加。3.文件路徑並非絕對的,可是建議按指定路徑存放,便於總體的後期維護更新


添加源碼文件.png

四、書寫CocoaPods配置文件LATAlert.podspec(建議使用Atom + cocoa-pods-ruby-snippets插件來編輯該文件)

雙擊打開LATAlert.podspec文件,此處我設置了默認Atom打開,看官自行配置,可是要注意編輯內容必定要教程同樣。


podspec文件編輯1.png

咱們能夠把沒有用的註釋刪掉。具體經常使用的配置以下:

# # Be sure to run `pod lib lint LATAlert.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 http://guides.cocoapods.org/syntax/podspec.html # Pod::Spec.new do |s| #庫名 s.name = 'LATAlert' #庫版本 s.version = '0.0.1' #庫簡短描述 s.summary = 'A short description of LATAlert.' #庫詳細描述 s.description = <<-DESC TODO: Add long description of the pod here. DESC #庫介紹主頁地址 s.homepage = 'https://github.com/<GITHUB_USERNAME>/LATAlert' #庫開源許可 s.license = { :type => 'MIT', :file => 'LICENSE' } #做者信息 s.author = { 'Later' => 'lshxin89@126.com' } #源碼git地址 s.source = { :git => 'https://github.com/<GITHUB_USERNAME>/LATAlert.git', :tag => s.version.to_s } #庫依賴系統版本 s.ios.deployment_target = '8.0' #源碼文件配置 s.source_files = 'LATAlert/Classes/**/*' #資源文件配置 s.resource_bundles = { 'LATAlert' => ['LATAlert/Assets/*.png'] } #源碼頭文件配置 s.public_header_files = 'Pod/Classes/**/*.h' #系統框架依賴 s.frameworks = 'UIKit', 'MapKit' #第三方框架依賴 s.dependency 'AFNetworking', '~> 2.3' end

清理掉不須要的配置項以後,總體以下:

Pod::Spec.new do |s| #庫名 s.name = 'LATAlert' #庫版本 s.version = '0.0.1' #庫簡短描述 s.summary = 'A short description of LATAlert.' #庫詳細描述 s.description = <<-DESC TODO: Add long description of the pod here. DESC #庫介紹主頁地址 s.homepage = 'https://github.com/<GITHUB_USERNAME>/LATAlert' #庫開源許可 s.license = { :type => 'MIT', :file => 'LICENSE' } #做者信息 s.author = { 'Later' => 'lshxin89@126.com' } #源碼git地址 s.source = { :git => 'https://github.com/<GITHUB_USERNAME>/LATAlert.git', :tag => s.version.to_s } #庫依賴系統版本 s.ios.deployment_target = '8.0' #源碼文件配置 s.source_files = 'LATAlert/Classes/**/*' #源碼頭文件配置 s.public_header_files = 'LATAlert/Classes/*.h' #系統框架依賴 s.frameworks = 'UIKit' end

細心的看官注意到,這裏咱們使用的homepage和source並不完整下面就須要咱們建立一個git私有庫,用於項目工程的存放,獲取咱們所須要的地址。

五、建立私有庫git地址,並完善.podspec配置文件

 建立私有庫地址這裏按照實際狀況選擇了。   正常狀況公司都有代碼管理的地方。拿我公司舉例是用gitlab管理代碼的。
在gitlab上建立個倉庫,設置爲私有的。 拿到地址就好了。
 
下面的是 以前文章裏的原文內容:

點擊建立,咱們的私有庫就建立完成了,複製咱們的私有庫地址,也就是咱們的.podspec文件裏面source所須要的地址了。


複製私有庫地址.png

source有了,可是咱們的homepage怎麼辦呢?若是有能夠訪問的高大上的介紹網頁頁面,建議使用其網址。

至此,咱們的配置文件就結束了。此時細心的看客又說了,咱們建立了git私有庫,可是尚未添加文件啊?

六、再次進入咱們的Example文件,執行pod更新指令,安裝本地庫源碼

Later@localhost:~/Desktop/私有庫建立教程/LATAlert/Example$ pod update --no-repo-update

終端截圖以下:


私有庫Example更新pod項目.png

此時進入咱們的Example文件夾,雙擊LATAlert.xcworkspace打開看看:


源碼文件導入後工程項目路徑.png

咱們的源碼已經在裏面了。

這時,能夠填寫咱們的樣例代碼來進行測試嘍。

七、添加樣例代碼,運行樣例測試

找到咱們的樣例文件LATViewController.m,引入頭文件LATAlert.h,填寫代碼,點擊屏幕時執行彈出Alert提示框,具體代碼以下:

// // LATViewController.m // LATAlert // // Created by Later on 10/14/2016. // Copyright (c) 2016 Later. All rights reserved. // #import "LATViewController.h" #import "LATAlert.h" @interface LATViewController () @end @implementation LATViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [LATAlert showAlertWithTitle:@"測試" message:@"這時測試信息" OkBlock:^{ NSLog(@"點擊了確認"); }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end

運行項目測試:


彈出Alert.png

alert信息彈出了,說明,咱們的本地運行例程是正常的。下面就須要咱們把項目發佈到git,並添加到pod了。

八、本地pod配置文件驗證

爲了保證項目的正確性,尤爲是pod配置文件的正確性,在正式提交前,咱們須要執行如下本地驗證。在本地驗證正常的狀況下,再上傳發布仍是比較穩妥的。

終端進入咱們的項目文件路徑:

Later@localhost:~/Desktop/私有庫建立教程/LATAlert/Example$ cd /Users/Later/Desktop/私有庫建立教程/LATAlert

若是如今終端正處於Example文件路徑下,也能夠執行

Later@localhost:~/Desktop/私有庫建立教程/LATAlert/Example$ cd ..

退回到上層文件夾,也就是咱們的項目文件路徑了。

執行pod本地驗證指令:

Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ pod lib lint
或者忽略警告的指令:
Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ pod lib lint --allow--warning 
 

界面顯示:


項目本地驗證錯誤1.png

若是經驗豐富,大概一看便知這裏的警告是告訴咱們這個summary簡介不是頗有內涵。此處咱們改一下咱們的podspec文件內部的簡介和描述,消除下警告。修改完的podspec文件以下。

# # Be sure to run `pod lib lint LATAlert.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 http://guides.cocoapods.org/syntax/podspec.html # Pod::Spec.new do |s| #庫名 s.name = 'LATAlert' #庫版本 s.version = '0.0.1' #庫簡短描述 s.summary = 'LATAlert pod Use.' #庫詳細描述 s.description = <<-DESC TODO: LATAlert pod Use. DESC #庫介紹主頁地址 s.homepage = 'http://mobile.fblife.com' #庫開源許可 s.license = { :type => 'MIT', :file => 'LICENSE' } #做者信息 s.author = { 'Later' => 'lshxin89@126.com' } #源碼git地址 s.source = { :git => 'https://git.oschina.net/KKLater/LATAlert.git', :tag => s.version.to_s } #庫依賴系統版本 s.ios.deployment_target = '8.0' #源碼文件配置 s.source_files = 'LATAlert/Classes/**/*' #源碼頭文件配置 s.public_header_files = 'LATAlert/Classes/*.h' #系統框架依賴 s.frameworks = 'UIKit' end

鑑因而初學者,仍是建議掌握下查看具體緣由的指令比較靠譜。具體指令也很簡單,在執行本地驗證的指令後面添加上--verbose在執行下就能夠了。

Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ pod lib lint --verbose

咱們再次執行下驗證,查看結果,BUILD SUCCEEDD,本地驗證成功:


podspec本地驗證成功.png


至此,咱們的源碼已經導入、樣例工程已經驗證執行、podspec配置文件本地已經驗證,那麼咱們是否是就能夠直接在pod裏面使用了呢?答案是確定的,仍是不行!目前只是處於本地狀態,並無發佈,因此仍是不能使用的。

九、項目工程發佈tag 0.0.1

在項目工程文件下執行git相關指令,並添加tag0.0.1,發佈到git。

Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ git remote add origin https://git.oschina.net/KKLater/LATAlert.git Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ git add . Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ git commit -a -m "0.0.1" Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ git pull origin master Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ git push origin master Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ git tag 0.0.1 Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ git push origin 0.0.1

相關指令執行結束後,此時咱們再去看咱們的git項目:


git上傳以後界面.png

十、私有庫發佈

對於開源框架,podspec文件實在cocoapod的開源管理spec項目上面的,見下圖:


cocoapod開源庫管理spec.png


在這裏的Specs文件加內,咱們能看到咱們熟悉的 AFNetworking


AFNetworking.png


可是咱們建立的是私有庫,因此咱們須要建立本身的Specs管理庫。

例如咱們要建立的是LATSpecs,首先須要再建立一個git私有庫,用於管理咱們的LATSpecs。

建立以後獲取到git地址:https://git.oschina.net/KKLater/LATPodspecs.git

在終端執行Specs建立指令:

Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ pod repo add LATSpecs https://git.oschina.net/KKLater/LATPodspecs.git

執行以後的結果是:


添加私有的Specs.png

看到這個,咱們能夠愉快的發佈了。執行發佈命令,直接發佈好了。


podspec發佈.png
Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ pod repo push LATSpecs LATAlert.podspec
若是有問題 能夠試試忽略警告
Later@localhost:~/Desktop/私有庫建立教程/LATAlert$ pod repo push LATSpecs LATAlert.podspec --allow--warnings
 

發佈成功以後,咱們來看一下咱們的LATSpecs的git項目:


podspec發佈以後的git.png

查看咱們本地的Specs庫:

直接Findle ->右鍵 -> 前往文件夾 -> 輸入:~/.cocoapods/repos ->點擊前往


查找本地的Specs庫.png

本地Specs庫.png

至此,咱們的私有庫建立發佈結束。可是咱們注意到,LATSpecs的README.md文件是空的,爲了後期的維護更新,以及團隊小夥伴的使用方便,建議完備一下信息。

完備以後的信息以下:


Readme完善.png


終端執行一下進入咱們的私有庫管理Specs,git更新提交下:

Later@localhost:~$ cd /Users/Later/.cocoapods/repos/LATSpecs Later@localhost:~/.cocoapods/repos/LATSpecs$ git add . Later@localhost:~/.cocoapods/repos/LATSpecs$ git commit -a -m "Add LATAlert" Later@localhost:~/.cocoapods/repos/LATSpecs$ git pull origin master Later@localhost:~/.cocoapods/repos/LATSpecs$ git push origin master

命令行執行結束以後,咱們再看一下咱們的git:


addReadme以後的git.png

有沒有高大上呢?

既然已經提交發布,那下面咱們新建一個CocoaPods管理的項目,來驗證一下咱們的庫吧。

十一、驗證發佈的私有庫

新建PodTest項目,並建立Podfile文件,安裝CocoaPods工程。

Podfile文件內的代碼以下:

platform :ios,'8.0' target 'PodTest' do pod 'LATAlert',:git => 'https://git.oschina.net/KKLater/LATAlert.git' end

注意:podfile文件裏要加上

source 'https://github.com/CocoaPods/Specs.git'

source 'http://112.124.41.46/tuchaoprivatecommon/xgtcprivatepodsrepo.git'

配置

若是你不配置系統的索引庫 會找不到其餘的庫 只能找到你本身建立的倉庫
最近一個命令 pod install
Nice 完成了

 

終端進入咱們的測試工程根目錄,並執行pod安裝指令:

Later@localhost:~$ cd /Users/Later/Desktop/私有庫建立教程/PodTest Later@localhost:~/Desktop/私有庫建立教程/PodTest$ pod install --no-repo-update

終端界面以下:


PodTest安裝pod.png

咱們從新打開測試項目:


測試項目成功導入了項目.png

在ViewController.m文件導入文件,並按照Example的樣例書寫代碼驗證。代碼以下:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [LATAlert showAlertWithTitle:@"測試" message:@"這時測試信息" OkBlock:^{ NSLog(@"點擊了確認"); }]; }

不一樣的是,這裏引入頭文件時,咱們使用的是:#import <LATAlert/LATAlert.h>而不是Example樣例工程裏面的引入方式:#import "LATAlert.h"。

運行PodTest項目,點擊屏幕,是否是也彈出Alert提示了呢?

至此,咱們的私有庫發佈結束。具體更詳細的信息,建議各位看客多多參考CocoaPods官網,會有更不同的體驗。英語很差的,體驗會更加深入喲!

 

 

至於更新版本的話

localhost:xgoalibrary kfj$ git add .     //新增的文件 加入倉庫 

localhost:xgoalibrary kfj$ git commit -a -m "0.1.2"      "0.1.2" -- tag 號 //  須要提交的文件上傳確認

localhost:xgoalibrary kfj$ git pull origin master         //下啦最新資源

localhost:xgoalibrary kfj$ git push origin master         //上傳本地最新代碼

localhost:xgoalibrary kfj$ git tag 0.1.2      //打tag

localhost:xgoalibrary kfj$ git push --tags   

 

localhost:xgoalibrary kfj$ pod lib lint --allow-warnings     //驗證本地庫是否正確    lib  換成 spec 的 話 就是 就是驗證遠程服務器的 庫 是否正確

localhost:xgoalibrary kfj$ pod repo push XGOALibrary XGOALibrary.podspec --allow-warnings

 

命令行指令:

history    查看本身用過的歷史指令

pod repo list   查看本地repo私有庫資源信息

open ~/.cocoapods   打開本地私有庫資源的位置

相關文章
相關標籤/搜索