[Cocoapods]使用Cocoapods + Github托管代碼

記錄使用cocoapods托管代碼的一些步驟

一. 新建項目

Github上新建項目, 新建的時候要勾選許可類型:

如果是已有的項目, 可以從其他地方copy一個LICENSE文件, 放到項目的根目錄下(與READEME.md同級)

然后, 使用Sourcetree, 將項目克隆到本地:

PS: 這里我使用的是Sourcetree, 也可以使用其他的方式克隆

新建一個項目, 隨便添加一個文件( 你需要支持cocoapods的文件, 這里以LZTool類為例 ):

推送到Github:

關于Cocoapods的使用, 可參考這篇文章:[Cocoapods]第三方管理工具Cocoapods使用

二. 創建podspec文件

打開終端, 進入到項目的根目錄:

 cd /Users/mac/Desktop/PodTest

然后執行下面的指令新建Podspec文件:

pod spec create LZTool

或者使用touch指令也可以, 文件名稱要寫全:

// 文件名稱需要添加后綴
touch LZTool.podspec

這里的LZTool, 就是你在使用cocoapods時寫到Podfile文件內的名稱;

pod spec create 文件名稱

執行結果:

此時, 本地文件夾下就多了一個文件: LZTool.podspec
在終端, 打開這個文件(或者使用其他的方式打開, 例如: 文本編輯器, Xcode, VSCode等 ):

vi LZTool.podspec

打開后, 會發現里面有很多內容, 如下 :

#
#  Be sure to run `pod spec lint LZTool.podspec' to ensure this is a
#  valid spec and to remove all comments including this before submitting the spec.
#
#  To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html
#  To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#

Pod::Spec.new do |s|

  # ―――  Spec Metadata  ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  These will help people to find your library, and whilst it
  #  can feel like a chore to fill in it's definitely to your advantage. The
  #  summary should be tweet-length, and the description more in depth.
  #

  s.name         = "LZTool"
  s.version      = "0.0.1"
  s.summary      = "A short description of LZTool."

  # 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
                   DESC

  s.homepage     = "http://EXAMPLE/LZTool"
  # s.screenshots  = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"


  # ―――  Spec License  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Licensing your code is important. See http://choosealicense.com for more info.
  #  CocoaPods will detect a license file if there is a named LICENSE*
  #  Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
  #

  s.license      = "MIT (example)"
  # s.license      = { :type => "MIT", :file => "FILE_LICENSE" }


  # ――― Author Metadata  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the authors of the library, with email addresses. Email addresses
  #  of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also
  #  accepts just a name if you'd rather not provide an email address.
  #
  #  Specify a social_media_url where others can refer to, for example a twitter
  #  profile URL.
  #

  s.author             = { "LQQZYY" => "592852848@qq.com" }
  # Or just: s.author    = "LQQZYY"
  # s.authors            = { "LQQZYY" => "592852848@qq.com" }
  # s.social_media_url   = "http://twitter.com/LQQZYY"

  # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  If this Pod runs only on iOS or OS X, then specify the platform and
  #  the deployment target. You can optionally include the target after the platform.
  #

  # s.platform     = :ios
  # s.platform     = :ios, "5.0"

  #  When using multiple platforms
  # s.ios.deployment_target = "5.0"
  # s.osx.deployment_target = "10.7"
  # s.watchos.deployment_target = "2.0"
  # s.tvos.deployment_target = "9.0"


  # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the location from where the source should be retrieved.
  #  Supports git, hg, bzr, svn and HTTP.
  #

  s.source       = { :git => "http://EXAMPLE/LZTool.git", :tag => "#{s.version}" }


  # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  CocoaPods is smart about how it includes source code. For source files
  #  giving a folder will include any swift, h, m, mm, c & cpp files.
  #  For header files it will include any header in the folder.
  #  Not including the public_header_files will make all headers public.
  #

  s.source_files  = "Classes", "Classes/**/*.{h,m}"
  s.exclude_files = "Classes/Exclude"

  # s.public_header_files = "Classes/**/*.h"


  # ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  A list of resources included with the Pod. These are copied into the
  #  target bundle with a build phase script. Anything else will be cleaned.
  #  You can preserve files from being cleaned, please don't preserve
  #  non-essential files like tests, examples and documentation.
  #

  # s.resource  = "icon.png"
  # s.resources = "Resources/*.png"

  # s.preserve_paths = "FilesToSave", "MoreFilesToSave"


  # ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Link your library with frameworks, or libraries. Libraries do not include
  #  the lib prefix of their name.
  #

  # s.framework  = "SomeFramework"
  # s.frameworks = "SomeFramework", "AnotherFramework"

  # s.library   = "iconv"
  # s.libraries = "iconv", "xml2"


  # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  If your library depends on compiler flags you can set them in the xcconfig hash
  #  where they will only apply to your library. If you depend on other Podspecs
  #  you can include multiple dependencies to ensure it works.

  # s.requires_arc = true

  # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
  # s.dependency "JSONKit", "~> 1.4"

end

文件里劃分了好幾個區:


1. Spec Metadata : 元數據規范

這些內容是你的類庫信息

  • s.name : 類庫的名稱
  • s.version : 類庫的版本號
  • s.summary : 類庫的摘要(簡介)
  • s.description : 描述, 用于生成標簽和提高搜索結果, 主要是類庫的功能等, 盡量簡短; 注意, 需要寫在兩個DESC中間
  • s.homepage : 類庫的地址鏈接
    s.summary- s.screenshots : 效果圖鏈接

2. Spec License : 開源許可證

3. Author Metadata : 作者信息
  • s.author :作者
  • s.social_media_url : 個人主頁地址

4. Platform Specifics : 平臺信息
  • s.platform : 平臺 例如: iOS , tvOS, watchOS等,
    在平臺信息后可以跟上需要的最低版本, 例如:
  • s.platform = :ios, "8.0"
    如果可以支持多個平臺, 可使用下面的方式指定:
  • s.ios.deployment_target = "5.0"
  • s.osx.deployment_target = "10.7"
  • s.watchos.deployment_target = "2.0"
  • s.tvos.deployment_target = "9.0"

5. Source Location : 文件地址

從哪里可以獲得這些文件, 支持 git, hg, bzr, svn and HTTP.

  • s.source : 鏈接地址

6. Resources : 資源
  • s.resource = "icon.png":單個資源
  • s.resources = "Resources/*.png" : 某個文件夾下的多個資源
  • s.preserve_paths = "FilesToSave", "MoreFilesToSave" : 需要保存的文件

7. Source Code : 共享源文件
  • s.source_files : 文件的相對路徑
  • s.public_header_files : 公開的頭文件, 如果不設置這個, 默認公開所有的頭文件
  • s.exclude_files = "Classes/Exclude" 排除不需要的文件

8. Project Linking : 鏈接庫和框架
  • s.framework = "SomeFramework" 單個framework
  • s.frameworks = "SomeFramework", "AnotherFramework" 多個庫framework
  • s.library = "iconv" 指定導入的庫,比如sqlite3
  • s.libraries = "iconv", "xml2" 多個庫

9. Project Settings : 工程設置
  • s.requires_arc = true 需要ARC
  • s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" } 項目配置
  • s.dependency "JSONKit", "~> 1.4" 需要其他第三方庫的支持,如果有多個第三方庫,就寫多個 s.dependency

完成后, 執行下面的指令來驗證這個文件是否可用:

pod lib lint LZTool.podspec

如果有錯誤, 根據錯誤提示, 進行修改; 如果僅僅是警告, 不想修改的話, 可以使用下面的指令來忽略:

pod lib lint LZTool.podspec —allow-warnings

最后, 輸出下面這個, 即是驗證通過:

這里如果遇到錯誤, 可以參考[Cocoapods]項目添加Cocoapods支持遇到的坑中的解決思路.

三. 新建版本

來到Github 上, 創建release版本
選擇release :

如果是個新的項目的話, 這里是沒有任何版本信息的, 點擊create a new release :

注意: 這里的Tag verson 一定要和上面設置的s.version以及s.source 中的 tag保持一致.

填好之后, 點擊下面的Public release 即可完成!

四. 注冊cocoapods賬戶

接下來輸入以下指令, 來鏈接到你的cocoapods賬戶:
如果是首次使用:

// 郵箱  用戶名   描述信息
pod trunk register 302934443@qq.com 'LQQ' --description='Test1'

如果已經注冊, 可使用下面的指令:

pod trunk register eloy@example.com --description='Work Laptop'
pod trunk register eloy@example.com

不出錯的話會輸出:

根據提示, 到你的郵箱里去確認郵件即可!

注意: 這里的郵箱開始我使用的是163的郵箱, 好久都沒有收到確認郵件, 然后改為qq郵箱, 立馬就受到確認郵件了, 所以, 建議使用qq郵箱來注冊.

確認結果如下所示:


這里如果遇到錯誤, 可以參考[Cocoapods]項目添加Cocoapods支持遇到的坑中的解決思路.

確認之后, 可以使用下面的指令來確認賬號是否可用:

pod trunk me

如果可用, 會輸出相關的信息:


五. 上傳代碼到Cocoapods

以上全部完成后, 最后一步就是上傳代碼到Cocoapods了;
首先, 使用下面的指令來檢查文件是否可用:

pod spec lint

如果沒有錯誤, 會輸出:

PS: 如果有警告, 而我們又不想處理, 可用—allow-warnings忽略, 即:

pod spec lint —allow-warnings

這里如果遇到錯誤, 可以參考[Cocoapods]項目添加Cocoapods支持遇到的坑中的解決思路.

最后就是上傳代碼了, 使用下面的指令進行上傳:

pod trunk push LZTool.podspec

上傳的時間可能會有點長, 耐心等待, 成功后會輸出:

上傳成功

PS: 如果這里因為有警告而導致失敗, 可以在后面添加--allow-warnings來忽略警告, 即:

pod trunk push LZTool.podspec --allow-warnings

然后, 可用使用下面的指令來查看是否可用:

pod search LZTool

注意: 這里有一個坑, 上面顯示成功, 但是我在使用指令搜索的時候, 怎么都搜不到, 然后新建了一個工程, 添加pod 'LZTool'至Podfile**, 文件, 然后就能正常下載文件:

六. 更新

如果我們需要更新自己的類庫, 只需要將最新的代碼提交到Github, 然后創建新的release版本, 注意: 這里的版本要和podspec文件內的s.version一致, 然后再執行一遍下面的指令即可:

pod trunk push LZTool.podspec --allow-warnings

最后附上本次實例的podspec文件內容:

Pod::Spec.new do |s|

  s.name         = "LZTool"
  s.version      = "0.0.1"
  s.summary      = " This is some summary for LZTool"

  s.description  = <<-DESC 
                          LZTool 是一個用于保存一些常用工具類的工具
                   DESC

  s.homepage     = "https://github.com/LQQZYY/LZTool"

  s.license      = "MIT"

  s.author    = "LQQZYY"
  s.platform     = :ios, "8.0"

  s.source       = { :git => "https://github.com/LQQZYY/PodTest.git", :tag => "0.0.1" }
  s.source_files = "LZTool"

  s.requires_arc = true

end

最后, 對于.podspec文件的編寫, 可以下載其他的第三方庫來學習里面的.podspec的編寫技巧.

7. 刪除

如果我們想刪除一個已有的庫,可以使用下面的指令:

delete 刪除某個版本
deprecate 棄用某個倉庫,不會刪除,但是是下載時會有提示已棄用

/// 刪除,后面需要跟上版本號
$ pod trunk delete LZScaner 0.0.1

/// 棄用某個庫
$ pod trunk deprecate LZScaner

如果使用 delete 指令會有下面的提醒:

WARNING: It is generally considered bad behavior to remove versions of a Pod that others are depending on!
Please consider using the `deprecate` command instead.

Are you sure you want to delete this Pod version?
> 

這時,輸入y,回車即可!

8. 添加子目錄

有可能一個類庫功能很多,但是實際中只需要其中的某部分,可以設置pod倉庫子目錄,主要使用到了:

// 設置默認的目錄
s.default_subspec = 'OBJ'
// 設置一個子目錄
s.subspec 'OBJ' do |obj|
    obj.source_files  = "LQWebView", "LQWebView/obj/LQWebView/*.{h,m}"
  end

// 如果有多個可以繼續添加
s.subspec 'A' do |a|
    a.source_files  = "LQWebView", "LQWebView/obj/LQWebView/*.{h,m}"
  end

在使用的時候,在Podfile文件添加:

pod 'LQWebView/SF'

如果不加/文件名,將加載設置的默認文件;

一個完整的文件


Pod::Spec.new do |s|

  s.name         = "LQWebView"
  s.version      = "1.5.0"
  s.summary      = "對WKWebView 的封裝,繼承自UIView"
  s.description  = <<-DESC
  對WKWebView 的封裝,繼承自UIView,封裝了常用方法,方便使用!
                   DESC

  s.homepage     = "https://github.com/LQi2009/LQWebView"
  s.license      = "MIT"
  s.author             = { "LiuQiqiang" => "lqq200912408@163.com" }

  s.platform     = :ios, "9.0"

  s.source       = { :git => "https://github.com/LQi2009/LQWebView.git", :tag => "#{s.version}" }


  s.requires_arc = true

  s.default_subspec = 'OBJ'
  
  s.subspec 'OBJ' do |obj|
    obj.source_files  = "LQWebView", "LQWebView/obj/LQWebView/*.{h,m}"
  end

  s.subspec 'SF' do |sf|
    sf.source_files  = "LQWebView", "LQWebView/sf/LQWebView/*.{swift}"
    s.swift_version = "4.1"
  end

end

參考文章

從零開始,讓你的框架支持CocoaPods

把自己Github上的代碼添加Cocoapods支持

刪除

手動刪除,打開Finder,前往文件夾,輸入路徑:

~/.cocoapods/repos/

會打開本地cocoapods倉庫,找到對應的文件刪除即可

(完)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容