写在前面的话
iOS 中常用到第三方开源框架,包括各种SDK, 一般引入三方开源框架有两种方式,一种是下载三方框架源代码,直接拖进当前项目,有的还需要添加对应的系统框架
这样会很麻烦,还有各种配置,(以前的我就这么干,实在不堪回首,无辜脸)后来使用CocoaPod 发现特别方便,后来项目一直用的这个管理三方,更新啥的特别方便。*如果对CocoaPod不是很了解请移步到这个地方:用CocoaPods做iOS程序的依赖管理
闲来无事,想把自己写的好的公用模块发布到CocoaPods上(分享是最好的学习,特别是记录下整个过程)所有我就写了这篇文章,留己备用。
下面开始上干货了啊 ^ v ^ :
创建本地CocoaPods:
要开源你自己的框架到CocoaPods第一步要干啥呢?我就不说了直接开干:
-
创建一个项目:把自己的开源框架拖到项目中去(这里为了测试随便写了一个Demo ,拖了2个系统FramWork进去, 项目名称为 CocoaPodTest),整个目录结构如下:
- (补充,为了教程更加完善加了2个三方开源文件如下所示)
1 target :'CocoaPodTest' do
2 use_frameworks!
3 platform :ios,'8.0'
4 pod 'MJRefresh', '~> 3.1.0'
5 end
- 然后把这个项目发布到github上,注意:上传的时候要打上Tag这个后面要用到
- 在项目根目录下 后在工程根目录中初始化一个Podspec文件,运行终端:执行以下命令:
pod spec create CocoaPodTest
创建成功后会在这里看到输出:
Specification created at CocoaPodTest.podspec
在终端打开这个Specification 文件 ,里面已经有非常丰富的说明文档。下面介绍如何声明第三方库的代码目录和资源目录,还有该第三方库所依赖ios核心框架和第三方库。
我的打开是这样的
#
# Be sure to run `pod spec lint CocoaPodTest.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 = "CocoaPodTest"
s.version = "0.0.1"
s.summary = "A short description of CocoaPodTest."
# 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/CocoaPodTest"
# 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 = { "用户名" => "XXXXX@qq.com" }
# ――― 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/CocoaPodTest.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
各种地方要需要改的地方描述的都很清楚了
字需要改几个地方就行了
其中几个注意的点就是
s.source = { :git => "http://EXAMPLE/CocoaPodTest.git", :tag => "#{s.version}" }
这个是CocoaPod 查找的资源地址,和对一个的tag 很重要
我对应这个项目改后是这样的:
s.source = { :git => "https://github.com/WCMYCML/CocoaPods.git", :tag => “1.0” }
需要依赖的三方开源框架:如果有多个需要填写多个s.dependency。
s.dependency "MJRefresh", "~> 3.1.0”
依赖的FrameWork
s.frameworks = "MobileCoreServices", "AVKit"
其中最重要的几个是这样
s.source_files = "Classes", "Classes/**/*.{h,m}"
s.exclude_files = "Classes/Exclude"
s.public_header_files = "Classes/**/*.h"
s.source_files声明了我们库的源代码的位置,这是从项目更根目录开始进行下去的产品所以这个地方不能填错了。忽略文件和公布的头文件可以选择性的填写。这个指定的目录下的文件都会进行编译。如果该目录下还有一些资源文件(如图片等
,这些文件并不需要进行编译。可以使用s.resourcs声明。*.{h,m}是一个类似正则表达式的字符串,表示匹配所有以.h和.m为扩展名的文件。
我改后是这样的
s.source_files = "CocoaPodTest", "CocoaPodTest/NamePodFile/*.{h,m}"
编辑完podspec文件后,需要验证一下这个文件是否可用,如果有任何WARNING或者ERROR都是不可以的,它就不能被添加到Spec Repo中,不过xcode的WARNING是可以存在的,验证需要执行命令:
在根目录下运行如下命令:
pod spec lint CocoaPodTest.podspec
我的当时出现很多错误(坑要多踩踩才好啊)
修改后还是有个错误如下:
# from CocoaPodTest.podspec:135
# -------------------------------------------
# s.dependency "MJRefresh", "~> 3.1.0”
> s.dependency "MBPhotoPicker”,
#
# -------------------------------------------
找了半天原来是标点符号有双引号有问题。
说明验证通过了。在检测你的podspec时候,如果直接用pod spec lint CocoaPodTest.podspec的话,出现错误它只会直接一句红色的话The spec did not pass validation, due to 1 error.告诉你的有多少个error和warning,而不会具体的指出你的错误出在哪里,这时候你可以在这句指令后面加上参数 --verbose 这样就会告诉你具体的错误信息。
这样根据它提示你的错误信息去解决就可以了。
当所有错误都解决掉后可能会遇到提示有警告不能提交,
[!] The spec did not pass validation, due to 42 warnings (but you can use `--allow-warnings` to ignore them).
这个时候在运行下面的命令:
pod spec lint CocoaPodTest.podspec --allow-warnings
当看到如下命令:
ocoaPodTest.podspec passed validation.
说明已经提交成功了啊。
- 编辑好podspec文件后就可以将该podspec文件保存到本机的~/.cocoapods/repos/master/Specs目录中仅供自己使用,也可以将其提交到CocoaPods/Specs代码库中。下面我们先将其保存到本机中,然后通过pod 像正常安装一样。
CocoaPods Trunk发布自己的Pods
要想把自己的项目提交到Pods上 可以通过 trunk 提交
步骤如下:
- 注册Trunk (如果你已经注册过可以忽略掉)我已经注册过了就忽略了
$ pod trunk register 邮箱 '用户名' --description='描述'
在注册时需要替换成自己的邮箱和用户名,一切顺利的话就会受到一份邮件,点击邮件中的链接后验证一下:
pod trunk me
我运行后的效果如下图:
当然,如果你的pod是由多人维护的,你也可以添加其他维护者:
运行如下代码:
$ pod trunk add-owner 账户邮箱
工作完成之后,就可以开始 trunk push了。
Trunk push
pod trunk push
命令会首先验证你本地的podspec
文件(是否有错误),之后会上传spec
文件到trunk
,最后会将你上传的podspec
文件转换为需要的json文件。在工程根目录(包含有.podspec)下执行命令
pod trunk push
出现这个就是正在上传要等一段时间,视文件大小而定:
[!] Found podspec `CocoaPodTest.podspec`
Updating spec repo `master`
如果在 trunk push
过程中报错了,仔细查看一下错误信息。
我的出现提示
[!] {"name"=>["is already taken"]}
找了半天发现是我当前的Space 文件的 s.author = { “用户名” => "邮箱@qq.com" }
和 trunk 中的用户名 邮箱不匹配
修改Space文件后。这个错误解决了
后面有提示了要给错误
[!] Oh no, an error occurred.
Search for existing GitHub issues similar to yours:
https://github.com/CocoaPods/CocoaPods/search?q=different+prefix%3A+%22%22+and+%22%2FUsers%2Fwanghaolin%2FDesktop%2FGIT%2FCocoaPods%2FCocoaPodTest%22&type=Issues
If none exists, create a ticket, with the template displayed above, on:
https://github.com/CocoaPods/CocoaPods/issues/new
Be sure to first read the contributing guide for details on how to properly submit a ticket:
https://github.com/CocoaPods/CocoaPods/blob/master/CONTRIBUTING.md
Don't forget to anonymize any private data!
查了半天,原来地址和gitHub上的一个开源框架CocoaPods冲突了(第一次遇到这种坑,)改了(这里就忽略掉这个步骤了有点多,能懂就行)这个就提交成功了,这是你可以在本地的~/.cocoapods/repos/master/Specs目录下看到转换之后的json文件
至此整个制作自己的开源库的过程就完成了,以后有新版本只需要修改工程根目录下的podspec文件更改版本号,然后重新执行pod trunk push命令
- 到现在整个创建和发布就算完成了,其实也不难,只是遇到很多坑,写下来希望后来的人不重蹈覆辙。也方便自己以后使用,利人利己的事何乐而不为呢。
结束语
第一次写这么长的技术文章,花费了2个多小时,排版啥的估计不堪入目,各位看客不要扔砖就行,有啥不明白或者有坑的地方可以提问,看到会第一时间解答。 最后附上我配置好的podspec
文件
#
# Be sure to run `pod spec lint CocoaPodTest.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 = "CocoaPodTest"
s.version = "0.0.1"
s.summary = "写的测试用例!"
# 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
测试上传啊 (一定要写在这2个代码中间)
DESC
s.homepage = "https://github.com/WCMYCML/CocoaPods"
# 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"
# 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 = { “WCMYCML” => "xxxxxx@qq.com" }
# ――― 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, 8.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 => "https://github.com/WCMYCML/CocoaPods.git", :tag => 1.0 }
# ――― 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 = "CocoaPodTest", "CocoaPodTest/CocoaPodTest/NamePodFile/*.{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.frameworks = "MobileCoreServices", "AVKit"
# ――― 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.dependency "MJRefresh", "~> 3.1.0"
end
第一次写,将就看看就行,打赏就不用了,动动小手指点下赞就行,你的小小赞是我继续写下去动力!!!