如何制做一个CocoaPods私有库

最近在学习组件化相关的知识,也准备写个项目练练手。iOS组件化的实现是利用CocoaPods制做Pod库,主工程分别引用这些Pod库。最终想要达到的目标是主工程只是一个壳工程,其它代码都在组件Pod里,主工程只负责加载这些组件,没有其它任何代码。html

这篇文章做为组件化开发的前置文章总结一下如何制做一个CocoaPods私有库。ios

下面经过一个例子来说解整个步骤流程。git

一、打开终端,进入到要创建私有库工程的目录,执行pod lib create MMUtils,MMUtils为项目名

这里会询问几个问题,答案根据实际状况具体设置github

命令执行完成后会建立一个私有库工程。bash

二、建立私有库Git地址,这里以GitHub为例

MyGitHubModule是我本身建立的一个Organization,为了方便组件的统一管理。网络

三、修改配置文件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 Referenceapp

四、打开终端,进入Example文件夹,执行pod install,安装依赖项

五、添加库的源码文件

将源码文件放入MMUtils/Classes文件下,与podspec文件中的配置要保持一致。这里我是放入了一个监听网络状态变化的工具类。ide

运行pod install,让工程加载新添加的类。这里须要注意的是,只要新增长类、资源文件或依赖的第三方库都须要从新运行pod install来进行更新。工具

六、添加测试代码,运行工程测试

在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.
}
复制代码

运行工程测试,切换网络状态,能够看到label文字的变化。

七、运行pod lib lint MMUtils.podspec验证私有库正确性

若是出现

[!] 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来忽略警告。

八、提交源码到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上对咱们的提交进行查看。

九、发布私有库

对于开源库,podspec文件是放在CocoaPods的Specs项目下的。

由于咱们建立的是私有库,因此咱们须要建立本身的Specs管理库。

建立一个Git库命名为MMSpecs,建立过程和第二步同样。

打开终端,运行

$ pod repo add MMSpecs https://github.com/MyGitHubModule/MMSpecs.git
复制代码

执行成功后,能够看到在本地生成了MMSpecs目录。

运行

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

进行发布。

若是没经过,能够试试pod repo push MMSpecs MMUtils.podspec --allow-warnings忽略警告。

成功以后能够在GitHub上对MMSpecs进行查看。

十、在本身项目中引用私有库

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 复制代码

这里很尴尬,发现开源库中也有个同名的库。

修改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年接下来的日子里也能继续努力。你们共勉。

本文示例代码下载:MMUtils

参考连接
相关文章
相关标签/搜索