博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios开发之上传自己组件到Cocoapod
阅读量:4146 次
发布时间:2019-05-25

本文共 8876 字,大约阅读时间需要 29 分钟。

作为一名ios开发者,我们平时都会使用AFNetworking、SDWebImg,但上传过自己的组件到cocoapod吗?收藏本文章,博主带你上传过自己的组件到cocoapod。

一、创建一个测试工程,我们随便命名为LaoGaoTestDemo

创建测试工程

二、给工程引入cocoapod

1.使用终端cd到工程目录下

创建Podfile文件:

pod init

2.打开Podfile文件:

open Podfile

3.我们添加一个SDWebImg

pod 'SDWebImage'

 三、编写组件代码

我们创建一个DGQCenterView,如下图:

编写DGQCycleView组件内容:

#import "DGQCenterView.h"#import "UIImageView+WebCache.h"@implementation DGQCenterView- (instancetype)initWithFrame:(CGRect)frame {    self = [super initWithFrame:frame];    if (self) {        _centerImgView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width/4, frame.size.height/4, frame.size.width/2, frame.size.height/2)];        [self addSubview:_centerImgView];    }    return self;}- (instancetype)initWithFrame:(CGRect)frame imageUrl:(NSString *)imageUrl {    self = [super initWithFrame:frame];    if (self) {        _centerImgView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width/2, frame.size.height/2, frame.size.width/2, frame.size.height/2)];        _imageUrl = imageUrl;        if (imageUrl.length >0) {            [_centerImgView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];        }        [self addSubview:_centerImgView];    }    return self;}- (void)setImageUrl:(NSString *)imageUrl {    _imageUrl = imageUrl;    if (imageUrl.length >0) {        [_centerImgView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];    }}@end

测试DGQCenterView组件:

经过测试没问题,接下来准备上传github

四、上传github

4.1.创建.podspec

cd到你项目的目录,执行命令:

// 注 DGQCycleView 这个是你框架的名称$ pod spec create DGQCycleView

4.2 编写:podspec文件内容

#

#  Be sure to run `pod spec lint DGQCycleView.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 https://guides.cocoapods.org/syntax/podspec.html
#  To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#

Pod::Spec.new do |spec|

  # ―――  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.
  #

  spec.name         = "DGQCycleView"

  spec.version      = "0.1.2"
  spec.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!
  spec.description  = "简单易用的图片无限轮播器..."

  spec.homepage     = "https://github.com/denggaoqiang/CustomView"

  # spec.screenshots  = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"

  # ―――  Spec License  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Licensing your code is important. See https://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'.
  #

  spec.license      = "MIT"

  # spec.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.
  #

  spec.author             = { "xxxxxxqiang" => "xxxxxxxx@qq.com" }

  # Or just: spec.author    = "xxxxxxqiang"
  # spec.authors            = { "xxxxxxqiang" => "xxxxxxxx@qq.com" }
  # spec.social_media_url   = "https://twitter.com/xxxxxxqiang"

  # ――― 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.
  #

  spec.platform     = :ios

  spec.platform     = :ios, "9.0"

  #  When using multiple platforms

  # spec.ios.deployment_target = "5.0"
  # spec.osx.deployment_target = "10.7"
  # spec.watchos.deployment_target = "2.0"
  # spec.tvos.deployment_target = "9.0"

  # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the location from where the source should be retrieved.
  #  Supports git, hg, bzr, svn and HTTP.
  #

  spec.source       = { :git => "https://github.com/denggaoqiang/CustomView.git", :tag => "#{spec.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.
  #

  spec.source_files  = "Class/*.{h,m}"

  # spec.exclude_files = "Class/Exclude"

  # spec.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.
  #

  # spec.resource  = "icon.png"

  # spec.resources = "Resources/*.png"

  # spec.preserve_paths = "FilesToSave", "MoreFilesToSave"

  # ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Link your library with frameworks, or libraries. Libraries do not include
  #  the lib prefix of their name.
  #

  # spec.framework  = "SomeFramework"

  # spec.frameworks = "SomeFramework", "AnotherFramework"

  # spec.library   = "iconv"

  # spec.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.

  # spec.requires_arc = true

  # spec.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }

  # spec.dependency "JSONKit", "~> 1.4"
  spec.dependency 'SDWebImage', '>= 5.0.0'

end

 

 

接下来讲解一下每行代码的含义

s.name:名称,pod search 搜索的关键词,注意这里一定要和.podspec的名称一样,否则报错
s.version:版本号
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:依赖库,如有多个可以这样写

 4.3.创建LICENSE(许可证/授权)文件,此文件必须要有

创建一个文件名字命名为LICENSE,内容为:只需要把前面的版权改一下就行了,后面的都一样

MIT License

Copyright (c) 2021 denggaoqiang

Permission is hereby granted, free of charge, to any person obtaining a copy

of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all

copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

 4.4验证.podspec文件

到此检查一下你工程下面的文件, 你的项目, .podspec文件, LICENSE文件

验证会先测试本地 .podspec 文件是否存在语法错误.

// --verbose 如果验证失败会报错误信息pod spec lint DGQCycleView.podspec --verbose

 

 

-> DGQCycleView

 

DGQCycleView.podspec passed validation.

如果有报错信息说明,报错内容很详细,查看自己的 podspec文件修改即可。

4.5上传代码到git

将测试工程及包含配置好的 .podspec, LICENSE 的项目提交 Git,

4.6打tag

因为cocoapods是依赖tag版本的,所以必须打tag,

以后再次更新只需要把你的项目打一个tag
然后修改.podspec文件中的版本接着提交到cocoapods官方就可以了,提交命令请看下面

//为git打tag, 第一次需要在前面加一个vgit tag "v1.0.2" //将tag推送到远程仓库git push --tags

4.7.注册Trunk 

trunk需要CocoaPods 0.33版本以上,用pod --version

已经注册过的不需要注册,怎么看自己有没有注册

pod trunk me

注册:

// 加上--verbose可以输出详细debug信息,方便出错时查看。pod trunk register xxxxxxxx@163.com "DGQCycleView" --verbose

 注册完成之后会给你的邮箱发个邮件,进入邮箱邮件里面有个链接,需要点击确认一下

4.8发布自己的组件

发布时会验证 Pod 的有效性,如果你在手动验证 Pod 时使用了 --use-libraries 或 --allow-warnings 等修饰符,那么发布的时候也应该使用相同的字段修饰,否则出现相同的报错。

// --use-libraries --allow-warningspod trunk push DGQCycleView.podspec

4.9 pod验证

pod search DGQCycleView

会出现如下信息:

到此大功告成,小伙伴们赶快让你的框架支持pod吧

如果遇到问题,可以添加博主qq号,453923710

转载地址:http://dwcti.baihongyu.com/

你可能感兴趣的文章
RQP-DEF-0177
查看>>
MySQL字段类型的选择与MySQL的查询效率
查看>>
Java的Properties配置文件用法【续】
查看>>
JAVA操作properties文件的代码实例
查看>>
IPS开发手记【一】
查看>>
Java通用字符处理类
查看>>
文件上传时生成“日期+随机数”式文件名前缀的Java代码
查看>>
Java代码检查工具Checkstyle常见输出结果
查看>>
北京十大情人分手圣地
查看>>
Android自动关机代码
查看>>
Android中启动其他Activity并返回结果
查看>>
2009年33所高校被暂停或被限制招生
查看>>
GlassFish 部署及应用入门
查看>>
X-code7 beta error: warning: Is a directory
查看>>
Error: An App ID with identifier "*****" is not avaliable. Please enter a different string.
查看>>
3.5 YOLO9000: Better,Faster,Stronger(YOLO9000:更好,更快,更强)
查看>>
iOS菜鸟学习--如何避免两个按钮同时响应
查看>>
iOS菜鸟学习—— NSSortDescriptor的使用
查看>>
C语言8
查看>>
Qt实现简单延时
查看>>