將可以復(fù)用的功能模塊或者業(yè)務(wù)模塊提取出來制作成私有庫撞叽,通過Git托管代碼,CocoaPods來管理這些私有庫。不論是公司內(nèi)部使用還是組件化都是必不可少的停蕉。
手動(dòng)引入三方代碼庫或者其他團(tuán)隊(duì)的代碼庫勺三,有時(shí)候需要對(duì)項(xiàng)目進(jìn)行一些配置雷滚,出現(xiàn)錯(cuò)誤有的還不太好解決,用CocoaPods管理這些代碼庫吗坚,可以簡化配置祈远,可以自動(dòng)配置編譯選項(xiàng)。通過GitLab(或者其他代碼托管平臺(tái))來創(chuàng)建私有庫商源,可以很好的保證的代碼庫的隱私性车份。
本文是使用GitLab和CocoaPods來創(chuàng)建、導(dǎo)入私有庫牡彻。
1. 本地創(chuàng)建私有庫
1. 在本地需要?jiǎng)?chuàng)建私有庫的位置使用pod創(chuàng)建私有庫
pod lib create PrivateRepo
2. 回答幾個(gè)問題
下圖回答僅供參考扫沼,可以根據(jù)實(shí)際情況填寫。
3. 現(xiàn)在會(huì)彈出新建好的項(xiàng)目庄吼,在項(xiàng)目文件夾中找到PrivateRepo.podspec文件缎除,打開文件,進(jìn)行編輯总寻。
文件原始內(nèi)容為:
#
# Be sure to run `pod lib lint PrivateRepo.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'PrivateRepo'
s.version = '0.1.0'
s.summary = 'A short description of PrivateRepo.'
# 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/武志遠(yuǎn)/PrivateRepo'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'Author' => 'wuzhiyuan@hqzhuanche.com' }
s.source = { :git => 'https://github.com/武志遠(yuǎn)/PrivateRepo.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'PrivateRepo/Classes/**/*'
# s.resource_bundles = {
# 'PrivateRepo' => ['PrivateRepo/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
這個(gè)文件的內(nèi)容決定了你項(xiàng)目的配置器罐,依賴。下面分塊進(jìn)行說明:
A. 基本信息
Pod::Spec.new do |s|
//庫名稱
s.name = 'PrivateRepo'
//庫版本號(hào)渐行,這也是我們podfile文件指定的版本號(hào)技矮。 每次發(fā)布版本都需要打tag標(biāo)簽(名稱就是版本號(hào))
s.version = ‘1.0.0'
//pod簡介
s.summary = 'ARC and GCD Compatible Reachability Class for iOS and OS X.'
//詳細(xì)描述
s.description = <<-DESC Computes the meaning of life.
Features:
1. Is self aware
...
42. Likes candies.
DESC
B. 下載鏈接、版本信息
//許可證殊轴,除非源代碼包含了LICENSE.*或者LICENCE.*文件衰倦,否則必須指定許可證文件。文件擴(kuò)展名可以沒有旁理,或者是.txt,.md,.markdown
s.license = { :type => 'BSD’ }
or s.license = ‘MIT'
or s.license = { :type => 'MIT', :file => 'MIT-LICENSE.txt’ }
or s.license = { :type => 'MIT', :text => <<-LICENSE
Copyright 2012
Permission is granted to...
LICENSE
}
//pod主頁
s.homepage = 'https://github.com/tonymillion/Reachability’
//pod庫維護(hù)者的名車和郵箱
s.authors = { 'Tony Million' => 'tonymillion@gmail.com’ }
or s.author = 'Darth Vader'
or s.authors = 'Darth Vader', 'Wookiee'
or s.authors = { 'Darth Vader' => 'darthvader@darkside.com',
'Wookiee' => 'wookiee@aggrrttaaggrrt.com' }
//指定多媒體地址樊零,如果是推特發(fā)布版本會(huì)有通知
s.social_media_url = 'https: //twitter.com/cocoapods
or s.social_media_url = 'https: //groups.google.com/forum/#!forum/cocoapods'
//獲取庫的地址
a. Git:git地址,tag:值以v開頭孽文,支持子模塊
s.source = { :git => 'https://github.com/tonymillion/Reachability.git', :tag => 'v3.1.0’ }
s.source = { :git => 'https://github.com/typhoon-framework/Typhoon.git',
:tag => "v#{s.version}", :submodules => true }
b. Svn:svn地址
s.source = { :svn => 'http://svn.code.sf.net/p/polyclipping/code', :tag => ‘4.8.8‘ }
c. Hg:Mercurial
s.source = { :hg => 'https://bitbucket.org/dcutting/hyperbek', :revision => "#{s.version}" }
// Pod 屏幕截圖驻襟,支持單個(gè)或者數(shù)組,主要適用于UI類的pod庫芋哭。cocoapods推薦使用gif
s.screenshot = 'http://dl.dropbox.com/u/378729/MBProgressHUD/1.png'
or s.screenshots = [ 'http://dl.dropbox.com/u/378729/MBProgressHUD/1.png',
'http://dl.dropbox.com/u/378729/MBProgressHUD/2.png' ]
// 說明文檔地址
s.documentation_url = 'http://www.example.com/docs.html’
// pod下載完成之后沉衣,執(zhí)行的命令〖跷可以創(chuàng)建豌习,刪除存谎,修改任何下載的文件。該命令在pod清理之前和pod創(chuàng)建之前執(zhí)行肥隆。
s.prepare_command = 'ruby build_files.rb'
or s.prepare_command = <<-CMD sed -i 's/MyNameSpacedHeader/Header/g' ./**/*.h
sed -i 's/MyNameOtherSpacedHeader/OtherHeader/g' ./**/*.h
CMD
//module name
s.module_name = ‘Rich'
//支持的swift版本
s.swift_version = ‘4.0'
// 支持的Cocoapods版本
s.cocoapods_version = ‘>=0.36’
C. 是否要廢棄庫
// 庫是否廢棄
s.deprecated = true
// 廢棄的pod名稱
s.deprecated_in_favor_of = 'NewMoreAwesomePod'
D. 如果項(xiàng)目中依賴了靜態(tài)庫既荚,比如:,需要說明使用靜態(tài)庫
// 是否使用靜態(tài)庫栋艳。如果podfile指明了use_frameworks!命令恰聘,但是pod倉庫需要使用靜態(tài)庫則需要設(shè)置
s.static_framework = true
E. 支持平臺(tái)
// pod支持的平臺(tái),如果沒有設(shè)置意味著支持所有平臺(tái),使用deployment_target支持選擇多個(gè)平臺(tái)
s.platform = :osx, ’10.8'
or s.platform = :ios
or s.platform = :osx
F. 添加Framework
s.vendored_frameworks = 'SQShareKit/ShareKitPlatformSDK/QQ/*.framework' # 使用的三方framework
G. 添加bundle資源
s.resource_bundles = {
'TencentOpenApi_IOS_Bundle' => ['SQShareKit/ShareKitPlatformSDK/QQ/*'] # 資源文件
}
H. 依賴其他三方庫
s.dependency 'WechatOpenSDK'
I. 如果有文件基于MRC吸占,有文件支持ARC晴叨。可以使用subspec子模塊配置來解決矾屯。
s.requires_arc = true # 基于ARC
non_arc_files = 'SQYCCategories/Classes/Foundation/NSThread+SQAdd.{h,m}' # 指出不支持ARC的文件
s.exclude_files = non_arc_files
s.subspec 'no-arc' do |sp| # 使用subspec(子模塊配置)兼蕊,每一個(gè)子模塊中可以各自設(shè)置,決定是否使用ARC
sp.source_files = non_arc_files
sp.requires_arc = false
end
4. 編輯好PrivateRepo.podspec文件之后在问拘,自動(dòng)生成項(xiàng)目的Example下執(zhí)行pod install遍略,可以將依賴的三方庫都pod下來
5.在PrivateRepo的Class文件夾下添加你的代碼。然后再回到Example文件夾下執(zhí)行pod install骤坐。如果PrivateRepo.podspec文件或者PrivateRepo文件下的內(nèi)容有改動(dòng)的話绪杏,都需要在Example下執(zhí)行pod install。
6. 可以在Example項(xiàng)目中測試一下代碼有沒有問題纽绍。
7. 在GitLab上創(chuàng)建私有項(xiàng)目
記錄下來項(xiàng)目的地址和git地址
8. 運(yùn)行pod lib lint PrivateRepo.podspec
驗(yàn)證私有庫正確性
有時(shí)候會(huì)遇到warnings蕾久、添加靜態(tài)庫等問題可以添加下面命令
--allow-warnings 忽略警告
--verbose 顯示檢查編譯的詳細(xì)信息
--use-libraries 使用靜態(tài)庫
基本上執(zhí)行pod lib lint PrivateRepo.podspec --verbose --use-libraries --allow-warnings
這一行就直接梭哈了。
9. 提交代碼到GitHub拌夏,并打tag
$ git remote add origin http://gitlab.hqzhuanche.com/app/iOS-Architecture/PrivateRepo.git
$ git add .
$ git commit -a -m "1.0.0"
$ git pull origin master
$ git push origin master
$ git tag 1.0.0
$ git push origin 1.0.0
10. 創(chuàng)建發(fā)布私有庫
在GitLab上繼續(xù)創(chuàng)建一個(gè)私有項(xiàng)目僧著,用來保存.podspec
文件,然后項(xiàng)目里面通過這個(gè)私有項(xiàng)目來加載私有庫障簿。
這個(gè)私有項(xiàng)目命名為:PrivateSpecs
執(zhí)行終端命令
pod repo add PrivateSpecs http://gitlab.hqzhuanche.com/app/iOS-Architecture/PrivateSpecs.git
11. 將PrivateRepo.podspec放到PrivateSpecs上盹愚。
pod repo push PrivateSpecs PrivateRepo.podspec --allow-warnings
12. 在你的項(xiàng)目里編輯Podfile文件
source 'https://github.com/CocoaPods/Specs.git'
source 'http://gitlab.hqzhuanche.com/app/iOS-Architecture/PrivateSpecs.git'
platform :ios, ‘8.0’
target “YourProject” do
pod 'PrivateRepo', '~> 1.0.0’
end
參考鏈接:
- 私有倉庫的創(chuàng)建范例一
https://juejin.im/post/5accdbc86fb9a028ca534f2e - 私有倉庫的創(chuàng)建范例二
https://blog.devzeng.com/blog/ios-cocoapods-private-repo.html - 私有倉庫的創(chuàng)建范例三
http://www.reibang.com/p/0c640821b36f - pod庫包含MRC的文件
https://blog.csdn.net/pwf2006/article/details/77244163 - 加載圖片等資源文件
http://www.reibang.com/p/1699f537558a - 解決報(bào)錯(cuò) - ERROR | [iOS] unknown: Encountered an unknown error (The 'Pods-App' tar......
https://blog.csdn.net/weixin_34006468/article/details/87206571