1.在GitHub上添加公有仓库:
GitHub是你的代码保存的实际仓库,也可以是其他公开仓库,这样不仅可以通过pod引入你的组件,还可以在GitHub直接download你的源码使用
登录github,点击个人头像,Your repositories,新建公有仓库:
image.png
2.在本地创建创建Pod项目工程文件:
通过pod lib create创建的工程比较完整(包含demo工程,podspec文件),如果是本地已有组件也可以在根目录直接创建podspec文件,关于podspec文件后面会介绍
已有组件,并且通过git管理,可以只创建podspec文件:
pod spec create [NAME]
如果从0开始创建,选一个合适地方,创建仓库目录:例如:Documents 创建pod目录
cd Documents/pod
创建本地仓库publicTool
pod lib create publicTool
按照要求选择,平台,语言,demo等
Cloning `https://github.com/CocoaPods/pod-template.git` into `publicTool`.
Configuring publicTool template.
------------------------------
To get you started we need to ask a few questions, this should only take a minute.
If this is your first time we recommend running through with the guide:
- https://guides.cocoapods.org/making/using-pod-lib-create.html
( hold cmd and click links to open in a browser. )
What platform do you want to use?? [ iOS / macOS ]
> iOS
What language do you want to use?? [ Swift / ObjC ]
> objc
Would you like to include a demo application with your library? [ Yes / No ]
> yes
Which testing frameworks will you use? [ Specta / Kiwi / None ]
> specta
Would you like to do view based testing? [ Yes / No ]
> yes
What is your class prefix?
> pb
创建成功后会在本地出现仓库(publicTool)目录,并且Xcode会自动打目录下的Example工程:
已包含了podspec后面无需在创建
image.png
我们可以将自己写好的组件代码拷贝到Classes下面,替换生成的ReplaceMe.m
image.png生成的Example包含Podfile文件
use_frameworks!
platform :ios, '9.0'
target 'publicTool_Example' do
pod 'publicTool', :path => '../'
target 'publicTool_Tests' do
inherit! :search_paths
pod 'Specta'
pod 'Expecta'
pod 'FBSnapshotTestCase'
pod 'Expecta+Snapshots'
end
end
pod 'publicTool', :path => '../' 会去上层目录下找publicTool目录
3.测试本地库:
进入到Example文件下,执行pod install
pod install
完成之后,会在Example下多处熟悉的Pods.xcodeproj project
publicTool下会出现刚才拷贝进去的文件,然后我们引入到Example测试没问题就下一步
4.修改.podspec
通过pod lib create创建的本地库,默认创建了.podspec
podspec是该组件的描述文件,pod 搜索就是根据.podspec文件信息查找的
在pod/publicTool目录下.podspec 修改它如下:
#
# Be sure to run `pod lib lint publicTool.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'publicTool' # 组件名
s.version = '0.1.0' # 版本 tag
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 = "这是个测试" # 描述,尽可能长一些,不然会警告
s.homepage = 'https://github.com/gebulin-boot/public-specs' #主页,可以填你的github主页地址
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'uname@email.com' }
s.source = { :git => 'https://github.com/gebulin-boot/public-specs.git', :tag => s.version.to_s } #你的远程库 后面的版本好从s.version读取的
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '9.0'
s.source_files = 'publicTool/Classes/**/*'
# s.resource_bundles = {
# 'publicTool' => ['publicTool/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3' #依赖的第三方库 有多个就多行写
end
以上修改没问题,下一步
5.提交代码到远程仓库(GitHub仓库),和平常提交代码的操作一样:
进入仓库目录下:
cd publicTool
查看提交状态
git status
提交到本地:
git add .
git commit -m '提交信息'
关联到你的远程仓库:
git remote add origin https://github.com/gebulin-boot/public-specs.git
推送到远程仓库(GitHub):
git push origin master
如果你平时是通过ssh key推送代码到GitHub,可能会报如下错误:
remote: Permission to gebulin-boot/public-specs.git denied to LUV-LL.fatal: unable to access 'https://github.com/gebulin-boot/public-specs.git/': The requested URL returned error: 403
到publicTool/.git/config
中url改成你的仓库ssh地址即可
以上没问题,下一步
6.打上标签(版本号)
组件问题了,接下来要给组件打上tag,这里的tag要和podspec中s.version一致
查看:
git tag
添加:
git tag '0.1.0'
推送到你的远程仓库:
git push --tags
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0To github.com:gebulin-boot/public-specs.git * [new tag] 0.1.0 -> 0.1.0
7.检查podspec,推送podspec到pod库:
进入仓库目录
cd publicTool
检查.podspec是否有问题
pod spec lint
可能会报这样的错:
-> publicTool (0.1.0) - WARN | description: The description is shorter than the summary. - ERROR | [iOS] unknown: Encountered an unknown error (/usr/bin/xcrun simctl list -j devicesxcrun: error: unable to find utility "simctl", not a developer tool or in PATH) during validation.Analyzed 1 podspec.[!] The spec did not pass validation, due to 1 error and 1 warning.
XCode未设置Command line tools,在XCode
-> Preferences...
-> Locations
中设置Command line tools。
最后出现以下提示代表检查通过:
publicTool.podspec passed validation.
推送到pod CDN库:
CocoaPods 自 1.8 版本开始默认使用 trunk CDN (cdn.cocoapods.org/) 作为 spec 的源,这样避免了把几个G的specs库克隆到本地
trunk目录位于.cocopods/repos目录下
pod trunk push publicTool.podspec [PATH] PATH缺省 默认是trunk目录
pod trunk push publicTool.podspec
第一次可能会报错:
[!] You need to run `pod trunk register` to register a session first.
按照要求注册cocopods账号即可:
pod trunk register your email@163.com 'uname' --description='cocopods description'
然后邮箱会收到:一份cocopods官方发的邮件,打开邮箱链接后不用对邮件做什么,此时可以再次执行推送命令:
以上没问题,可以通过pod search 搜索到你的库
网友评论