前言
接上一篇文章:iOS组件化之路总结一(CocoaPods安装三方库原理)
继续iOS组件化之路,记录一下如何发布到github,并提供cocoapods支持
一.创建项目
1. 在github官网https://github.com新建代码仓库如图:
- 项目名称需要验证是否在cocoapods已存在,使用
pod search 项目名称
- 许可协议一定要选
- 添加gitignore
2.添加文件
- 在本地创建文件夹,打开终端运行
git clone git仓库地址
仓库地址如下
-
新建Classes文件夹拖入要开源的代码:
开源代码一定要能独立编译成功、无其它文件依赖
二、编写spec文件
1.创建podspec文件
- 打开终端cd进入项目文件夹,注意目录层级
- 执行
pod spec create 项目名称
生成项目名称.podspec
文件
- 编辑podspec文件,文件内容如下(有些内容可以省略),语法规则可参考podspec用法
每行代码的含义
s.name:名称,pod search 搜索的关键词,注意这里一定要和.podspec的名称一样,否则报错
s.version:版本号、必须最新push的tag值
s.ios.deployment_target:支持的pod最低版本
s.summary: 简介
s.homepage:项目主页地址
s.license:许可证
s.author:作者
s.social_media_url:社交网址,这里我写的微博默认是Twitter,如果你写Twitter的话,你的podspec发布成功后会@你
s.source:项目的地址
s.source_files:需要包含的源文件
s.resources: 资源文件
s.requires_arc: 是否支持ARC
s.dependency:依赖库,不能依赖未发布的库
s.dependency:依赖库,如有多个可以这样写
# Be sure to run `pod spec lint FDPodTest.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 = "FDPodTest"
s.version = "0.0.1"
s.summary = "FDPodTest 测试pod管理框架"
# 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
FDPodTest 测试pod管理框架这里是长标题,可以忽略
DESC
s.homepage = "https://github.com/WangLongONE/FDPodTest"
# 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 => "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 = { "wanglong" => "[email protected]" }
# Or just: s.author = "wanglong"
# s.authors = { "wanglong" => "" }
# s.social_media_url = "http://twitter.com/wanglong"
# ――― 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, "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/WangLongONE/FDPodTest.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
三.上传文件
- 编辑podspec完成后上传代码到仓库
- 提交代码到远程仓库
git commit -m "提交信息"
git push
- 添加并发布tag
git tag -a '0.0.1' -m '标签说明'
git push --tags
标签推送成功之后就可以验证posspec文件了
- 验证posspec是否有问题
pod lib lint
验证spec文件、验证成功如下
- 注册Trunk
#[email protected] :必须真实邮箱(后边要用)
#xxxname :名字
#--description :描述(可以不写)
#--verbose :打印信息
注册成功如下
注册完后根据提示打开注册邮箱[email protected]会发现一份邮件如图
复制邮件地址,打开链接如下图
- 上传spec文件到cocapods远程specs索引库:
需要稍等一下
上传成功如下
-
至此你接可以使用cocoapod管理使用自己到代码了
搜索项目名称如下:
遇到的几个问题
1.license: Unable to find a license file
原因:在创建仓库的时候缺少协议,
解决办法:参考https://www.cnblogs.com/zhanglinfeng/p/7772404.html
创建完成后,先git pull
拉代码,然后git tag -d 0.0.1
删除本地tag,git push origin :0.0.1
删除远端标签,重新打标签即可;
2./Users/mac/Desktop/pod测试/PodTestLib/Classes/Category/xxxx.m:15:46: error: use of undeclared identifier 'Global'
原因:原来加的文件中未引用Global相关文件
解决办法:删处添加依赖文件或者替换代码
3. You ([email protected]) are not allowed to push new versions for this pod. The owners of this pod are [email protected].
原因:您创建的代码项目名称在cocoapod已经有人创建过了
解决办法:更改项目名称
4.上传成功但pod search 检索不到自己的项目
原因:本地的索引文件没有更新
解决办法:删除本机search_index.json索引文件,后重新pod search ,索引文件地址参考上一片文章
至此配合前一篇文章相信你对CocoaPods的工作流程及原理应该有了更深入的了解了!