[iOS模块式开发]例讲如何制作一个CocoaPods私有库

参考资料:http://www.jianshu.com/p/9e2c7a7c1211

 

私有库A与私有库B之间的依赖处理:
例如私有库B依赖于私有库A,在私有库B本地验证时:

pod lib lint

会报错,提示找不到私有库A。
执行命令为:

pod lib lint --sources='http://[privateLibName]/cocoaspecs.git,https://github.com/CocoaPods/Specs.git'

关键词:库、模块式开发、CocoaPods、私有库、
工具:终端、git、CocoaPods
步骤总结:
1、打开终端,进入要建立私有库项目工程的路径,并执行pod库工程创建命令行
2、进入我们的Example项目工程,执行安装CocoaPods项目命令行
3、添加库源码文件
4、书写CocoaPods配置文件LATAlert.podspec(建议使用Atom + cocoa-pods-ruby-snippets插件来编辑该文件)
5、创建私有库git地址,并完善.podspec配置文件
6、再次进入我们的Example文件,执行pod更新指令,安装本地库源码
7、添加样例代码,运行样例测试
8、本地pod配置文件验证
9、项目工程发布tag 0.0.1
10、私有库发布
11、验证发布的私有库
涉及到的命令行:
Git:

Later@localhost:~/Desktop/私有库创建教程/LATAlert$ git remote add origin https://git.oschina.net/KKLater/LATAlert.git
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ git add .
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ git commit -a -m "0.0.1"
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ git pull origin master
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ git push origin master
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ git tag 0.0.1
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ git push origin 0.0.1

Pod:

Later@localhost:~/Desktop/私有库创建教程$ pod lib create LATAlert
Later@localhost:~/Desktop/私有库创建教程/LATAlert/Example$ pod install --no-repo-update
Later@localhost:~/Desktop/私有库创建教程/LATAlert/Example$ pod update --no-repo-update
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ pod repo add LATSpecs https://git.oschina.net/KKLater/LATPodspecs.git
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ pod lib lint
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ pod lib lint --verbose
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ pod repo push LATSpecs LATAlert.podspec 
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ pod repo push LATSpecs LATAlert.podspec --verbose

//忽略警告

Later@localhost:~/Desktop/私有库创建教程/LATAlert$ pod lib lint --allow-warnings
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ pod repo push LATSpecs LATAlert.podspec --allow-warnings
Later@localhost:~$ pod repo remove LATSpecs

下面我们通过一个例子来具体讲解整个步骤流程。
例子里面,我们创建一个私有库LATAlert,导入LATAlert私有库的项目,可以调用私有库的方法来执行弹出一个Alert显示信息!

1、打开终端,进入要建立私有库项目工程的路径,并执行pod库工程创建命令行

先创建个文件夹来放置  这个例子的文件夹名字:私有库创建教程
Later@localhost:~$ cd /Users/Later/Desktop/私有库创建教程 
Later@localhost:~/Desktop/私有库创建教程$ pod lib create LATAlert


注意:cocoapods要升级到最新版本。  我之前电脑是旧版本,是不会出现下面的界面的。  至于旧版本怎么设置下面这些属性个人没研究,有兴趣的可以自己试试。

终端得到以下界面:
这里会询问几个问题(答案根据实际情况设置),分别是:
1、语言选择
—— 教程选择Objc,如果要做Swift私有库,请选择输入Swift
2、是不是需要一个demo项目工程
​ —— 教程选择Yes,需要创建一个demo工程,建议创建一个demo工程
3、测试框架使用哪一个
​ —— 教程选择None
4、是不是需要做基本的测试
​ —— 教程选择Yes
5、类前缀是什么
​ —— 教程输入LAT


PodTest安装pod

如果出现上面的界面,说明,私有库工程创建成功了。

2、进入我们的Example项目工程,执行安装CocoaPods项目命令行

Later@localhost:~$ cd /Users/Later/Desktop/私有库创建教程/LATAlert/Example 
Later@localhost:~/Desktop/私有库创建教程/LATAlert/Example$ pod install --no-repo-update

终端界面如下:


私有库Example安装pod项目.png


双击LATAlert.xcworkspace运行项目Example工程,验证工程的正确性。

3、添加库源码文件

将源码文件复制到文件夹路径:LATAlert/LATAlert/Classes下。

资源文件放到Assets下。

注:1.如果还没有源码,则可以在Example下直接创建源码文件,实际测试通过后,再按照下面的方式来添加源码文件。2.方便起见这里直接将之前使用的源码文件拷贝过来,其他根据实际情况,看官自行添加。3.文件路径并不是绝对的,但是建议按指定路径存放,便于整体的后期维护更新


添加源码文件.png

4、书写CocoaPods配置文件LATAlert.podspec(建议使用Atom + cocoa-pods-ruby-snippets插件来编辑该文件)

双击打开LATAlert.podspec文件,此处我设置了默认Atom打开,看官自行配置,但是要注意编辑内容一定要教程一样。


podspec文件编辑1.png

我们可以把没有用的注释删掉。具体常用的配置如下:

#
# Be sure to run `pod lib lint LATAlert.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             = 'LATAlert'
  #库版本
  s.version          = '0.0.1'
  #库简短描述
  s.summary          = 'A short description of LATAlert.'
  #库详细描述
  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC
  #库介绍主页地址
  s.homepage         = 'https://github.com/<GITHUB_USERNAME>/LATAlert'
  #库开源许可
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  #作者信息
  s.author           = { 'Later' => 'lshxin89@126.com' }
  #源码git地址
  s.source           = { :git => 'https://github.com/<GITHUB_USERNAME>/LATAlert.git', :tag => s.version.to_s }
  #库依赖系统版本
  s.ios.deployment_target = '8.0'
  #源码文件配置
  s.source_files = 'LATAlert/Classes/**/*'
  #资源文件配置
  s.resource_bundles = {
    'LATAlert' => ['LATAlert/Assets/*.png']
  }
  #源码头文件配置
  s.public_header_files = 'Pod/Classes/**/*.h'
  #系统框架依赖
  s.frameworks = 'UIKit', 'MapKit'
  #第三方框架依赖
  s.dependency 'AFNetworking', '~> 2.3'
end

清理掉不需要的配置项之后,整体如下:

Pod::Spec.new do |s|
  #库名
  s.name             = 'LATAlert'
  #库版本
  s.version          = '0.0.1'
  #库简短描述
  s.summary          = 'A short description of LATAlert.'
  #库详细描述
  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC
  #库介绍主页地址
  s.homepage         = 'https://github.com/<GITHUB_USERNAME>/LATAlert'
  #库开源许可
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  #作者信息
  s.author           = { 'Later' => 'lshxin89@126.com' }
  #源码git地址
  s.source           = { :git => 'https://github.com/<GITHUB_USERNAME>/LATAlert.git', :tag => s.version.to_s }
  #库依赖系统版本
  s.ios.deployment_target = '8.0'
  #源码文件配置
  s.source_files = 'LATAlert/Classes/**/*'
  #源码头文件配置
  s.public_header_files = 'LATAlert/Classes/*.h'
  #系统框架依赖
  s.frameworks = 'UIKit'
end

细心的看官注意到,这里我们使用的homepage和source并不完整下面就需要我们创建一个git私有库,用于项目工程的存放,获取我们所需要的地址。

5、创建私有库git地址,并完善.podspec配置文件

 创建私有库地址这里按照实际情况选择了。   正常情况公司都有代码管理的地方。拿我公司举例是用gitlab管理代码的。
在gitlab上创建个仓库,设置为私有的。 拿到地址就行了。
 
下面的是 之前文章里的原文内容:

点击创建,我们的私有库就创建完成了,复制我们的私有库地址,也就是我们的.podspec文件里面source所需要的地址了。


复制私有库地址.png

source有了,但是我们的homepage怎么办呢?如果有可以访问的高大上的介绍网页页面,建议使用其网址。

至此,我们的配置文件就结束了。此时细心的看客又说了,我们创建了git私有库,但是还没有添加文件啊?

6、再次进入我们的Example文件,执行pod更新指令,安装本地库源码

Later@localhost:~/Desktop/私有库创建教程/LATAlert/Example$ pod update --no-repo-update

终端截图如下:


私有库Example更新pod项目.png

此时进入我们的Example文件夹,双击LATAlert.xcworkspace打开看看:


源码文件导入后工程项目路径.png

我们的源码已经在里面了。

这时,可以填写我们的样例代码来进行测试喽。

7、添加样例代码,运行样例测试

找到我们的样例文件LATViewController.m,引入头文件LATAlert.h,填写代码,点击屏幕时执行弹出Alert提示框,具体代码如下:

//
//  LATViewController.m
//  LATAlert
//
//  Created by Later on 10/14/2016.
//  Copyright (c) 2016 Later. All rights reserved.
//

#import "LATViewController.h"
#import "LATAlert.h"

@interface LATViewController ()

@end

@implementation LATViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [LATAlert showAlertWithTitle:@"测试" message:@"这时测试信息" OkBlock:^{
        NSLog(@"点击了确认");
    }];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

运行项目测试:


弹出Alert.png

alert信息弹出了,说明,我们的本地运行例程是正常的。下面就需要我们把项目发布到git,并添加到pod了。

8、本地pod配置文件验证

为了保证项目的正确性,尤其是pod配置文件的正确性,在正式提交前,我们需要执行以下本地验证。在本地验证正常的情况下,再上传发布还是比较稳妥的。

终端进入我们的项目文件路径:

Later@localhost:~/Desktop/私有库创建教程/LATAlert/Example$ cd /Users/Later/Desktop/私有库创建教程/LATAlert

如果现在终端正处于Example文件路径下,也可以执行

Later@localhost:~/Desktop/私有库创建教程/LATAlert/Example$ cd ..

退回到上层文件夹,也就是我们的项目文件路径了。

执行pod本地验证指令:

Later@localhost:~/Desktop/私有库创建教程/LATAlert$ pod lib lint
或者忽略警告的指令:
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ pod lib lint  --allow--warning   
 

界面显示:


项目本地验证错误1.png

如果经验丰富,大概一看便知这里的警告是告诉我们这个summary简介不是很有内涵。此处我们改一下我们的podspec文件内部的简介和描述,消除下警告。修改完的podspec文件如下。

#
# Be sure to run `pod lib lint LATAlert.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             = 'LATAlert'
  #库版本
  s.version          = '0.0.1'
  #库简短描述
  s.summary          = 'LATAlert pod Use.'
  #库详细描述
  s.description      = <<-DESC
TODO: LATAlert pod Use.
                       DESC
  #库介绍主页地址
  s.homepage         = 'http://mobile.fblife.com'
  #库开源许可
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  #作者信息
  s.author           = { 'Later' => 'lshxin89@126.com' }
  #源码git地址
  s.source           = { :git => 'https://git.oschina.net/KKLater/LATAlert.git', :tag => s.version.to_s }
  #库依赖系统版本
  s.ios.deployment_target = '8.0'
  #源码文件配置
  s.source_files = 'LATAlert/Classes/**/*'
  #源码头文件配置
  s.public_header_files = 'LATAlert/Classes/*.h'
  #系统框架依赖
  s.frameworks = 'UIKit'
end

鉴于是初学者,还是建议掌握下查看具体原因的指令比较靠谱。具体指令也很简单,在执行本地验证的指令后面添加上--verbose在执行下就可以了。

Later@localhost:~/Desktop/私有库创建教程/LATAlert$ pod lib lint --verbose

我们再次执行下验证,查看结果,BUILD SUCCEEDD,本地验证成功:


podspec本地验证成功.png


至此,我们的源码已经导入、样例工程已经验证执行、podspec配置文件本地已经验证,那么我们是不是就可以直接在pod里面使用了呢?答案是肯定的,还是不行!目前只是处于本地状态,并没有发布,所以还是不能使用的。

9、项目工程发布tag 0.0.1

在项目工程文件下执行git相关指令,并添加tag0.0.1,发布到git。

Later@localhost:~/Desktop/私有库创建教程/LATAlert$ git remote add origin https://git.oschina.net/KKLater/LATAlert.git
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ git add .
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ git commit -a -m "0.0.1"
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ git pull origin master
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ git push origin master
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ git tag 0.0.1
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ git push origin 0.0.1

相关指令执行结束后,此时我们再去看我们的git项目:


git上传之后界面.png

10、私有库发布

对于开源框架,podspec文件实在cocoapod的开源管理spec项目上面的,见下图:


cocoapod开源库管理spec.png


在这里的Specs文件加内,我们能看到我们熟悉的 AFNetworking


AFNetworking.png


但是我们创建的是私有库,所以我们需要创建自己的Specs管理库。

例如我们要创建的是LATSpecs,首先需要再创建一个git私有库,用于管理我们的LATSpecs。

创建之后获取到git地址:https://git.oschina.net/KKLater/LATPodspecs.git

在终端执行Specs创建指令:

Later@localhost:~/Desktop/私有库创建教程/LATAlert$ pod repo add LATSpecs https://git.oschina.net/KKLater/LATPodspecs.git

执行之后的结果是:


添加私有的Specs.png

看到这个,我们可以愉快的发布了。执行发布命令,直接发布好了。


podspec发布.png
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ pod repo push LATSpecs LATAlert.podspec
如果有问题 可以试试忽略警告
Later@localhost:~/Desktop/私有库创建教程/LATAlert$ pod repo push LATSpecs LATAlert.podspec  --allow--warnings
 

发布成功之后,我们来看一下我们的LATSpecs的git项目:


podspec发布之后的git.png

查看我们本地的Specs库:

直接Findle ->右键 -> 前往文件夹 -> 输入:~/.cocoapods/repos ->点击前往


查找本地的Specs库.png

本地Specs库.png

至此,我们的私有库创建发布结束。但是我们注意到,LATSpecs的README.md文件是空的,为了后期的维护更新,以及团队小伙伴的使用方便,建议完备一下信息。

完备之后的信息如下:


Readme完善.png


终端执行一下进入我们的私有库管理Specs,git更新提交下:

Later@localhost:~$ cd /Users/Later/.cocoapods/repos/LATSpecs 
Later@localhost:~/.cocoapods/repos/LATSpecs$ git add .
Later@localhost:~/.cocoapods/repos/LATSpecs$ git commit -a -m "Add LATAlert"
Later@localhost:~/.cocoapods/repos/LATSpecs$ git pull origin master
Later@localhost:~/.cocoapods/repos/LATSpecs$ git push origin master

命令行执行结束之后,我们再看一下我们的git:


addReadme之后的git.png

有没有高大上呢?

既然已经提交发布,那下面我们新建一个CocoaPods管理的项目,来验证一下我们的库吧。

11、验证发布的私有库

新建PodTest项目,并创建Podfile文件,安装CocoaPods工程。

Podfile文件内的代码如下:

platform :ios,'8.0'
target 'PodTest' do
pod 'LATAlert',:git => 'https://git.oschina.net/KKLater/LATAlert.git'
end

注意:podfile文件里要加上

source 'https://github.com/CocoaPods/Specs.git'

source 'http://112.124.41.46/tuchaoprivatecommon/xgtcprivatepodsrepo.git'

配置

如果你不配置系统的索引库 会找不到其他的库 只能找到你自己创建的仓库
最近一个命令 pod install
Nice 完成了

 

终端进入我们的测试工程根目录,并执行pod安装指令:

Later@localhost:~$ cd /Users/Later/Desktop/私有库创建教程/PodTest 
Later@localhost:~/Desktop/私有库创建教程/PodTest$ pod install --no-repo-update

终端界面如下:


PodTest安装pod.png

我们重新打开测试项目:


测试项目成功导入了项目.png

在ViewController.m文件导入文件,并按照Example的样例书写代码验证。代码如下:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [LATAlert showAlertWithTitle:@"测试" message:@"这时测试信息" OkBlock:^{
        NSLog(@"点击了确认");
    }];
}

不同的是,这里引入头文件时,我们使用的是:#import <LATAlert/LATAlert.h>而不是Example样例工程里面的引入方式:#import "LATAlert.h"。

运行PodTest项目,点击屏幕,是不是也弹出Alert提示了呢?

至此,我们的私有库发布结束。具体更详细的信息,建议各位看客多多参考CocoaPods官网,会有更不一样的体验。英语不好的,体验会更加深刻哟!

 

 

至于更新版本的话

localhost:xgoalibrary kfj$ git add .     //新增的文件 加入仓库 

localhost:xgoalibrary kfj$ git commit -a -m "0.1.2"      "0.1.2" -- tag 号 //  需要提交的文件上传确认

localhost:xgoalibrary kfj$ git pull origin master         //下啦最新资源

localhost:xgoalibrary kfj$ git push origin master         //上传本地最新代码

localhost:xgoalibrary kfj$ git tag 0.1.2      //打tag

localhost:xgoalibrary kfj$ git push --tags   

 

localhost:xgoalibrary kfj$ pod lib lint --allow-warnings     //验证本地库是否正确    lib  换成 spec 的 话 就是 就是验证远程服务器的 库 是否正确

localhost:xgoalibrary kfj$ pod repo push XGOALibrary XGOALibrary.podspec --allow-warnings

 

命令行指令:

history    查看自己用过的历史指令

pod repo list   查看本地repo私有库资源信息

open ~/.cocoapods   打开本地私有库资源的位置

 

 

 

 

 

 

 

 

posted on 2017-06-28 14:27  yuzx  阅读(2940)  评论(0编辑  收藏  举报

导航