Swift制作公有库

103 阅读2分钟

1.新建github仓库

注意点

  • Repository Name 仓库名
  • Add a license 开源许可证 一般选MIT

2.本地新建.podsepc文件

新建一个文件夹,打开终端,执行命令

$ wujiandeMacBook-Pro:RLPublicLib wujian$ pod lib create RLPublicLib

回答以下问题

What platform do you want to use?? [ iOS / macOS ]
 > iOS

What language do you want to use?? [ Swift / ObjC ]
 > Swift

Would you like to include a demo application with your library? [ Yes / No ]
 > Yes

Which testing frameworks will you use? [ Quick / None ]
 > None

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

Running pod install on your new library.

完成后会自动生成带有Example的工程,工程目录如图所示

2.编辑.podspec文件

#
# Be sure to run `pod lib lint RLPublicLib.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             = 'RLPublicLib'
  s.version          = '0.1.0'
  s.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!

  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://www.jianshu.com/p/710e5c598d76'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { '1056709274@qq.com' => 'wujianren@chandashi.com' }
  s.source           = { :git => 'https://github.com/Conan-R/PublicLibTest.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '9.3'

  s.source_files = 'RLPublicLib/Classes/**/*'
  
  # s.resource_bundles = {
  #   'RLPublicLib' => ['RLPublicLib/Assets/*.png']
  # }
  s.frameworks = 'UIKit'
  s.dependency 'SnapKit'
  s.swift_version = '4.2'
end

官方文档

注意

  • s.summary 公有库简介 一定要修改

  • wift的话记得加版本号 s.swift_version = '4.2'

  • 依赖的第三方库可以使用s.dependency 'SnapKit'

  • s.source 的地址要修改成github仓库地址

编辑完可以使用命令进行验证 pod lib lint [xxx.podspec] [--allow-warnings] [--verbose]

$ wujiandeMacBook-Pro:RLPublicLib wujian$ pod lib lint

 -> RLPublicLib (0.1.0)
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description
    - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')

RLPublicLib passed validation.
wujiandeMacBook-Pro:RLPublicLib wujian$ 

3.加入项目文件

$ pod update --verbose --no-repo-update

4.把文件push到github的仓库上,并打tag包

$ git remote add origin https://github.com/Conan-R/PublicLibTest.git
$ git push -u origin master
$ git tag -a 0.1.0 -m '创建 pod 库'
$ git push origin --tags

5.注册Trunk

先用命令 pod trunk me 判断是否已经注册

wujiandeMacBook-Pro:RLPublicLib wujian$ pod trunk me
[!] Authentication token is invalid or unverified. Either verify it with the email that was sent or register a new session.

我这里是还没注册, 执行命令进行注册pod trunk register EMAIL [YOUR_NAME]

pod trunk register 1056709274@qq.com "RL_Conan"

到自己邮箱里打开邮件打开链接看见下面这个画面就OK了, 我这里是在垃圾邮件里找到的

再次执行命令pod trunk me

$ wujiandeMacBook-Pro:RLPublicLib wujian$ pod trunk register 1056709274@qq.com "RL_Conan"
[!] Please verify the session by clicking the link in the verification email that has been sent to 1056709274@qq.com
wujiandeMacBook-Pro:RLPublicLib wujian$ pod trunk me
  - Name:     Conan-R
  - Email:    1056709274@qq.com
  - Since:    March 10th, 07:26
  - Pods:
    - RLBase
    - RLNewBase
  - Sessions:
    - March 10th, 07:26 -    July 24th, 22:58. IP: 106.122.182.15
    - August 3rd, 21:31 - December 9th, 21:34. IP: 117.28.112.79 

6.发布公有库

执行命令: pod trunk push [NAME].podspec

$ pod trunk push RLPublicLib.podspec

最后可以通过命令查找库: pod search RLPublicLib