欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Cocoapods创建公有库

程序员文章站 2022-04-09 19:40:00
...

Cocoapods创建公有库

分两种情况来说:

  • 创建一个全新的共享项目
  • 共享已有的项目

创建一个全新的共享项目

参考Making a CocoaPod,推荐使用如下的命令:

pod lib create [pod name]

如下的例子,pod名称为WZViews,如下:

pod lib create WZViews

输出命令后,会有一系列的提示,如下所示:

Cocoapods创建公有库

创建后,Xcode会自动打开项目,结构如下:

Cocoapods创建公有库

Cocoapods创建公有库

创建一个Github Repo

在Github上创建一个Repo,如下:

Cocoapods创建公有库

上面创建的文件结构中,已有README文件,已包含MIT Lisence了,所以在创建Repo时可以不选了

然后,就可以push已存在的Repo到Github上了,通过如下的命令:

git remote add origin https://github.com/winfredzen/WZViews.git
git push -u origin master

具体的过程如下图所示:

Cocoapods创建公有库

Podspec Metadata目录如下,特意注意WZViews.podspec

Cocoapods创建公有库

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来忽略

Cocoapods创建公有库

pod lib lint WZViews.podspec --allow-warnings后,会提示成功

Cocoapods创建公有库

添加源文件

添加源文件的位置,与WZViews.podspecs.source_files指定的路径,保持一致

Cocoapods创建公有库

如下创建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 addgit commitgit push

Cocoapods创建公有库

然后使用git tag 0.1.0 -a,添加tag,并添加注释,注意此处的tag要与WZViews.podspec中的s.version保持一致

再使用git push --tags命令,push tag

Cocoapods创建公有库

Push to Cocoapods

在公开到Cocoapods,需要确保已注册,使用如下的命令注册:

$ pod trunk register orta@cocoapods.org 'Orta Therox' --description='macbook air'

会提示发邮件给你,点击确认

然后就可以使用pod trunk push来发布了

Cocoapods创建公有库

然后就可以在Cocoapods中使用了:

pod 'WZViews', '~>0.1.0'

参考

相关标签: cocoapods 公有库

上一篇: 图像风格迁移

下一篇: vlan实验