现在大部分iOS项目都是在用Cocoapods来管理项目了,但是当我们自己写的一个公有或者私有项目的时候,想要支持Cocoapods,那应该怎么做呢?本教程手把手的教大家如何创建podsepc
步骤
-
github上创建自己的共有项目
-
创建自己的Xcode工程,并提交到该repository
-
创建podspec文件
我们cd当我们项目的根目录下。本项目中就是上图的文件夹下。执行如下命令:pod spec create OceanFish [email protected]:FlyOceanFish/CreateOwnSpec.git
编辑我们的podspec文件
#
# Be sure to run `pod spec lint OceanFish.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 = "OceanFish"
s.version = "0.0.1"
s.summary = "make a Spec"
# 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
手把手叫你如何创建自己的Spec
DESC
s.homepage = "https://github.com/FlyOceanFish"
# 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 = { :type => "MIT", :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 = { "OceanFish" => "[email protected]" }
# Or just: s.author = "frances"
# s.authors = { "frances" => "[email protected]" }
# s.social_media_url = "http://twitter.com/frances"
# ――― 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, "10.3"
# 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 => "https://github.com/FlyOceanFish/CreateOwnSpec.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 = "FlyOceanFish", "FlyOceanFish/**/*.{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
-
执行如下命令进行校验文件的正确性
pod lib lint
如果见到如上图,则代表验证通过;如果没有通过的话按照提示进行修改即可。
备注:这个命令是本地校验的,还要pod spec lint 这个是本地和远程校验。如果此时我们用pod spec lint会报错,因为我们还有发布一个版本,就是第6步还没做呢。
-
在github上创建一个发布版本
此时我们运行pod spec lint则会见到如下图
到这步整个制作过程已经完成接下来让我们发布自己spec
发布到CocoaPods
CocoaPods 0.33中加入了 Trunk 服务,使用 Trunk 服务可以方便的发布自己的Pod。要想使用 Trunk 服务,首先需要使用如下命令注册自己的电脑。这很简单,只要你指明你的邮箱地址(spec文件中的)和名称即可。CocoaPods 会给你填写的邮箱发送验证邮件,点击邮件中的链接就可通过验证。
- 注册
pod trunk register [email protected] "FlyOceanFish"
2、验证是否注册成功
pod trunk me
3、发布我们的podspec
pod trunk push
此图代表我们已经发布成功
不过此时我们pod search还搜不到我们的podspec。
可以通过一下两步之后再尝试搜索:
- 运行pod setup更新本地的spec,再搜索一下试试看。
- 删除~/Library/Caches/CocoaPods目录下的search_index.json文件
pod setup成功后会生成~/Library/Caches/CocoaPods/search_index.json文件。
终端输入rm ~/Library/Caches/CocoaPods/search_index.json
删除成功后再执行pod search
参考链接
CocoaPods
between 'pod spec lint' and 'pod lib lint'?