美文网首页
制作、发布自己的第三方代码库

制作、发布自己的第三方代码库

作者: ychen3022 | 来源:发表于2018-05-02 14:05 被阅读114次

很多时候,自己写了一个小工具,希望在自己的每个app上都能够灵活的使用,这个时候选择用CocoaPod来管理要方便的多,所以我们来学习一下如何制作自己的第三方代码库。

制作本地库

在自己本地新建一个文件夹Demo,然后再控制台中cd到该目录下,输入指令创建库,ComAlertDialogTool是我们要创建的库的名字

pod lib create ComAlertDialogTool

输入命令后会显示下载模板,会有几秒钟等待,这里有几项需要我们输入信息

chenyuandeMacBook-Pro:~ chenyuan$ pod lib create ComAlertDialogTool
Cloning `https://github.com/CocoaPods/pod-template.git` into `ComAlertDialogTool`.
Configuring ComAlertDialogTool 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 double 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 ]     //你要使用哪个测试框架?(没有就写None)
 > None

Would you like to do view based testing? [ Yes / No ]     //是否要UI测试?
 > Yes

What is your class prefix?     //类名前缀是什么?
 > Demo

Running pod install on your new library.

Analyzing dependencies
Fetching podspec for `ComAlertDialogTool` from `../`
Downloading dependencies
Installing ComAlertDialogTool (0.1.0)
Installing FBSnapshotTestCase (2.1.4)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `ComAlertDialogTool.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.

[!] Automatically assigning platform `ios` with version `9.3` on target `ComAlertDialogTool_Example` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'ComAlertDialogTool/Example/ComAlertDialogTool.xcworkspace'

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.
chenyuandeMacBook-Pro:~ chenyuan$ 

然后我们就创建好了一个项目,看他的文件结构如下:
我们可以在ReplaceMe.m位置添加自己的代码。
注意:听说这个里面最多只能有一层文件夹。


屏幕快照 2018-04-25 下午5.49.28.png
3DC86090-1BA5-450A-A71F-60CBDF02A434.png

添加完代码后,记得查看Podfile文件,并执行pod install
到这里本地库我们已经制作好了,如果把我们本地当做git库的话,这时候我们只需要在你其他的项目或者工程的Podfile文件里添加ComAlertDialogTool:

use_frameworks!

target 'ComAlertDialogTool_Example' do
  pod 'ComAlertDialogTool', :path => '../'             #路径根据实际情况而定
  
end
本地库提交到CocoaPods远程仓库

我们使用CocoaPods的指令创建好本地库之后,默认生成的podspec文件,它描述该库某一个版本的信息。

#
# Be sure to run `pod lib lint ComAlertDialogTool.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             = 'ComAlertDialogTool'
  s.version          = '0.0.1'       //版本,每次发布pod库都要修改
  s.summary          = 'A short description of ComAlertDialogTool.'

# 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/ychen3022/ComAlertDialogTool'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'ychen3022' => '3023056944@qq.com' }
  s.source           = { :git => 'https://github.com/ychen3022/ComAlertDialogTool.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

  s.source_files = 'ComAlertDialogTool/Classes/**/*'
  
  # s.resource_bundles = {
  #   'ComAlertDialogTool' => ['ComAlertDialogTool/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
    s.dependency 'ComTools', '~> 0.0.3'            //依赖的其他pod库

end

在确定自己的.podspec没有问题之后,我们需要将代码上传到git上面
首先在自己的github仓库上创建一个工程


屏幕快照 2018-05-02 下午1.40.20.png

然后执行下面的git命令将代码上传到github上面

git init
git remote add origin https://github.com/ychen3022/ComAlertDialogTool
git add .
git commit -m "ComAlertDialogTool-firstCommit"
git push origin master
git tag 0.0.1
git push origin 0.0.1

成功将代码传到git上后,我们再提交到官方的CocoaPods
首先先用cd到Spec文件所在的目录,然后执行pod lib lint / pod lib lint --allow-warnings
当出现ComAlertDialogTool passed validation.的字样,说明通过验证。

chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ ls       //需要在.podspec文件所在目录下面
ComAlertDialogTool      Example             README.md
ComAlertDialogTool.podspec  LICENSE             _Pods.xcodeproj
chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ pod lib lint

 -> ComAlertDialogTool (0.0.1)
    - WARN  | summary: The summary is not meaningful.
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:205:10: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:243:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:276:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:277:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:142:13: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:306:20: warning: incompatible pointer types returning 'UIView *' from a function with result type 'ComLoadingView *' [-Wincompatible-pointer-types]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:317:20: warning: incompatible pointer types returning 'UIView *' from a function with result type 'ComLoadingView *' [-Wincompatible-pointer-types]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:116:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:147:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:148:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]

[!] ComAlertDialogTool did not pass validation, due to 11 warnings (but you can use `--allow-warnings` to ignore them).
You can use the `--no-clean` option to inspect any issue.    
//没有通过验证,发现是有很多警告,我们可以去修改警告,或者使用pod lib lint --allow-warnings
chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ pod lib lint --allow-warnings

 -> ComAlertDialogTool (0.0.1)
    - WARN  | summary: The summary is not meaningful.
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:205:10: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:243:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:276:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:277:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:142:13: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:306:20: warning: incompatible pointer types returning 'UIView *' from a function with result type 'ComLoadingView *' [-Wincompatible-pointer-types]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:317:20: warning: incompatible pointer types returning 'UIView *' from a function with result type 'ComLoadingView *' [-Wincompatible-pointer-types]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:116:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:147:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:148:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]

ComAlertDialogTool passed validation.
chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ 

接下来我们把Spec提交到官方的CocoaPods的Specs Repo
提交之前,你需要注册pod账号。实质上是将"邮箱--名称--电脑"绑定在一起,所以这里不需要密码。
按照执行之后的提示,需要我们去登录邮箱验证邮件。


chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ pod trunk register 3023056944@qq.com ychen3022
[!] Please verify the session by clicking the link in the verification email that has been sent to 3023056944@qq.com
chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ 

然后,尝试提交pod库,执行pod trunk push ComAlertDialogTool.podspec --allow-warnings
出现下面的命令则说明提交到官方CocoaPods已经成功,
可是用pod search搜索可能不会搜索到,原因可能有延迟,需要等待一个小时左右,才会搜到。

chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ pod trunk push ComAlertDialogTool.podspec --allow-warnings
Updating spec repo `master`
Validating podspec
 -> ComAlertDialogTool (0.0.1)
    - WARN  | summary: The summary is not meaningful.
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:205:10: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:243:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:276:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:277:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:142:13: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:306:20: warning: incompatible pointer types returning 'UIView *' from a function with result type 'ComLoadingView *' [-Wincompatible-pointer-types]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:317:20: warning: incompatible pointer types returning 'UIView *' from a function with result type 'ComLoadingView *' [-Wincompatible-pointer-types]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:116:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:147:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:148:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]

Updating spec repo `master`

--------------------------------------------------------------------------------
 🎉  Congrats

 🚀  ComAlertDialogTool (0.0.1) successfully published
 📅  May 1st, 23:58
 🌎  https://cocoapods.org/pods/ComAlertDialogTool
 👍  Tell your friends!
--------------------------------------------------------------------------------
chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ 

注:本文参考Miaoz0070的CocoaPods制作第三方代码库,发布到官方Pod和自己的私有库(模块化、组件化)
链接:https://www.jianshu.com/p/f218fe3baff8

CocoaPods的中高级用法:https://www.jianshu.com/p/d6a592d6fced

相关文章

  • 制作、发布自己的第三方代码库

    很多时候,自己写了一个小工具,希望在自己的每个app上都能够灵活的使用,这个时候选择用CocoaPod来管理要方便...

  • 项目问题记录

    一、Git问题我们自己制作的第三方库如果没有发布到Cocoapods上,而我们又要在Podfile中引用,那么我们...

  • 制作一个带版本的CocoaPods开源三方库并托管到GitHub

    日常开发的时候,我们会用CocoaPods集成一些第三方库在项目中使用。但我们也希望能够将自己的代码制作成Pod库...

  • Include of non-modular header in

    当制作自己的pod时,我的代码依赖FMDB第三方库, 编译或者运行demo的时候就会报这个错误。 解决办法一:(这...

  • iOS_发布代码到CocoaPods(Trunk方式)

    制作pod库 发布代码到Cocopods ✔️ 遇到的bug 一、介绍 关于CocoaPods的介绍不在本文的主题...

  • Flutter 库制作

    制作第三方插件库 两种方式 纯dart 开发,不包含原生代码 2.含原生代码 参考 Flutter 插件开发:以微...

  • Cocopods 提示target has transitive

    制作自己的pod库时,依赖其他第三方库,在pod install时提示: 解决方案:在自己的pod库.podspe...

  • 制作CocoaPods

    iOS管理第三方库大多使用CocoaPods,如果需要实现组件化或者发布自己的开源库/私有库到CocoaPods,...

  • iOS动态库、静态库及使用场景、方式

    前面介绍过制作过程,这里不讲如何制作动态库、静态库。 静态库和动态库都是以二进制提供代码复用的代码库。 静态库常见...

  • pod 组件

    含第三方库的组件 本地检测代码仓库是否有问题 远程检测代码仓库是否有问题 向远程代码索引库提交spec 不含第三方...

网友评论

      本文标题:制作、发布自己的第三方代码库

      本文链接:https://www.haomeiwen.com/subject/vomylftx.html