1、什么是spec文件
官方解释如下:
“A specification describes a version of Pod library. It includes details about where the source should be fetched from, what files to use, the build settings to apply, and other general metadata such as its name, version, and description.”
cocoapods 官网地址:https://guides.cocoapods.org/syntax/podspec.html
2、创建spec 文件
pod spec create YRTestCocoapodSpec
YRTestCocoapodSpec 是创建安的spec文件的名字,
通常和cocoapods 的库文件名字一直,eg:Reachability 这个框架的spec名字也是Reachability
新建的YRTestCocoapodSpec.spec 文件内容如下:
说明: 语句前的 "#" 表示的是注释
#
# Be sure to run `pod spec lint YRTestCocoapodSpec.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 = "YRTestCocoapodSpec"
s.version = "0.0.1"
s.summary = "A short description of YRTestCocoapodSpec."
# 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.description 的内容填写最好比s.summary 长,否则易报错报异常
s.homepage = "http://EXAMPLE/YRTestCocoapodSpec"
# 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)" // 协议一般直接填"MIT" 即可
# 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 = { "mango" => "690852195@qq.com" }
# Or just: s.author = "mango"
# s.authors = { "mango" => "690852195@qq.com" }
# s.social_media_url = "http://twitter.com/mango"
# ――― 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/YRTestCocoapodSpec.git", :tag => "#{s.version}" }
注意:s.source 指的就是远程仓库的源码,tag 对应的就是 打的标签
# ――― 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}"
注意1:cocoapods只会下载远程仓库s.source 中指定路径s.source_files 对应的文件, **代表匹配所有目录, *.{h,m} 代表匹配所有的.h 和.m 文件,
注意2:Classes 代表的是和远程仓库的中的spec 同一目录路径下的Classes这个文件夹
#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
3、cocoapod 官方建议spec 文件书写内容方式有两种如下:
精简版(在编写自己的spec 文件时至少要填写以下内容)
Pod::Spec.new do |spec|
spec.name = 'Reachability'
spec.version = '3.1.0'
spec.license = { :type => 'BSD' }
spec.homepage = 'https://github.com/tonymillion/Reachability'
spec.authors = { 'Tony Million' => 'tonymillion@gmail.com' }
spec.summary = 'ARC and GCD Compatible Reachability Class for iOS and OS X.'
spec.source = { :git => 'https://github.com/tonymillion/Reachability.git', :tag => 'v3.1.0' }
spec.source_files = 'Reachability.{h,m}'
spec.framework = 'SystemConfiguration'
end
1>、编写时根据自己的实际情况调整修改对应的项目即可
2>、homepage 和 source 其实只有一个 .git 差异.
详细版
Pod::Spec.new do |spec|
spec.name = 'Reachability'
spec.version = '3.1.0'
spec.license = { :type => 'BSD' }
spec.homepage = 'https://github.com/tonymillion/Reachability'
spec.authors = { 'Tony Million' => 'tonymillion@gmail.com' }
spec.summary = 'ARC and GCD Compatible Reachability Class for iOS and OS X.'
spec.source = { :git => 'https://github.com/tonymillion/Reachability.git', :tag => 'v3.1.0' }
spec.module_name = 'Rich'
spec.ios.deployment_target = '9.0'
spec.osx.deployment_target = '10.10'
spec.source_files = 'Reachability/common/*.swift'
spec.ios.source_files = 'Reachability/ios/*.swift', 'Reachability/extensions/*.swift'
spec.osx.source_files = 'Reachability/osx/*.swift'
spec.framework = 'SystemConfiguration'
spec.ios.framework = 'UIKit'
spec.osx.framework = 'AppKit'
spec.dependency 'SomeOtherPod'
end
如果还不清楚可以查看随便找一第三方库,在github上找到作者的原始文件参考,比如:https://github.com/lingyfh/AFNetwork.git

4、使用CocoaPods Trunk上传spec 文件到cocoapod的远程spec仓库
-
1、什么是CocoaPods Trunk
官方解释如下:
CocoaPods Trunk is an authentication and CocoaPods API service. To publish new or updated libraries to CocoaPods for public release you will need to be registered with Trunk and have a valid Trunk session on your current device. You can read about Trunk's history and development on the blog, and about private pods for yourself or your team.CocoaPods Trunk is available starting with CocoaPods 0.33. A collection of commands under
pod trunk
automate the deployment and management of your Podspecs. At any time you can runpod trunk [command] --help
to see inline help. -
2、注册trunk
pod trunk register 690852195@qq.com 'mango' --verbose // 690852195@qq.com 作者的邮箱 // mango 作者的名称 // --verbose 固定参数
邮箱以及用户名请对号入座。用户名我使用的是Github上的用户名。--verbose参数是为了便于输出注册过程中的调试信息。执行上面的语句后,你的邮箱将会受到一封带有验证链接的邮件,如果没有请去垃圾箱找找,有可能被屏蔽了。点击邮件的链接就完成了trunk注册流程。使用下面的命令可以向trunk服务器查询自己的注册信息:
注册pod trunk 成功.png
-
3、 上传 spec文件到cocoapodsSpec 远程仓库
pod trunk push YRTestCocoapodSpec.spec

网友评论