0%

私有Pod制作

使用本地pod

创建Podspec文件:pod spec create XQPerson

修改XQPerson.podspec文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Pod::Spec.new do |s|
s.name = "XQPerson"
s.version = "0.0.1"
s.summary = "Use XQPerson to print hello."
s.description = <<-DESC
A pod used to print hello. this is just a simply pod.
DESC
s.homepage = "https://github.com/tonymillion/Reachability"
s.license = "MIT"
s.author = { "xq_120" => "xq_120455@yahoo.com" }
s.platform = :ios, "7.0"
s.source = { :git => "/Users/xuequan/Desktop/XQPerson/", :tag => "#{s.version}" }
s.source_files = "XQPerson/*.{h,m}"

s.framework = "Foundation"
# s.frameworks = "SomeFramework", "AnotherFramework"

# s.library = "iconv"
# s.libraries = "iconv", "xml2"

s.requires_arc = true

# s.dependency "JSONKit", "~> 1.4"

end

验证Spec文件:pod lib lint:

1
2
3
 -> XQPerson (0.0.1)

XQPerson passed validation.

准备好你的pod文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
xuequandeMacBook-Pro:XQPerson xuequan$ tree /Users/xuequan/Desktop/XQPerson -L 2
/Users/xuequan/Desktop/XQPerson
├── LICENSE
├── XQPerson
│   ├── XQPerson.h
│   └── XQPerson.m
├── XQPerson.podspec
└── XQPersonDemo
├── Podfile
├── Podfile.lock
├── Pods
├── XQPersonDemo
├── XQPersonDemo.xcodeproj
├── XQPersonDemo.xcworkspace
├── XQPersonDemoTests
└── XQPersonDemoUITests

打好tag.

使用本地pods时,Podfile有两种写法:

  1. pod ‘XQPerson’, :podspec => ‘~/Desktop/XQPerson/XQPerson.podspec’ # 指定包含.podspec文件的路径
  2. pod ‘XQPerson’, :path => ‘~/Desktop/XQPerson’ # 指定podspec文件的路径

不过如果
s.source = { :git => "~/Desktop/XQPerson", :tag => "#{s.version}" }
则指定podspec文件写法,pod install时会安装失败.而指定包含.podspec文件的路径写法不会.

最后pod install:

1
2
3
4
5
6
7
Analyzing dependencies
Downloading dependencies
Using XQPerson (0.0.1)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

至此使用本地pod就完成了.

提交本地pod至远程仓库

在GitLab上创建一个私有工程XQPerson.

修改XQPerson.podspec中的:

1
2
s.homepage     = "https://gitlab.com/xq_120/XQPerson"
s.source = { :git => "https://gitlab.com/xq_120/XQPerson.git", :tag => "#{s.version}" }

使用远程地址代替之前的本地路径.
重新验证一下XQPerson.podspec文件:pod lib lint

1
2
3
4
5
xuequandeMacBook-Pro:XQPerson xuequan$ pod lib lint

-> XQPerson (0.0.1)

XQPerson passed validation.

提交文件到远程仓库:

1
2
3
4
$ git add .
$ git commit -s -m "Initial Commit of Library"
$ git remote add origin git@gitlab.com:xq_120/XQPerson.git #添加远端仓库
$ git push origin master #提交到远端仓库

打好tag:

1
2
$ git tag -m "first release" 0.0.1
$ git push --tags

创建私有Spec Repo

在GitLab上创建一个git远程仓库用于存储自己的私有podspec文件.其实这就有点类似CocoaPods的官方Spec repo.官方的地址是https://github.com/CocoaPods/Specs.git,只不过官方存储的都是公开的pod的podspec文件.而我们的git远程仓库由于是私有的,因此外部人员就无法访问到.

创建完成之后在Terminal中执行如下命令:
$ pod repo add XQSpecs https://gitlab.com/xq_120/XQSpecs.git

此时如果成功的话进入到~/.cocoapods/repos目录下就可以看到XQSpecs这个目录了。

向Spec Repo提交podspec

向我们的私有Spec Repo提交podspec只需要一个命令:
$ pod repo push XQSpecs XQPerson.podspec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
xuequandeMacBook-Pro:XQPerson xuequan$ pod repo push XQSpecs XQPerson.podspec

Validating spec
-> XQPerson (0.0.1)

Updating the `XQSpecs' repo

Already up-to-date.

Adding the spec to the `XQSpecs' repo

- [No change] XQPerson (0.0.1)

Pushing the `XQSpecs' repo

xuequandeMacBook-Pro:XQPerson xuequan$

完成之后这个组件库就添加到我们的私有Spec Repo中了,可以进入到~/.cocoapods/repos/XQSpecs目录下查看:tree . -l

1
2
3
4
.
├── XQPerson
│   └── 0.0.1
│   └── XQPerson.podspec

再去看我们的Spec Repo远端仓库,也有了一次提交,这个podspec也已经被Push上去了。

至此,我们的这个组件库就已经制作添加完成了,使用pod search命令就可以查到我们自己的库了:

1
2
3
4
5
6
7
-> XQPerson (0.0.1)
Use XQPerson to print hello.
pod 'XQPerson', '~> 0.0.1'
- Homepage: https://gitlab.com/xq_120/XQPerson
- Source: https://gitlab.com/xq_120/XQPerson.git
- Versions: 0.0.1 [XQSpecs repo]
(END)

搜索不到的,将/Users/xuequan/Library/Caches/CocoaPods路径下的search_index.json删除,然后重新搜索(可能需要先更新下Spec repo:pod repo update).

使用远程私有Pod

Podfile文件:

1
2
3
4
5
6
7
8
9
10
11
platform :ios, '9.0'

source 'https://github.com/CocoaPods/Specs.git'
source 'https://gitlab.com/xq_120/XQSpecs.git'

target 'tanwanlanyue' do

pod 'XQPerson', '~> 0.0.1'
pod 'AFNetworking', '~> 3.2.0'

end

执行pod install:

1
2
3
4
5
6
7
8
9
10
xuequandeMacBook-Pro:tanwanlanyue xuequan$ pod install
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (3.2.0)
Installing XQPerson (0.0.1)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.
xuequandeMacBook-Pro:tanwanlanyue xuequan$

使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#import "ViewController.h"
#import <XQPerson.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

XQPerson *p = [XQPerson new];
[p printHello]; //2018-03-22 11:34:17.434385+0800 tanwanlanyue[9186:96648] hello
}


@end

参考

使用Cocoapods创建私有podspec

Cocoapods代码管理

cocoapods进阶

Cocoapods使用私有库中遇到的坑

CocoaPods 私有仓库的创建(超详细)

CocoaPods创建公有和私有Pod库方法总结


Git查看、删除、重命名远程分支和tag

觉得文章有帮助可以打赏一下哦!