記錄使用cocoapods托管代碼的一些步驟
一. 新建項目
在Github上新建項目, 新建的時候要勾選許可類型:
如果是已有的項目, 可以從其他地方copy一個LICENSE文件, 放到項目的根目錄下(與READEME.md同級)
然后, 使用Sourcetree, 將項目克隆到本地:
PS: 這里我使用的是Sourcetree, 也可以使用其他的方式克隆
新建一個項目, 隨便添加一個文件( 你需要支持cocoapods的文件, 這里以LZTool類為例 ):
推送到Github:
關(guān)于Cocoapods的使用, 可參考這篇文章:[Cocoapods]第三方管理工具Cocoapods使用
二. 創(chuàng)建podspec文件
打開終端, 進(jìn)入到項目的根目錄:
cd /Users/mac/Desktop/PodTest
然后執(zhí)行下面的指令新建Podspec文件:
pod spec create LZTool
或者使用touch指令也可以, 文件名稱要寫全:
// 文件名稱需要添加后綴
touch LZTool.podspec
這里的LZTool, 就是你在使用cocoapods時寫到Podfile文件內(nèi)的名稱;
pod spec create 文件名稱
執(zhí)行結(jié)果:
此時, 本地文件夾下就多了一個文件: LZTool.podspec
在終端, 打開這個文件(或者使用其他的方式打開, 例如: 文本編輯器, Xcode, VSCode等 ):
vi LZTool.podspec
打開后, 會發(fā)現(xiàn)里面有很多內(nèi)容, 如下 :
#
# 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
文件里劃分了好幾個區(qū):
1. Spec Metadata : 元數(shù)據(jù)規(guī)范
這些內(nèi)容是你的類庫信息
- s.name : 類庫的名稱
- s.version : 類庫的版本號
- s.summary : 類庫的摘要(簡介)
- s.description : 描述, 用于生成標(biāo)簽和提高搜索結(jié)果, 主要是類庫的功能等, 盡量簡短; 注意, 需要寫在兩個DESC中間
- s.homepage : 類庫的地址鏈接
s.summary- s.screenshots : 效果圖鏈接
2. Spec License : 開源許可證
- s.license: 許可類型, 常用的是 MIT
更多可參考: http://choosealicense.com
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 : 公開的頭文件, 如果不設(shè)置這個, 默認(rèn)公開所有的頭文件
- s.exclude_files = "Classes/Exclude" 排除不需要的文件
8. Project Linking : 鏈接庫和框架
- s.framework = "SomeFramework" 單個framework
- s.frameworks = "SomeFramework", "AnotherFramework" 多個庫framework
- s.library = "iconv" 指定導(dǎo)入的庫,比如sqlite3
- s.libraries = "iconv", "xml2" 多個庫
9. Project Settings : 工程設(shè)置
- s.requires_arc = true 需要ARC
- s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" } 項目配置
- s.dependency "JSONKit", "~> 1.4" 需要其他第三方庫的支持,如果有多個第三方庫,就寫多個 s.dependency
完成后, 執(zhí)行下面的指令來驗證這個文件是否可用:
pod lib lint LZTool.podspec
如果有錯誤, 根據(jù)錯誤提示, 進(jìn)行修改; 如果僅僅是警告, 不想修改的話, 可以使用下面的指令來忽略:
pod lib lint LZTool.podspec —allow-warnings
最后, 輸出下面這個, 即是驗證通過:
這里如果遇到錯誤, 可以參考[Cocoapods]項目添加Cocoapods支持遇到的坑中的解決思路.
三. 新建版本
來到Github 上, 創(chuàng)建release版本
選擇release :
如果是個新的項目的話, 這里是沒有任何版本信息的, 點擊create a new release :
注意: 這里的Tag verson 一定要和上面設(shè)置的s.version以及s.source 中的 tag保持一致.
填好之后, 點擊下面的Public release 即可完成!
四. 注冊cocoapods賬戶
接下來輸入以下指令, 來鏈接到你的cocoapods賬戶:
如果是首次使用:
// 郵箱 用戶名 描述信息
pod trunk register 302934443@qq.com 'LQQ' --description='Test1'
如果已經(jīng)注冊, 可使用下面的指令:
pod trunk register eloy@example.com --description='Work Laptop'
pod trunk register eloy@example.com
不出錯的話會輸出:
根據(jù)提示, 到你的郵箱里去確認(rèn)郵件即可!
注意: 這里的郵箱開始我使用的是163的郵箱, 好久都沒有收到確認(rèn)郵件, 然后改為qq郵箱, 立馬就受到確認(rèn)郵件了, 所以, 建議使用qq郵箱來注冊.
確認(rèn)結(jié)果如下所示:
這里如果遇到錯誤, 可以參考[Cocoapods]項目添加Cocoapods支持遇到的坑中的解決思路.
確認(rèn)之后, 可以使用下面的指令來確認(rèn)賬號是否可用:
pod trunk me
如果可用, 會輸出相關(guān)的信息:
五. 上傳代碼到Cocoapods
以上全部完成后, 最后一步就是上傳代碼到Cocoapods了;
首先, 使用下面的指令來檢查文件是否可用:
pod spec lint
如果沒有錯誤, 會輸出:
PS: 如果有警告, 而我們又不想處理, 可用—allow-warnings忽略, 即:
pod spec lint —allow-warnings
這里如果遇到錯誤, 可以參考[Cocoapods]項目添加Cocoapods支持遇到的坑中的解決思路.
最后就是上傳代碼了, 使用下面的指令進(jìn)行上傳:
pod trunk push LZTool.podspec
上傳的時間可能會有點長, 耐心等待, 成功后會輸出:
PS: 如果這里因為有警告而導(dǎo)致失敗, 可以在后面添加--allow-warnings來忽略警告, 即:
pod trunk push LZTool.podspec --allow-warnings
然后, 可用使用下面的指令來查看是否可用:
pod search LZTool
注意: 這里有一個坑, 上面顯示成功, 但是我在使用指令搜索的時候, 怎么都搜不到, 然后新建了一個工程, 添加pod 'LZTool'至Podfile**, 文件, 然后就能正常下載文件:
六. 更新
如果我們需要更新自己的類庫, 只需要將最新的代碼提交到Github, 然后創(chuàng)建新的release版本, 注意: 這里的版本要和podspec文件內(nèi)的s.version一致, 然后再執(zhí)行一遍下面的指令即可:
pod trunk push LZTool.podspec --allow-warnings
最后附上本次實例的podspec文件內(nèi)容:
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文件的編寫, 可以下載其他的第三方庫來學(xué)習(xí)里面的.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. 添加子目錄
有可能一個類庫功能很多,但是實際中只需要其中的某部分,可以設(shè)置pod倉庫子目錄,主要使用到了:
// 設(shè)置默認(rèn)的目錄
s.default_subspec = 'OBJ'
// 設(shè)置一個子目錄
s.subspec 'OBJ' do |obj|
obj.source_files = "LQWebView", "LQWebView/obj/LQWebView/*.{h,m}"
end
// 如果有多個可以繼續(xù)添加
s.subspec 'A' do |a|
a.source_files = "LQWebView", "LQWebView/obj/LQWebView/*.{h,m}"
end
在使用的時候,在Podfile文件添加:
pod 'LQWebView/SF'
如果不加/文件名,將加載設(shè)置的默認(rèn)文件;
一個完整的文件
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
參考文章
刪除
手動刪除,打開Finder,前往文件夾,輸入路徑:
~/.cocoapods/repos/
會打開本地cocoapods倉庫,找到對應(yīng)的文件刪除即可