建立podSpec,使用pod管理第三方庫

 提要:ios

  podfile文件會先讀取.podspec文件,根據.podspec文件的指向來下載第三方庫到項目中。c++

  本文先經過1、2、三這三個步驟講解了如何創建一個"podspec文件在本地.cocoaPod庫,第三方庫在遠程機器"的例子。git

  後文中的第四項,講解了"podspec文件在本地項目中,第三方庫在遠程機器"的設置方法;最後講了"podspec文件在本地項目中,第三方庫也在本地項目中"的設置方法。第五項,講解了"podspec文件的語法知識"。github

 

正文講解:正則表達式

1、建立須要pod管理的第三方庫sql

(1) 本地建立第三方庫起名爲lvPodLibrary,用命令建立以下:swift

sheron_lv@MacLv:~/codeLv/github$ pod lib create lvPodLibrary //輸入命令

根據提示回答四個問題,1.是否須要一個例子工程;2.選擇一個測試框架;3.是否基於View測試;4.類的前綴。框架

加入咱們要用Pod管理的類,這裏直接起名爲lvPodLibrary.h和lvPodLibrary.m。測試

 

(2)在github上建立New repository,即遠端lvPodLibrary庫。拿到地址,好比個人是https://github.com/SheronLv/lvPodLibrary.git。把上面建立的本地庫push到遠端:ui

sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ git add .
sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ git commit -m "Initial Commit of Library"
On branch master
nothing to commit, working directory clean
sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ git remote add origin https://github.com/SheronLv/lvPodLibrary.git //添加遠端倉庫
sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ git push origin master //提交到遠端倉庫

由於podspec文件中獲取第三方庫lvPodLibrary這個Git版本控制的項目還須要tag號,因此咱們要打上一個tag

sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ git tag -m "first release" "0.1.0"
sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ git push --tags //推送tag到遠端倉庫

也就是說,我如今推送的lvPodLibrary第三方庫是0.1.0版本的。

(3)編輯lvPodLibrary.podspec文件,或者若是這個庫是經過其餘方式建立的沒有這個文件的話,建立lvPodLibrary.podspec文件

sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ pod spec create lvPodLibrary https://github.com/SheronLv/lvPodLibrary.git

內容以下:

Pod::Spec.new do |s|
  s.name         = "lvPodLibrary" #名稱 s.version = "0.1.0" s.summary = "Just Testing" #簡短介紹,下面是詳細介紹 s.description = <<-DESC
            Testing Testing Testing DESC s.homepage = "https://github.com/SheronLv/lvPodLibrary" s.license = 'MIT' s.author = { "Sheron lv" => "email@address.com" } s.platform = :ios, "7.0"
s.source = { :git => "https://github.com/SheronLv/lvPodLibrary.git", :tag => s.version } s.source_files = "lvPodLibrary", "lvPodLibrary/**/*.{h,m}" end

檢驗.podspec文件是否可用可用

sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ pod lib lint -> lvPodLibrary (0.1.0)

lvPodLibrary passed validation. // 此提示信息表示可用

而後把全部文件push到遠端。

2、建立podSpec倉庫

(1)在github上建立New repository,做爲podSpec的遠程倉庫,以下圖,我在github上建立了一個名爲lvPodSpec的倉庫。

(3)將遠程podSpec倉庫添加到本地倉庫目錄下,即~/.cocoapods/repos目錄下。

sheron_lv@MacLv:~$ pod repo add lvPodSpec https://github.com/SheronLv/lvPodSpec.git//輸入命令

Cloning spec repo `lvPodSpec` from `https://github.com/SheronLv/lvPodSpec.git` //執行過程、結果

此時本地~/.cocoapods/repos目錄下已經有了名爲lvPodSpec的倉庫。經過pod search命令能夠查到這個庫的信息:

sheron_lv@MacLv:~/Desktop/lvDemo$ pod search lvPodLibrary

//查到信息以下

-> lvPodLibrary (0.1.0)

   Just Testing

   pod 'lvPodLibrary', '~> 0.1.0'

   - Homepage: https://github.com/SheronLv/lvPodLibrary

   - Source:   https://github.com/SheronLv/lvPodLibrary.git

   - Versions: 0.1.0 [lvPodSpec repo]

把以前的lvPodLibrary.podspec文件拷貝到本地的lvPodSpec項目中.

而後把全部的內容push到遠程lvPodSpec下。(其實能夠刪掉lvPodLibrary項目下的lvPodLibrary.podspec文件,沒有指向它。但如今能夠先留着,後面練習直接指向第三方庫的.podspec文件可使用)

(注意:每次更新版本tag,把上級目錄的名字也更新,或者創建新的文件夾好比0.1.一、0.1.2,不然沒法找到這個版本的.podspec)

3、在新的項目中使用pod管理第三方庫

編寫這個項目的Podfile以下:

 source 'https://github.com/SheronLv/lvPodSpec.git'   //注:若是不添加source句有可能找不到,就會出錯

platform:ios,"7.0"

inhibit_all_warnings!
target "lvDemo" do
        pod 'lvPodLibrary','0.1.0'
end

運行pod install,項目中就出現了要用的lvPodLibrary項目

總結:通過屢次實踐,以上的三大步驟,實際上是如下指向關係:

1)須要使用第三方庫的項目中的Podfile指向本地的倉庫中的.podspec文件

    例如 Podfile中關鍵代碼

      pod 'lvPodLibrary','0.1.5'

2)本地的.podspec文件指向遠程倉庫的第三方庫的地址
  例如 本地倉庫中~/.cocoapods/repos/項目lvPodSpec的lvPodLibrary.podspec文件
     s.name         ="lvPodLibrary" 
   s.version      = 「0.1.5"
s.source       = { :git => "https://github.com/SheronLv/lvPodLibrary.git", :tag => s.version }
s.source_files  = "lvPodLibrary"
(3)遠程倉庫lvPodLibrary.git

    關鍵是:

    push時要打tag;

    存放第三方庫lvPodLibrary的目錄結構與.podspec文件中指定的s.source_files一致。

 

咱們知道,運行pod install時,運行的是本地.cocoapods下的.podspec,遠程對應的.podspec和在你要管理的第三庫裏建立的.podspec都是不起做用的。

4、 PS:  podfile不一樣寫法

(1)

pod 'lvPodLibrary'  , :git =>'https://github.com/SheronLv/lvPodLibrary.git', :tag => '0.1.2'

這樣,pod會去找遠程git地址上,tag是0.1.2的那次提交的代碼裏的lvPodLibrary.podspec文件,根據lvPodLibrary.podspec文件下載對應的第三方庫。

(2)將上文中建立的lvPodLibrary.podspec文件複製到須要依賴該庫代碼的項目目錄下,如本項目demo/spec/lvPodLibrary.podspec,而後修改Podfile中對該庫的依賴爲:

pod 'lvPodLibrary'  , :podspec => './spec/lvPodLibrary.podspec'

執行pod install也會拉到對應的第三方庫的代碼。

(3)若是第三方庫的代碼lvPodLibrary不想放到遠程,能夠經過使用path的方式將代碼添加到pod中,以下所示: 

#Podfile中這樣寫
pod 'lvPodLibrary'  , :path => './LocalPod/lvPodLibrary.podspec' #.podspec文件中這樣寫
s.source       = { :tag => s.version}
s.source_files  = "lvPodLibrary"

注意:這樣寫時,將文件lvPodLibrary.podspec和第三方庫代碼lvPodLibrary並列放在本項目的同一文件夾下,好比本項目是LocalPod文件夾,不然找不到第三方庫。

 5、 podspec語法知識點

Pod::Spec.new do |s|
  s.name         = "NVUtils"
  s.version      = "0.0.1"
  s.summary      = "NVUtils repo"

  s.description  = <<-DESC
                          NVUtils repo
                   DESC

  s.homepage     = "http://XXXX/services"
  s.license      = 'MIT'
  s.author       = { "SheronLv" => "lvxueyin@hotmail.com" }
  s.platform     = :ios, "7.0"


  s.source_files  = "NVUtils/*.{h,m}"
  s.resources = 'NVUtils/*.{xib,png}'
//s.source   = { :git => 'https://github.com/ADVProgressBar.git', :commit => 'f17b15c15574d6d101cd5fcfd58239e16e806647' } 
s.requires_arc = true s.dependency 'Core1' s.dependency 'Core2' end

   s.name  聲明庫的名稱

   s.summary 庫的簡短說明文檔

   s.homepage  聲明庫的主頁(只是告訴了這個url,運行podfile並不會據此把庫push到對應的url上)

   s.version  庫原代碼的版本

   s.license 所採用的受權版本

   s.author 庫的做者

   s.source 原代碼的地址

   s.source_files 包含全部源代碼的目錄,目錄的層級關係必定要跟代碼文件的保持一致,最後一部分*.{h,m}是一個相似正則表達式的字符串,表示匹配全部以.h和.m爲擴展名的文件。

   s.resources NVUtils/目錄下還有一個NVUtils.bundle目錄,該目錄存放一些資源文件(如圖片等),這些文件並不須要進行編譯。可使用s.resourcs聲明。

   s.dependency 本庫依賴的其餘的第三方庫

 s.vendored_libraries 指定外部的靜態庫

  對比記憶如下四個設置:

 s.libraries  表示這個pod依賴的 蘋果官方的庫,也就是相似libstdc++.a ,libsqlite.a 等等的a文件;

 

   s.vendored_libraries 就表示用戶本身的a文件,好比新浪微博SDK的libWeiboSDK.a ;

 

   s.frameworks 表示pod依賴的 蘋果的framework, 好比 UIKit,SystemConfiguration等等

 

   s.vendored_frameworks, 表示pod依賴的本身的framework,好比 QQSDK的TencentOpenAPI.framework;
 
總結以下(引自: https://www.jianshu.com/p/5c67c41766f6):
#  基本信息的配置
name:框架名
version:當前版本(注意,是當前版本,假如你後續更新了新版本,須要修改此處)
summary:簡要描述,在pod search的時候會顯示該信息。
description:詳細描述
homepage:頁面連接
license:開源協議
author:做者
platform:支持最低ios版本
swift_version : swift對應的版本


# 源文件的配置
source:源碼git地址
source_files:源文件(能夠包含.h和.m)
subspec:子庫
public_header_files:頭文件(.h文件)
resource_bundles:資源文件(配置的文件會放到你本身指定的bundle中)

# 依賴的配置
frameworks:依賴的系統框架
vendored_frameworks:依賴的非系統框架
libraries:依賴的系統庫
vendored_libraries:依賴的非系統的靜態庫
dependency:依賴的三方庫
相關文章
相關標籤/搜索