cocoapods搭建私有库,pod框架

1,226 阅读5分钟

框架发布基本流程图

一.注册 COCOAPODS 用户

  • 注册session

    /// 注册 session
    $ pod trunk register YOURMAIL 'YOURNAME' --verbose
    opening connection to trunk.cocoapods.org:443...
    opened
    starting SSL for trunk.cocoapods.org:443...
    SSL established, protocol: TLSv1.2, cipher: ECDHE-RSA-AES128-GCM-SHA256
    <- "POST /api/v1/sessions HTTP/1.1\r\nContent-Type: application/json; charset=utf-8\r\nAccept: application/json; charset=utf-8\r\nUser-Agent: CocoaPods/1.7.2\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nHost: trunk.cocoapods.org\r\nContent-Length: 75\r\n\r\n"
    <- "{\"email\":\"YOURMAIL\",\"name\":\"YOURNAME\",\"description\":null}"
    -> "HTTP/1.1 201 Created\r\n"
    -> "Date: Thu, 20 Jun 2019 14:17:48 GMT\r\n"
    -> "Connection: keep-alive\r\n"
    -> "Strict-Transport-Security: max-age=31536000\r\n"
    -> "Content-Type: application/json\r\n"
    -> "Content-Length: 192\r\n"
    -> "X-Content-Type-Options: nosniff\r\n"
    -> "Server: thin 1.6.2 codename Doc Brown\r\n"
    -> "Via: 1.1 vegur\r\n"
    -> "\r\n"
    reading 192 bytes...
    -> "{\"created_at\":\"2019-06-20 14:17:48 UTC\",\"valid_until\":\"2019-10-26 14:17:48 UTC\",\"verified\":false,\"created_from_ip\":\"223.104.3.12\",\"description\":null,\"token\":\"xxxxxxxxxx\"}"
    read 192 bytes
    Conn keep-alive
    [!] Please verify the session by clicking the link in the verification email that has been sent to YOURMAIL
    
  • 去邮箱点击验证链接

  • 验证是否注册成功

    $ pod trunk me
      - Name:     YOURNAME
      - Email:    YOURMAIL
      - Since:    June 2nd, 05:34
      - Pods:
        - PodName
      - Sessions: 
        - June 2nd, 19:49 - October 8th, 19:51. IP: ****   Description:
        macbook pro
    

二.创建一个spec Repo的私有远程仓库 和 一个存放pod所需的项目工程文件的远程仓库

可以去github或者码云之类的平台建立两个空项目就ok了

创建spec Repo
前往文件夹 ~/.cocoapods/repos
pod repo Specname spec的git地址
pod repo BJSpec https://github.com/JunboZhou/BJSpec.git

创建pod的所需的项目工程文件,并上传到远程私有仓库
1.创建本地pod所需的项目工程文件

执行: pod lib create YOURPODNAME,接下来按照提示,根据自己的情况进行就可以了。

找一个本地路径用于开发框架,进入路径,执行pod lib create YOURPODNAME

执行 pod lib create BJPodLib 如下

Cloning `https://github.com/CocoaPods/pod-template.git` into `BJPodLib`.
Configuring BJPodLib 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 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 ]
 > non

Possible answers are [ Specta / Kiwi / None ]
 > none

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

What is your class prefix?
 > BJ

Running pod install on your new library.

完成后 xcode会自动打开工程

然后进到到BJPodLib里面找到Classes中的"ReplaceMe.m"文件删除,然后将自己所需要的文件放到这里目录下,如下图

接下来最重要的一步,也是很容易忽略的一步,不然提交后检查会出现很多问题。 具体步骤: (1)cd 到Example文件下,然后pod install下,更新Example项目的pod。

(2)打开example中的项目,找到项目中的podspec文件,更改spec

Pod::Spec.new do |s|
  s.name             = 'YOURPODNAME'
  s.version          = '0.1.0'
  s.summary          = 'A short description of YOURPODNAME.'

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

  s.ios.deployment_target = '8.0'

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

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end

修改完成后,cd 到BJPodLib目录下,然后验证本地podspec文件有效性,终端命令如下: pod lib lint BJPodLib.podspec

如果类似错误可以加上 --allow-warnings 即可

    - WARN  | summary: The summary is not meaningful.
    - WARN  | url: The URL (https://github.com/JunboZhou/BJPodProject) is not reachable.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | xcodebuild:  note: Building targets in parallel
    - 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')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'BJPodLib' from project 'Pods')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')

[!] BJPodLib did not pass validation, due to 2 warnings (but you can use `--allow-warnings` to ignore them).

pod lib lint BJPodLib.podspec --allow-warnings

2. 本地项目文件上传到远程仓库中

将本地项目与远程仓库相关联,使用终端命令: git remote add origin 远程仓库地址

git remote add origin https://github.com/JunboZhou/BJPodProject.git

提交代码 git push origin master

Enumerating objects: 41, done.
Counting objects: 100% (41/41), done.
Delta compression using up to 4 threads
Compressing objects: 100% (33/33), done.
Writing objects: 100% (41/41), 14.35 KiB | 979.00 KiB/s, done.
Total 41 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To https://github.com/JunboZhou/BJPodProject.git
 * [new branch]      master -> master

这个时候远程仓库就我们本地项目了

打tag 提交

git tag -a 0.1.0 -m "初始版本0.1.0"
git push --tags

之后,再次使用pod spec lint BJPodLib.podspec --allow-warnings,进行.podspec文件本地和远程有效性的验证

验证的时候,可能会报错,或是报警告。报错的话,必须解决,详细看具体信息,一般能找到问题,如果不能,自行 google 😁。警告的话,可以忽略,不过能解决的话,最好解决。

三.向私有的spec Repo远程仓库中提交podspec

使用 pod repo push BJSpec BJPodLib.podspec 命令把私有库添加到私有repo中。 注意:1.仍然在本项目的目录路径下进行操作

  1. 远程仓库中最好不要为空,可以放一个readme文件,如果为空 pull的时候会报错

  2. 如果报验证未通过的错误 specification does not validate记得加上 --allow-warning pod repo push BJSpec BJPodLib.podspec

  这个时候`pod search`就可以看到提交的框架代码了

如果搜索不到,可能是仓库的索引库出现问题,将其删除,重新生成索引。索引缓存地址:

~/Library/Caches/CocoaPods/search_index.json

四.具体用法

新建一个test项目,然后在podfile文件中加入相关配置, pod install