使用Cocoapods創(chuàng)建開源庫 及 更新

轉(zhuǎn)載自:CSDN 使用Cocoapods創(chuàng)建開源庫 及 更新

題外話:項目中有一些組件需要從項目中剝離出來,使用Cocoapods創(chuàng)建私有的podspec并在項目中部署使用

實現(xiàn)步驟(基于pods自動創(chuàng)建)

在github 上新建一個repository

名字為 YYLib,如圖:


這里寫圖片描述

1.克隆這個項目到本地 git checkout /clone ***

使用命令創(chuàng)建模板項目:pod lib create [podName]

該命令會根據(jù)輸入的信息自動創(chuàng)建名為podName的本地git倉庫,在項目的根目錄下自動創(chuàng)建了podName.podspec、README.md、LICENSE文件,自動創(chuàng)建使用了podName庫的Demo項目,在Pods工程下自動創(chuàng)建pod個人庫開發(fā)文件夾。

$ pod lib create YYLib

命令執(zhí)行后,會讓確認一下幾個問題

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 ]

Kiwi

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

yes

What is your class prefix?

CY

回答完這幾個問題,用 XCode打開該庫,如圖所示:


這里寫圖片描述

用 XCode打開 YYLib.podspec,進行修改

Pod::Spec.new do |s|
  s.name             = 'YYLib'
  s.version          = '0.1.0'
  s.summary          = 'it is my pod Demo'

# 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
it is my pod Demo.
                       DESC

  s.homepage         = 'https://github.com/<GITHUB_USERNAME>/YYLib'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'yanyanforest' => 'yanyanforest@163.com' }
  s.source           = { :git => 'https://github.com/yanyanforest/YYLib.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '7.0'

  s.source_files = 'YYLib/Classes/**/*'

  # s.resource_bundles = {
  #   'YYLib' => ['YYLib/Assets/*.png']
  # }

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

修改文件podspec和README 文件,添加封裝的類。確保無誤。

驗證本地.podspec 文件的有效性(下面文中)

再把本地的文件提交并打 tag

$ git add .

$ git commit -a -m ‘first commit’//提交代碼,并標注此次提交的信息

$ git push --set-upstream origin master

$ git remote add origin https://github.com/yanyanforest/YYLib.git

$ git push -u origin master

$ git tag -m “first release” “0.1.0”

$ git push --tags #推送tag到遠端倉庫

驗證本地.podspec 文件的有效性

$ pod lib lint YYLib.podspec

 -> YYLib (0.1.0)
    - WARN  | url: The URL (https://github.com/yanyanforest/YYLib) is not reachable.

[!] YYLib did not pass validation, due to 1 warning (but you can use `--allow-warnings` to ignore it) and all results apply only to public specs, but you can use `--private` to ignore them if linting the specification for a private pod.
You can use the `--no-clean` option to inspect any issue.

如果有 Error這需要更改 error。如果有 warning, 但有不改正需要執(zhí)行下面的命令:
$ pod lib lint YYLib.podspec --allow-warnings

在 pod 上創(chuàng)建庫 遠程托管地址為https://github.com/yanyanforest/YYLib.git

$ pod repo add YYLib https://github.com/yanyanforest/YYLib.git

正常執(zhí)行結(jié)果為:

Cloning spec repo `YYOCFramework` from `https://github.com/yanyanforest/YYLib.git`

如果報錯為

[!] /usr/bin/git clone https://github.com/yanyanforest/YYLib.git YYLib

Cloning into 'YYLib'...
remote: Repository not found.
fatal: repository 'https://github.com/yanyanforest/YYLib.git/' not found

解決方法:去自己的git官網(wǎng) 創(chuàng)建一個repository 名字為YYLib,之后執(zhí)行

$ pod repo add YYLib https://github.com/yanyanforest/YYLib.git

Cloning spec repo YYLib from https://github.com/yanyanforest/YYLib.git

再次執(zhí)行

$ pod lib lint YYLib.podspec

YYLib passed validation.

推到 pod 上

$ pod trunk push YYLib.podspec --allow-warnings

Log messages:

  • December 1st, 20:18: Push for `YYLib 0.1.0' initiated.
  • December 1st, 20:18: Push for`YYLib 0.1.0’ has been pushed
    (0.207867721 s).

分支

查看自己 pod 的信息。

$ pod trunk me

  - Name:     yanyanforest
  - Email:    yanyanforest@163.com
  - Since:    November 28th, 20:30
  - Pods:
    - UICollectionViewLeftAlignFlowLayout
    - YYLib
  - Sessions:
    - November 28th, 20:30 日期地址
    Description: yanyan

$ pod trunk push YYLib.podspec

[!] You need to register a session first.

$ pod trunk register yanyanforest@163.com ‘yanyanforest’ –description=’yanyan’

[!] Please verify the session by clicking the link in the verification email that has been sent to yanyanforest@163.com
進入自己的郵箱,打開鏈接即可。

新建一個項目測試一下自己的類庫

pod search 一下自己的類庫

$ pod search YYLib

問題

pod search 搜索不到庫

Unable to find a pod with name, author, summary, or descriptionmatching 「庫名」

解決方法:輸入下面命令
rm ~/Library/Caches/CocoaPods/search_index.json

An unexpected version directory Assets was encountered for the /Users/yanyan/.cocoapods/repos/** Pod in the YYLib repository.

解決方法:進入指定文件夾repos把YYLib刪除即可。

回到指定 tag 的版本

$ git checkout -B master 0.1.3

注意這里的參數(shù)-B 和-b 的區(qū)別

git checkout -b
git checkout -b|-B []
Specifying -b causes a new branch to be created as if git-branch(1)
were called and then checked out. In this case you can use the
–track or –no-track options, which will be passed to git branch.
As a convenience, –track without -b implies branch creation; see
the description of –track below.

       If -B is given, <new_branch> is created if it doesn't exist;
       otherwise, it is reset. This is the transactional equivalent of

           $ git branch -f <branch> [<start point>]
           $ git checkout <branch>

       that is to say, the branch is not reset/created unless "git
       checkout" is successful.

-b
Create a new branch named and start it at
; see git-branch(1) for details.

-B
Creates the branch and start it at ; if
it already exists, then reset it to . This is
equivalent to running “git branch” with “-f”; see git-branch(1) for
details.

bug

[!] Authentication token is invalid or unverified. Either verify it with the email that was sent or register a new session.
或者

[!] You need to register a session first.

解決方法:

終端輸入命令:$ pod trunk register 郵箱地址 名字

  • ERROR | [iOS] unknown: Encountered an unknown error (Simulator iPhone 4s is not available.) during validation.
    Pods workspace available at/var/folders/wx/yl8z3mrs3_x4hrznm38rhdj00000gn/T/CocoaPods/Lint/App.xcworkspace for inspection.

解決方法:

! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to ‘https://github.com/yanyanforest/UICollectionViewLeftAlignFlowLayout.git

解決方法:
$ git push -f origin master


更新

打開Example 下的項目文件: 庫名.xcworkspace
找到Podfile,修改 pod ‘CYFramework’, :path => ‘../’
在終端進入到庫文件夾
執(zhí)行$ pod update
這里注意:有時會報如下錯誤

$ pod update

Update all pods
Updating local specs repositories
  $ /usr/bin/git -C /Users/yanyan/.cocoapods/repos/master fetch origin
  --progress
  fatal: unable to access 'https://github.com/CocoaPods/Specs.git/': LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 
[!] CocoaPods was not able to update the `master` repo. If this is an unexpected issue and persists you can inspect it running `pod repo update --verbose`

解決方法
$ env GIT_SSL_NO_VERIFY=true
再執(zhí)行$ pod update
這里順便注意一下,只要在過程中遇到LibreSSL SSL_connect: SSL_ERROR_SYSCALL ... 443 的錯誤就在終端輸入該命令即可$ env GIT_SSL_NO_VERIFY=true

修改或者添加相應的文件, 修改對應的文件 :[庫名].podspec 版本號

驗證podspec文件的有效性

$ pod lib lint --allow-warnings

提交代碼 打tag 該tag值和[庫名].podspec中版本號保持一致
$ git tag 0.1.3 $ git push --tags
最后 trunk push [ 庫名].podspec
$ pod trunk push [ 庫名].podspec --allow-warnings
結(jié)果:

這里寫圖片描述

3.Unable to find a specification for 'xxx' depended upon by 'xxx'

pod lib lint WTxxxSDK.podspec

 -> WTxxSDK (0.1.0)
    - WARN  | source: Git SSH URLs will NOT work for people behind firewalls configured to only allow HTTP, therefore HTTPS is preferred.
    - WARN  | summary: The summary is not meaningful.
    - ERROR | [iOS] unknown: Encountered an unknown error (Unable to find a specification for `WTyySDK (~> 0.1.0)` depended upon by `WTxxSDK`

You have either:
 * out-of-date source repos which you can update with `pod repo update` or with `pod install --repo-update`.
 * mistyped the name or version.
 * not added the source repo that hosts the Podspec to your Podfile.
) during validation.

[!] CMIAPSDK did not pass validation, due to 1 error and 2 warnings.
You can use the `--no-clean` option to inspect any issue.

我的問題是因為,要上傳的私有庫依賴了一個WTyySDK私有庫,導致無法找到。
解決,在pod lib link 時,添加--sources=''

pod spec lint --sources='https://github.com/CocoaPods/Specs.git,http://gitlab.appshahe.com/component/metaapp-ios-specs.git' --use-libraries --allow-warnings
最后我自己的項目中遇得到的一些問題,一些命令的區(qū)分使用:

參考:CocoaPods創(chuàng)建私有pod庫(踩坑篇)
1.pod lib lint 和 pod spec lint
2.pod repo push
pod repo push xxxxxxx-ios_common_spec_repo XXIAPSDK.podspec --allow-warnings
3.“An unexpected version directory ‘Assets’ was encountered for the /Users/yanyan/.cocoapods/repos/** Pod in the xxx repository.”
這可能是由于誤操作將私有庫制作成了私有倉庫了,搞成容器了,可以進入指定文件夾repos把xxx刪除即可,刪除完再發(fā)布就可以了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容