iOS 創(chuàng)建CocoaPods私有庫

CocoaPods

一、簡介

為了實現(xiàn)組件化開發(fā),方便每個模塊到開發(fā)和版本控制,為每個組件創(chuàng)建CocoaPods私有庫是目前最有效的方案。
最新最詳細的教程請前往iOS CocoaPods私有庫的創(chuàng)建和版本更新

二、步驟

  1. 打開終端cd進入到要創(chuàng)建私有庫到目錄
  2. 在終端上執(zhí)行pod lib create <私有庫名稱>來創(chuàng)建庫
OneMacBookPro:YDShelfLife admin$ cd /Users/admin/Documents/RemoteLibrary
OneMacBookPro:RemoteLibrary admin$ pod lib create PrivatePod
Cloning `https://github.com/CocoaPods/pod-template.git` into `PrivatePod`.
Configuring PrivatePod template.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

2020-03-19 11:23:32.387 defaults[62448:8968663]
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:
- https://guides.cocoapods.org/making/using-pod-lib-create.html
( hold cmd and click links to open in a browser. )

Press return to continue.

創(chuàng)建前有幾個配置需要進行選擇

# 選擇使用的平臺
What platform do you want to use?? [ iOS / macOS ]
 > iOS
# 選擇使用的語言
What language do you want to use?? [ Swift / ObjC ]
 > ObjC
# 庫是否包含demo應用
Would you like to include a demo application with your library? [ Yes / No ]
 > Yes
# 選擇用來測試的framework
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?
 > YD
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

Running pod install on your new library.

Analyzing dependencies
Fetching podspec for `PrivatePod` from `../`
Downloading dependencies
Installing FBSnapshotTestCase (2.1.4)
Installing PrivatePod (0.1.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `PrivatePod.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'PrivatePod/Example/PrivatePod.xcworkspace'

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `https://guides.cocoapods.org/making/making-a-cocoapod`.
  1. cd到測試項目Example目錄,執(zhí)行pod install命令,完成
OneMacBookPro:RemoteLibrary admin$ cd /Users/admin/Documents/RemoteLibrary/PrivatePod/Example
OneMacBookPro:Example admin$ pod install
Analyzing dependencies
Fetching podspec for `PrivatePod` from `../`
Downloading dependencies
Using FBSnapshotTestCase (2.1.4)
Using PrivatePod (0.1.0)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.
  1. 打開Example項目的PrivatePod.xcworkspace文件,將源文件目錄Classes拖進項目,并新建類目NSString+YDCategory文件
    源文件目錄Classe

文件目錄'Classes'拖進項目的目的是可以自己在源文件進行修改,也可以自己在項目文件夾下新新建個目錄,然后再修改一下PrivatePod.podspec文件中的源文件路徑s.source_files,'Classes'目錄中.gitkeep文件可能被一起拖進項目,可以刪除.gitkeep文件的引用。

新建類目NSString+YDCategory文件

在類目中添加方法printSelf

- (void) printSelf
{
    NSLog(@"YDCategory: %@", self);
}

在控制器中添加代碼

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"PrivatePod";
    [self.title printSelf];
}

運行項目輸出結(jié)果:

運行結(jié)果
  1. 修改PrivatePod.podspec文件
    使用Sublime Text打開
    PrivatePod.podspec文件
Pod::Spec.new do |s|
  s.name             = 'PrivatePod'
  s.version          = '0.1.0'
  s.summary          = 'Test Private Podspec.'

# 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         = 'http://www.lxweimin.com/u/6b426bd2ebfd'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { '作者' => '郵箱' }
  s.source           = { :git => 'git地址', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

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

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

編輯完成后保存一下,Example項目重新pod install就可以看到Pod中的源文件有剛新建的類目文件

Pod中的源文件

  1. 在碼云上新建對應的私有庫PrivatePod


    新建私有庫

將生成的git地址在PrivatePod.podspec文件中替換對應的git地址

s.source           = { :git => 'git地址', :tag => s.version.to_s }
  1. 驗證Pod配置文件pod lib lint
    驗證出錯

    驗證失敗,原因是CocoaPods版本太低
    更新CocoaPods
sudo gem install cocoapods

再次驗證通過


驗證通過
  1. 將項目推送到遠程Git倉庫
OneMacBookPro:PrivatePod admin$ git remote add origin https://gitee.com/xxxxxx/PrivatePod.git
# 添加所有文件到緩存
OneMacBookPro:PrivatePod admin$ git add .
# 將緩存內(nèi)容提交到本地倉庫
OneMacBookPro:PrivatePod admin$ git commit -a -m "首次提交 version 0.1.0"
# 將本地倉庫推送到遠程倉庫
OneMacBookPro:PrivatePod admin$ git pull origin master --allow-unrelated-histories

推送到遠程倉庫時出現(xiàn)了沖突,導致推送失敗

warning: no common commits
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
From https://gitee.com/heroyoungday/PrivatePod
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
OneMacBookPro:PrivatePod admin$ git push origin master
To https://gitee.com/heroyoungday/PrivatePod.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://gitee.com/heroyoungday/PrivatePod.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

沖突出現(xiàn)在README.md文件,因為前面新建倉庫的時候,錯誤的勾選了使用Readme文件初始化這個倉庫,本地創(chuàng)建CocoaPods私有庫時也自動生成了一個Readme文件,導致推送時發(fā)生沖突。

不要添加readme文件

我使用了sourcetree的代碼合并工具,刪除了遠程倉庫的readme文件內(nèi)容,推送成功了,并打了標簽

使用sourceTree推送成功

  1. 創(chuàng)建本地Spec管理庫
OneMacBookPro:Desktop admin$ cd /Users/admin/Documents/RemoteLibrary/PrivatePod
OneMacBookPro:PrivatePod admin$ pod repo add PrivatePod https://gitee.com/heroyoungday/PrivatePod.git
Cloning spec repo `PrivatePod` from `https://gitee.com/heroyoungday/PrivatePod.git`
OneMacBookPro:PrivatePod admin$ pod repo push PrivatePod PrivatePod.podspec

Validating spec
 -> PrivatePod (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')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'PrivatePod' 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')

Updating the `PrivatePod' repo


Adding the spec to the `PrivatePod' repo

 - [Update] PrivatePod (0.1.0)

Pushing the `PrivatePod' repo
  1. 驗證私有庫是否發(fā)布成功
    新建一個項目PrivatePodDemo,cd進入項目根目錄,執(zhí)行vim Podfile命令,Podfile的內(nèi)容為
platform :ios,'8.0'
target 'PrivatePodDemo' do
   pod 'PrivatePod',:git => 'https://gitee.com/xxxxxxxx/PrivatePod.git'
end

查看Pod中的源碼


查看Pod中的源碼

在ViewController文件中添加代碼

#import "ViewController.h"
#import "NSString+YDCategory.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"PrivatePodDemo";
    [self.title printSelf];
}
@end

運行結(jié)果:


運行結(jié)果

參考文章:
IOS創(chuàng)建CocoaPods私有庫
iOS創(chuàng)建自己的私有cocoaPods庫

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,606評論 6 533
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,582評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,540評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,028評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,801評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,223評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,294評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,442評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,976評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,800評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,996評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,543評論 5 360
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,233評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,662評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,926評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,702評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,991評論 2 374

推薦閱讀更多精彩內(nèi)容