最近在學習組件化相關的知識谨究,也準備寫個項目練練手。iOS組件化的實現(xiàn)是利用CocoaPods制作Pod庫胶哲,主工程分別引用這些Pod庫。最終想要達到的目標是主工程只是一個殼工程潭辈,其它代碼都在組件Pod里澈吨,主工程只負責加載這些組件寄摆,沒有其它任何代碼谅辣。
這篇文章作為組件化開發(fā)的前置文章總結一下如何制作一個CocoaPods私有庫冰肴。
下面通過一個例子來講解整個步驟流程屈藐。
1熙尉、打開終端,進入到要建立私有庫工程的目錄检痰,執(zhí)行pod lib create MMUtils
,MMUtils為項目名
這里會詢問幾個問題铅歼,答案根據(jù)實際情況具體設置
命令執(zhí)行完成后會創(chuàng)建一個私有庫工程。
2椎椰、創(chuàng)建私有庫Git地址,這里以GitHub為例
MyGitHubModule是我自己創(chuàng)建的一個Organization慨飘,為了方便組件的統(tǒng)一管理。
3瓤的、修改配置文件MMUtils.podspec
#
# Be sure to run `pod lib lint MMUtils.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 http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'MMUtils'
s.version = '0.0.1'
s.summary = 'MMUtils is some utils for My Project.'
# 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/MyGitHubModule/MMUtils'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'anotherchase@gmail.com' => 'AnotherChase@gmail.com' }
s.source = { :git => 'https://github.com/MyGitHubModule/MMUtils.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'MMUtils/Classes/**/*'
# s.resource_bundles = {
# 'MMUtils' => ['MMUtils/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
s.dependency 'Reachability', '~> 3.2'
s.dependency 'ReactiveCocoa', '~> 2.5'
end
podspec文件的詳細說明可以看Podspec Syntax Reference。
4圈膏、打開終端,進入Example文件夾稽坤,執(zhí)行pod install
,安裝依賴項
5尿褪、添加庫的源碼文件
將源碼文件放入MMUtils/Classes
文件下,與podspec文件中的配置要保持一致茫多。這里我是放入了一個監(jiān)聽網絡狀態(tài)變化的工具類忽刽。
運行pod install
夺欲,讓工程加載新添加的類。這里需要注意的是今膊,只要新增加類、資源文件或依賴的第三方庫都需要重新運行pod install
來進行更新斑唬。
6、添加測試代碼恕刘,運行工程測試
在MMViewController中添加測試代碼
#import "MMViewController.h"
#import <MMUtils/MMReachabilityHelper.h>
#import <ReactiveCocoa/ReactiveCocoa.h>
@interface MMViewController ()
@property (nonatomic, strong) UILabel *statusLabel;
@end
@implementation MMViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 100) / 2.0, 100, 100, 100)];
self.statusLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.statusLabel];
RAC(self.statusLabel, text) = [RACObserve(ReachabilityHelper, networkStatus) map:^(NSNumber *networkStatus) {
return networkStatus.integerValue == NotReachable ? @"無網絡" : @"有網絡";
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
運行工程測試,切換網絡狀態(tài)褐着,可以看到label文字的變化。
7含蓉、運行pod lib lint MMUtils.podspec
驗證私有庫正確性
如果出現(xiàn)
[!] MMUtils did not pass validation, due to 15 warnings (but you can use `--allow-warnings` to ignore them).
You can use the `--no-clean` option to inspect any issue.
可以運行pod lib lint MMUtils.podspec --allow-warnings
來忽略警告。
8馅扣、提交源碼到GitHub,并打tag
在項目工程文件下運行以下命令:
# chenjb @ chenjbdeMBP in ~/Desktop/MyGitHubModule/MMUtils on git:master x [20:41:30]
$ git remote add origin https://github.com/MyGitHubModule/MMUtils.git
# chenjb @ chenjbdeMBP in ~/Desktop/MyGitHubModule/MMUtils on git:master x [21:01:06]
$ git add .
# chenjb @ chenjbdeMBP in ~/Desktop/MyGitHubModule/MMUtils on git:master x [21:01:17]
$ git commit -a -m "0.0.1"
# chenjb @ chenjbdeMBP in ~/Desktop/MyGitHubModule/MMUtils on git:master o [21:01:24]
$ git pull origin master
# chenjb @ chenjbdeMBP in ~/Desktop/MyGitHubModule/MMUtils on git:master o [21:01:38] C:1
$ git push origin master
# chenjb @ chenjbdeMBP in ~/Desktop/MyGitHubModule/MMUtils on git:master o [21:01:59]
$ git tag 0.0.1
# chenjb @ chenjbdeMBP in ~/Desktop/MyGitHubModule/MMUtils on git:master o [21:02:08]
$ git push origin 0.0.1
運行完成后差油,可以去GitHub上對我們的提交進行查看。
9厌殉、發(fā)布私有庫
對于開源庫,podspec文件是放在CocoaPods的Specs項目下的公罕。
因為我們創(chuàng)建的是私有庫,所以我們需要創(chuàng)建自己的Specs管理庫楼眷。
創(chuàng)建一個Git庫命名為MMSpecs,創(chuàng)建過程和第二步一樣罐柳。
打開終端,運行
$ pod repo add MMSpecs https://github.com/MyGitHubModule/MMSpecs.git
執(zhí)行成功后张吉,可以看到在本地生成了MMSpecs目錄齿梁。
運行
# chenjb @ chenjbdeMBP in ~/Desktop/MyGitHubModule/MMUtils on git:master o [21:58:28]
$ pod repo push MMSpecs MMUtils.podspec
進行發(fā)布。
如果沒通過勺择,可以試試pod repo push MMSpecs MMUtils.podspec --allow-warnings
忽略警告。
成功之后可以在GitHub上對MMSpecs進行查看省核。
10、在自己項目中引用私有庫
Podfile如下:
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/MyGitHubModule/MMSpecs.git'
platform :ios, ‘8.0’
target “TestTest” do
pod 'MMUtils', '~> 0.0.1’
end
這里很尷尬气忠,發(fā)現(xiàn)開源庫中也有個同名的庫。
修改Podfile為:
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/MyGitHubModule/MMSpecs.git'
platform :ios, ‘8.0’
target “TestTest” do
pod 'MMUtils', :git => 'https://github.com/MyGitHubModule/MMUtils.git', :tag => '0.0.1'
end
讀者朋友這個操作就不要學了旧噪。
具體步驟流程就是這樣了,這是我2018年的第一篇博客淘钟,希望自己在2018年接下來的日子里也能繼續(xù)努力。大家共勉日月。
本文示例代碼下載:MMUtils