pod init

一、建立私有的Spec Repo

Spec Repo 是全部公開的Pods 的podspec文件的一個git倉庫,當使用Cocoapods後它會被clone到本地的~/.cocoapods/repos目錄下,能夠進入到這個目錄看到master文件夾就是這個官方的Spec Repo了。所以咱們須要建立一個相似於master的私有Spec Repo 。同理這個私有Spec Repo咱們也要有一個遠程端。那麼咱們須要建立一個 Git倉庫,這個倉庫你能夠建立私有的也能夠建立公開的。若是是私有的話,項目中其餘同事,你要給他這個Git倉庫的權限。javascript

在github上建立一個mySpecs倉庫php

在終端執行命令:css

$pod repo add mySpecs https://github.com/xxx/mySpecs.git 

以後在~/.cocoapods/repos目錄下就能看到mySpecshtml

二、建立Pods工程

在本地建立一個test目錄
cd到test目錄並執行終端命令java

$pod lib create pod_test 

終端會顯示ios

Cloning `https://github.com/CocoaPods/pod-template.git` into `pod_test`. Configuring pod_test template. ------------------------------ To get you started we need to ask a few questions, this should only take a minute. 2018-03-01 15:35:21.732 defaults[11084:244747] The domain/default pair of (org.cocoapods.pod-template, HasRunbefore) does not exist If this is your first time we recommend running through with the guide: - http://guides.cocoapods.org/making/using-pod-lib-create.html ( hold cmd and double click links to open in a browser. ) Press return to continue. 

依次填寫iOS、ObjC、Yes、Specta、Yes、JkWgit

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 ] > Specta Would you like to do view based testing? [ Yes / No ] > Yes What is your class prefix? > JKW 

設置完成後打印臺會輸出github

Running pod install on your new library.

Analyzing dependencies
Fetching podspec for `pod_test` from `../` Downloading dependencies Installing Expecta (1.0.6) Installing Expecta+Snapshots (3.1.1) Installing FBSnapshotTestCase (2.1.4) Installing Specta (1.0.7) Installing pod_test (0.1.0) Generating Pods project Integrating client project [!] Please close any current Xcode sessions and use `pod_test.xcworkspace` for this project from now on. Sending stats Pod installation complete! There are 5 dependencies from the Podfile and 5 total pods installed. [!] Automatically assigning platform `ios` with version `9.3` on target `pod_test_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 'pod_test/Example/pod_test.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`. 

成功後會在test目錄下建立一個pod_test工程vim

目錄以下:緩存

 
 

咱們來添加一個分類文件

 
 

NSString+Test分類文件以下

#import <Foundation/Foundation.h> @interface NSString (Test) - (void)test; @end 
#import "NSString+Test.h" @implementation NSString (Test) - (void)test{ NSLog(@"只是一個測試"); } @end 

cd 在Example工程目錄下執行 pod update命令

打開項目工程,能夠看到庫文件都被加載到Pods子項目中了
不過它們並無在Pods目錄下,而是跟測試項目同樣存在於Development Pods/MyLib中,這是由於咱們是在本地測試,而沒有把podspec文件添加到Spec Repo中的緣故。

注:這裏須要注意的是每當添加了新的文件或者之後更新了podspec的版本都須要從新執行一遍pod update命令。

四、提交pods庫到github上

cd 到pod_test目錄下

$git init
$git add .
$ git commit -am "第一次提交" $ git remote add origin https://github.com/xxx/pod_test $ git push origin master //必定要有標籤,否則會有下面的警告 //podspec文件中獲取Git版本控制的項目須要tag號, $ git tag -m "first release" "0.1.0" $ git push --tags 
五、配置pod_test的podspec文件

該pod_test.podspec文件在執行$pod lib create pod_test已經被自動建立

podspec 文件格式

Pod::Spec.new do |s| s.name = 'pod_test' s.version = '0.1.0' s.summary = '這是我第一個私有庫項目demo' s.description = <<-DESC TODO: Add long description of the pod here. DESC s.homepage = 'https://github.com/kamto6/pod_test' # s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2' s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { 'kwjie5566@163.com' => 'kangwei.jie@mydeertrip.com' } s.source = { :git => 'https://github.com/xxx/pod_test.git', :tag => s.version.to_s } # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>' s.ios.deployment_target = '8.0' s.source_files = 'pod_test/Classes/**/*' # s.resource_bundles = { # 'pod_test' => ['pod_test/Assets/*.png'] # } # s.public_header_files = 'Pod/Classes/**/*.h' # s.frameworks = 'UIKit', 'MapKit' # s.dependency 'AFNetworking', '~> 2.3' end 
  • s.name :pod search 搜索的關鍵詞,注意這裏必定要和.podspec的名稱同樣

  • s.version :版本號,每個版本對應一個tag

  • s.summary : 簡介

  • s.homepage : 項目主頁地址

  • s.license : 許可證

  • s.author : 做者

  • s.social_media_url : 社交網址

  • s.source : 項目的地址

  • s.source_files : 須要包含的源文件

  • s.resources: 資源文件

  • s.requires_arc : 是否支持ARC

  • s.dependency :依賴庫

  • s.ios.deployment_target = '8.0' : 支持的pod最低版本

如:

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

「*」 表示匹配全部文件
「**」 表示匹配全部子目錄

驗證該podspec文件,cd到pod_test目錄

$ pod spec lint

pod spec相對於pod lib會更爲精確,pod lib至關於只驗證一個本地倉庫,pod spec會同時驗證本地倉庫和遠程倉庫。

打印臺提示:

-> pod_test (0.1.0) - WARN | summary: The summary is not meaningful. - WARN | url: The URL (https://github.com/kwjie5566@163.com/pod_test) is not reachable. [!] pod_test did not pass validation, due to 2 warnings (but you can use `--allow-warnings` to ignore them). You can use the `--no-clean` option to inspect any issue. 

固然咱們驗證時能夠忽略這些警告

$ pod spec lint --allow-warnings

--allow-warnings是容許warning的存在,也就是說當你在pod spec lint驗證podspec的時候,若是不加這句,而你的代碼裏又一些警告的話,是驗證不經過的。而加上這句話的話,有警告也能驗證經過。

終端輸入:vim pod_test.podspec

按i進入編輯狀態修改:summary (隨便填寫便可) 和 url(post_test對應的倉庫) ,再按esc + : + wq 退出
終端再執行$ pod spec lint

打印臺提示:

-> pod_test (0.1.0) pod_test passed validation. 

表示這個podspec文件驗證經過,不然修改到驗證經過爲止

六、在Example工程修改podfile文件
#pod 'pod_test', :path => '../' pod 'pod_test',:podspec => '../pod_test.podspec' 

再執行pod update

有時候當你使用pod update時會發現特別慢,那是由於pod會默認先更新一次podspec索引。使用--no-repo-update參數能夠禁止其作索引更新操做。

如:pod update --no-repo-update

$cd Example/
$ pod update
Installing pod_test (0.1.0) [!] Error installing pod_test [!] /usr/bin/git clone https://github.com/kamto6/pod_test.git /var/folders/7k/1bb1m_fx0xs3r8pzl28mykz00000gn/T/d20180301-13092-1rajecj --template= --single-branch --depth 1 --branch 0.1.0 Cloning into '/var/folders/7k/1bb1m_fx0xs3r8pzl28mykz00000gn/T/d20180301-13092-1rajecj'... warning: Could not find remote branch 0.1.0 to clone. fatal: Remote branch 0.1.0 not found in upstream origin [!] Automatically assigning platform `ios` with version `9.3` on target `pod_test_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`. 

cd 到pod_test目錄

$ git tag -m "first release" "0.1.0" $ git push --tags 

而後再執行

$cd Example/
$ pod update

再打開Example工程,發現庫文件都被加載到Pods項目下

六、把pod_test.podspec提交到Spec Repo倉庫
pod repo push [本地Spec Repo名稱][podspec文件路徑] 
$ pod repo push mySpecs pod_test.podspec
Validating spec
 -> pod_test (0.1.0)

Updating the `mySpecs' repo Your configuration specifies to merge with the ref 'refs/heads/master' from the remote, but no such ref was fetched. Adding the spec to the `mySpecs' repo - [Add] pod_test (0.1.0) Pushing the `mySpecs' repo 

表示這個庫已經提交到Spec Repo上了

 
 

在對應的spec倉庫地址也能看到提交記錄。

這裏說的添加到私有的Spec Repo,若是要添加到CocoaPods的官方庫,能夠用到trunk工具。

使用終端 pod search pod_test

注意:這裏搜索repos目錄,若是緩存裏沒有該spec文件,則不會搜索到

-> pod_test (0.1.0) 私有庫項目demo pod 'pod_test', '~> 0.1.0' - Homepage: https://github.com/xxx/pod_test - Source: https://github.com/xxx/pod_test.git - Versions: 0.1.0 [mySpecs repo] 

此時到~/.cocoapods/repos/mySpecs

如圖:

查看本地repo信息

$ pod repo

接下來咱們再新建一個工程來測試一下

新建一個工程,在項目的podfile裏添加

#私有spec倉庫的地址,而不是某個pod倉庫的地址 source 'https://github.com/xxx/mySpecs' pod 'pod_test' 

注意:若是該倉庫沒有權限,則會失敗

到此,算是完成pods私有庫的搭建了。

注:若是添加到CocoaPods官方庫上,別人均可以search或者install

cd 到pod_test目錄,執行

pod trunk push pod_test.podspec 

紅色警告:

[!] You need to register a session first. 
$ pod trunk register xxx@163.com 'xxx' --description='pro' 

再執行

$pod trunk push pod_test.podspec 

若是提示:

[!] You (xxx@163.com) are not allowed to push new versions for this pod. The owners of this pod are aaaa@126.com. 

呵呵。大體的意思是repo 已經被這個全部者佔有了

$pod trunk info pod_test
 
終端打印:

pod_test
    - Versions: - 0.0.2 (2017-01-13 07:03:27 UTC) - Owners: - aaa <aaa@126.com> 

若是沒有問題,那麼發佈到Cocoapods後,在終端更新本地pods倉庫信息

$ pod setup

而後查詢

$ pod search pod_test

做者:kamto 連接:https://www.jianshu.com/p/006d6ab89c3c 來源:簡書 簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。
相關文章
相關標籤/搜索