一、簡介
為了實現(xiàn)組件化開發(fā),方便每個模塊到開發(fā)和版本控制,為每個組件創(chuàng)建CocoaPods私有庫是目前最有效的方案。
最新最詳細的教程請前往iOS CocoaPods私有庫的創(chuàng)建和版本更新
二、步驟
- 打開終端cd進入到要創(chuàng)建私有庫到目錄
- 在終端上執(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`.
- 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.
- 打開Example項目的
PrivatePod.xcworkspace
文件,將源文件目錄Classes拖進項目,并新建類目NSString+YDCategory
文件
源文件目錄Classe
文件目錄'Classes'拖進項目的目的是可以自己在源文件進行修改,也可以自己在項目文件夾下新新建個目錄,然后再修改一下
PrivatePod.podspec
文件中的源文件路徑s.source_files
,'Classes'目錄中.gitkeep
文件可能被一起拖進項目,可以刪除.gitkeep
文件的引用。
在類目中添加方法printSelf
- (void) printSelf
{
NSLog(@"YDCategory: %@", self);
}
在控制器中添加代碼
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"PrivatePod";
[self.title printSelf];
}
運行項目輸出結(jié)果:
- 修改
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
中的源文件有剛新建的類目文件
-
在碼云上新建對應的私有庫PrivatePod
新建私有庫
將生成的git地址在PrivatePod.podspec
文件中替換對應的git地址
s.source = { :git => 'git地址', :tag => s.version.to_s }
- 驗證Pod配置文件
pod lib lint
驗證出錯
驗證失敗,原因是CocoaPods
版本太低
更新CocoaPods
sudo gem install cocoapods
再次驗證通過
- 將項目推送到遠程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ā)生沖突。
我使用了sourcetree
的代碼合并工具,刪除了遠程倉庫的readme
文件內(nèi)容,推送成功了,并打了標簽
- 創(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
- 驗證私有庫是否發(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中的源碼
在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é)果:
參考文章:
IOS創(chuàng)建CocoaPods私有庫
iOS創(chuàng)建自己的私有cocoaPods庫