创建一个自己的pods库

167 阅读3分钟

随着组件化方案的推进,或是想要建立一个属于自己的工具库,我们一步一步来创建一个属于自己的pods库。这里以我自己项目中的一个模块为例

创建paods工程项目

pod lib create FMPaymentManager

执行命令会提示设置

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

Would you like to do view based testing? [ Yes / No ]
 > Yes

What is your class prefix?
 > FM

设置完相关的问题之后就会创建出一个pods工程

在创建的过程中可能会出现错误 [!] Error installing FBSnapshotTestCase 此时我们cd到Example文件下,执行pod install就可以了,完整的项目工程如下:

image.png

image.png

Example就是我们开发过程中需要用到的测试工程,FMPaymentManager中的Classes就是我们要放置代码的地方,其中自动生成的ReplaceMe就是提示你用自己的代码将其替换的文件,删掉即可。

Assets文件放资源文件

接下来我们来看一下podspec文件中的额内容

Pod::Spec.new do |s|
  s.name             = 'FMPaymentManager'   组件工程名称
  s.version          = '0.1.0'              组件版本号
  s.summary          = 'A short description of FMPaymentManager.'       组件简短描述

# 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/高荣华/FMPaymentManager'    远程组件仓库地址
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'                                     
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'grh' => 'gaorh@ifeng.com' }
  s.source           = { :git => 'https://github.com/高荣华/FMPaymentManager.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '9.0'

  s.source_files = 'FMPaymentManager/Classes/**/*'  默认Classes文件夹下的所有文件夹和文件
  
  # s.resource_bundles = {
  #   'FMPaymentManager' => ['FMPaymentManager/Assets/*.png']
  # }   默认Assets文件下的png图片

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'  组件依赖的其他库,在执行pod install的时候如果项目中没有集成依赖库,会同时pod依赖库到当前工程

当前默认设置中的git地址是不可用的,需要申请自己的git仓库进行替换。

创建组建代码

创建FMPaymentManager并删除ReplaceMe文件,注意此处应该创建完成拖到文件夹中,不要再Xcode的Pods/FMPaymentManager中直接创建,否则执行pod install执行的还是将ReplaceMe重新拉回来

#import "FMPaymentManager.h"

@implementation FMPaymentManager

+ (void)payOrderSuccess:(void(^)(NSInteger))success {
    if (success) {
        success(0);
    }
}

@end

设置好之后执行pod install后,在Pods中出现创建的类

image.png

调用组件代码

AppDelegate中引入#import "FMPaymentManager.h"

image.png

image.png

至此pods库的本地使用就完成了,但我们的目的不单单是本地使用,而是给团队的其他人也同样能使用,那我们就要将做好的组件上传到远程仓库以供使用

发布到远程仓库

创建好自己的git仓库,添加远程仓库

git remote add origin http://xxxxxx/xxxxxx/FMPaymentManager.git

push本地代码

git push -u origin master

接下来要给当前版本打tag

git tag 0.1.0

git push --tags

发布

验证

发布到CocoaPods要求podspec文件必须正确,需要验证其正确性

pod spec lint

此时会有Error和Warn,其中Error必须处理,但Warn可以忽略

pod lib lint FMPaymentManager.podspec --allow-warnings --use-libraries

验证通过,但如果如果项目要求严格还是建议对照信息进行处理以免后期造成不必要的麻烦

提交

通过trunk进行注册

pod trunk register gaoxx@ifeng.com --description='description'

image.png 打开邮箱进行验证

image.png

推到CocoaPods pod trunk push FMPaymentManager.podspec --allow-warnings

image.png

注意:这里遇到一个问题[iOS] file patterns: The source_files pattern did not match any file,找到了一个好的解决方式,其他的都没有什么用,点这里