Cocoapods创建公有库
Cocoapods创建公有库
分两种情况来说:
- 创建一个全新的共享项目
- 共享已有的项目
创建一个全新的共享项目
参考Making a CocoaPod,推荐使用如下的命令:
pod lib create [pod name]
如下的例子,pod名称为WZViews
,如下:
pod lib create WZViews
输出命令后,会有一系列的提示,如下所示:
创建后,Xcode会自动打开项目,结构如下:
创建一个Github Repo
在Github上创建一个Repo,如下:
上面创建的文件结构中,已有README文件,已包含MIT Lisence了,所以在创建Repo时可以不选了
然后,就可以push已存在的Repo到Github上了,通过如下的命令:
git remote add origin https://github.com/winfredzen/WZViews.git
git push -u origin master
具体的过程如下图所示:
Podspec Metadata
目录如下,特意注意WZViews.podspec
WZViews.podspec
的内容如下:
#
# Be sure to run `pod lib lint WZViews.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 = 'WZViews'
s.version = '0.1.0'
s.summary = 'A short description of WZViews.'
# 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
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://github.com/winfredzen/WZViews'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'winfredzen' => 'aaa@qq.com' }
s.source = { :git => 'https://github.com/winfredzen/WZViews.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'WZViews/Classes/**/*'
# s.resource_bundles = {
# 'WZViews' => ['WZViews/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
s.frameworks = 'UIKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
编写完成后, 我们需要验证.podspec
文件的合法性, 这里需要终端cd到.podspec
文件所在文件夹, 执行:
pod lib lint WZViews.podspec
可能会有警告,可使用--allow-warnings
来忽略
pod lib lint WZViews.podspec --allow-warnings
后,会提示成功
添加源文件
添加源文件的位置,与WZViews.podspec
中s.source_files
指定的路径,保持一致
如下创建NoScrollCollectionView
类,注意对外公开的类,修饰符修改为public
import Foundation
public class NoScrollCollectionView: UICollectionView {
override public var contentSize: CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override public var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height)
}
}
打tag, 发release版本
先跟上面一样,git add
和 git commit
,git push
然后使用git tag 0.1.0 -a
,添加tag,并添加注释,注意此处的tag
要与WZViews.podspec
中的s.version
保持一致
再使用git push --tags
命令,push tag
Push to Cocoapods
在公开到Cocoapods,需要确保已注册,使用如下的命令注册:
$ pod trunk register orta@cocoapods.org 'Orta Therox' --description='macbook air'
会提示发邮件给你,点击确认
然后就可以使用pod trunk push
来发布了
然后就可以在Cocoapods中使用了:
pod 'WZViews', '~>0.1.0'