如何制作一個(gè)CocoaPods私有庫(kù)

最近在學(xué)習(xí)組件化相關(guān)的知識(shí),也準(zhǔn)備寫(xiě)個(gè)項(xiàng)目練練手。iOS組件化的實(shí)現(xiàn)是利用CocoaPods制作Pod庫(kù),主工程分別引用這些Pod庫(kù)。最終想要達(dá)到的目標(biāo)是主工程只是一個(gè)殼工程,其它代碼都在組件Pod里,主工程只負(fù)責(zé)加載這些組件,沒(méi)有其它任何代碼。

這篇文章作為組件化開(kāi)發(fā)的前置文章總結(jié)一下如何制作一個(gè)CocoaPods私有庫(kù)。

下面通過(guò)一個(gè)例子來(lái)講解整個(gè)步驟流程。

1、打開(kāi)終端,進(jìn)入到要建立私有庫(kù)工程的目錄,執(zhí)行pod lib create MMUtils,MMUtils為項(xiàng)目名

這里會(huì)詢(xún)問(wèn)幾個(gè)問(wèn)題,答案根據(jù)實(shí)際情況具體設(shè)置

命令執(zhí)行完成后會(huì)創(chuàng)建一個(gè)私有庫(kù)工程。

2、創(chuàng)建私有庫(kù)Git地址,這里以GitHub為例

MyGitHubModule是我自己創(chuàng)建的一個(gè)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文件的詳細(xì)說(shuō)明可以看Podspec Syntax Reference

4、打開(kāi)終端,進(jìn)入Example文件夾,執(zhí)行pod install,安裝依賴(lài)項(xiàng)
5、添加庫(kù)的源碼文件

將源碼文件放入MMUtils/Classes文件下,與podspec文件中的配置要保持一致。這里我是放入了一個(gè)監(jiān)聽(tīng)網(wǎng)絡(luò)狀態(tài)變化的工具類(lèi)。

運(yùn)行pod install,讓工程加載新添加的類(lèi)。這里需要注意的是,只要新增加類(lèi)、資源文件或依賴(lài)的第三方庫(kù)都需要重新運(yùn)行pod install來(lái)進(jìn)行更新。

6、添加測(cè)試代碼,運(yùn)行工程測(cè)試

在MMViewController中添加測(cè)試代碼

#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 ? @"無(wú)網(wǎng)絡(luò)" : @"有網(wǎng)絡(luò)";
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

運(yùn)行工程測(cè)試,切換網(wǎng)絡(luò)狀態(tài),可以看到label文字的變化。

7、運(yùn)行pod lib lint MMUtils.podspec驗(yàn)證私有庫(kù)正確性

如果出現(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.

可以運(yùn)行pod lib lint MMUtils.podspec --allow-warnings來(lái)忽略警告。

8、提交源碼到GitHub,并打tag

在項(xiàng)目工程文件下運(yùn)行以下命令:

# 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

運(yùn)行完成后,可以去GitHub上對(duì)我們的提交進(jìn)行查看。

9、發(fā)布私有庫(kù)

對(duì)于開(kāi)源庫(kù),podspec文件是放在CocoaPods的Specs項(xiàng)目下的。

因?yàn)槲覀儎?chuàng)建的是私有庫(kù),所以我們需要?jiǎng)?chuàng)建自己的Specs管理庫(kù)。

創(chuàng)建一個(gè)Git庫(kù)命名為MMSpecs,創(chuàng)建過(guò)程和第二步一樣。

打開(kāi)終端,運(yùn)行

$ pod repo add MMSpecs https://github.com/MyGitHubModule/MMSpecs.git

執(zhí)行成功后,可以看到在本地生成了MMSpecs目錄。

運(yùn)行

# chenjb @ chenjbdeMBP in ~/Desktop/MyGitHubModule/MMUtils on git:master o [21:58:28] 
$ pod repo push MMSpecs MMUtils.podspec 

進(jìn)行發(fā)布。

如果沒(méi)通過(guò),可以試試pod repo push MMSpecs MMUtils.podspec --allow-warnings忽略警告。

成功之后可以在GitHub上對(duì)MMSpecs進(jìn)行查看。

10、在自己項(xiàng)目中引用私有庫(kù)

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)開(kāi)源庫(kù)中也有個(gè)同名的庫(kù)。

修改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

讀者朋友這個(gè)操作就不要學(xué)了。

具體步驟流程就是這樣了,這是我2018年的第一篇博客,希望自己在2018年接下來(lái)的日子里也能繼續(xù)努力。大家共勉。

本文示例代碼下載:MMUtils

參考鏈接
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容