pod init

1、创建私有的Spec Repo

Spec Repo 是所有公开的Pods 的podspec文件的一个git仓库,当使用Cocoapods后它会被clone到本地的~/.cocoapods/repos目录下,可以进入到这个目录看到master文件夹就是这个官方的Spec Repo了。因此我们需要创建一个类似于master的私有Spec Repo 。同理这个私有Spec Repo我们也要有一个远程端。那么我们需要创建一个 Git仓库,这个仓库你可以创建私有的也可以创建公开的。如果是私有的话,项目中其他同事,你要给他这个Git仓库的权限。

在github上创建一个mySpecs仓库

在终端执行命令:

$pod repo add mySpecs https://github.com/xxx/mySpecs.git

之后在~/.cocoapods/repos目录下就能看到mySpecs

2、创建Pods工程

在本地创建一个test目录
cd到test目录并执行终端命令

$pod lib create pod_test

终端会显示

Cloning `https://github.com/CocoaPods/pod-template.git` into `pod_test`.
Configuring pod_test template.

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

2018-03-01 15:35:21.732 defaults[11084:244747] 
The domain/default pair of (org.cocoapods.pod-template, HasRunbefore) does not exist
If this is your first time we recommend running through with the guide: 
 - http://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and double click links to open in a browser. )

 Press return to continue.

依次填写iOS、ObjC、Yes、Specta、Yes、JkW

What platform do you want to use?? [ iOS / macOS ]
 > iOS

What language do you want to use?? [ Swift / ObjC ]
 >ObjC 

Would you like to include a demo application with your library? [ Yes / No ]
 > Yes

Which testing frameworks will you use? [ Specta / Kiwi / None ]
 > Specta

Would you like to do view based testing? [ Yes / No ]
 > Yes

What is your class prefix?
 > JKW
 

设置完成后打印台会输出

Running pod install on your new library.

Analyzing dependencies
Fetching podspec for `pod_test` from `../`
Downloading dependencies
Installing Expecta (1.0.6)
Installing Expecta+Snapshots (3.1.1)
Installing FBSnapshotTestCase (2.1.4)
Installing Specta (1.0.7)
Installing pod_test (0.1.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `pod_test.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 5 dependencies from the Podfile and 5 total pods installed.

[!] Automatically assigning platform `ios` with version `9.3` on target `pod_test_Example` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'pod_test/Example/pod_test.xcworkspace'

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.

成功后会在test目录下创建一个pod_test工程

目录如下:

 
 

我们来添加一个分类文件

 
 

NSString+Test分类文件如下

#import <Foundation/Foundation.h>

@interface NSString (Test)
- (void)test;
@end
#import "NSString+Test.h"

@implementation NSString (Test)
- (void)test{
    NSLog(@"只是一个测试");
}
@end

cd 在Example工程目录下执行 pod update命令

打开项目工程,可以看到库文件都被加载到Pods子项目中了
不过它们并没有在Pods目录下,而是跟测试项目一样存在于Development Pods/MyLib中,这是因为我们是在本地测试,而没有把podspec文件添加到Spec Repo中的缘故。

注:这里需要注意的是每当添加了新的文件或者以后更新了podspec的版本都需要重新执行一遍pod update命令。

4、提交pods库到github上

cd 到pod_test目录下

$git init
$git add .
$ git commit -am "第一次提交" 
$ git remote add origin https://github.com/xxx/pod_test
$ git push origin master 
//一定要有标签,不然会有下面的警告
//podspec文件中获取Git版本控制的项目需要tag号,
$ git tag -m "first release" "0.1.0" 
$ git push --tags 

5、配置pod_test的podspec文件

该pod_test.podspec文件在执行$pod lib create pod_test已经被自动创建

podspec 文件格式

Pod::Spec.new do |s|
  s.name             = 'pod_test'
  s.version          = '0.1.0'
  s.summary          = '这是我第一个私有库项目demo'

  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://github.com/kamto6/pod_test'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'kwjie5566@163.com' => 'kangwei.jie@mydeertrip.com' }
  s.source           = { :git => 'https://github.com/xxx/pod_test.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

  s.source_files = 'pod_test/Classes/**/*'
  
  # s.resource_bundles = {
  #   'pod_test' => ['pod_test/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end
  • s.name :pod search 搜索的关键词,注意这里一定要和.podspec的名称一样

  • s.version :版本号,每一个版本对应一个tag

  • s.summary : 简介

  • s.homepage : 项目主页地址

  • s.license : 许可证

  • s.author : 作者

  • s.social_media_url : 社交网址

  • s.source : 项目的地址

  • s.source_files : 需要包含的源文件

  • s.resources: 资源文件

  • s.requires_arc : 是否支持ARC

  • s.dependency :依赖库

  • s.ios.deployment_target = '8.0' : 支持的pod最低版本

如:

s.source_files = 'pod_test/Classes/**/*'

“*” 表示匹配所有文件
“**” 表示匹配所有子目录

验证该podspec文件,cd到pod_test目录

$ pod spec lint

pod spec相对于pod lib会更为精确,pod lib相当于只验证一个本地仓库,pod spec会同时验证本地仓库和远程仓库。

打印台提示:

 -> pod_test (0.1.0)
    - WARN  | summary: The summary is not meaningful.
    - WARN  | url: The URL (https://github.com/kwjie5566@163.com/pod_test) is not reachable.

[!] pod_test did not pass validation, due to 2 warnings (but you can use `--allow-warnings` to ignore them).
You can use the `--no-clean` option to inspect any issue.

当然我们验证时可以忽略这些警告

$ pod spec lint --allow-warnings

--allow-warnings是允许warning的存在,也就是说当你在pod spec lint验证podspec的时候,如果不加这句,而你的代码里又一些警告的话,是验证不通过的。而加上这句话的话,有警告也能验证通过。

终端输入:vim pod_test.podspec

按i进入编辑状态修改:summary (随便填写即可) 和 url(post_test对应的仓库) ,再按esc + : + wq 退出
终端再执行$ pod spec lint

打印台提示:

 -> pod_test (0.1.0)

pod_test passed validation.

表示这个podspec文件验证通过,否则修改到验证通过为止

6、在Example工程修改podfile文件
#pod 'pod_test', :path => '../'
pod 'pod_test',:podspec => '../pod_test.podspec'

再执行pod update

有时候当你使用pod update时会发现特别慢,那是因为pod会默认先更新一次podspec索引。使用--no-repo-update参数可以禁止其做索引更新操作。

如:pod update --no-repo-update

$cd Example/
$ pod update
Installing pod_test (0.1.0)

[!] Error installing pod_test
[!] /usr/bin/git clone https://github.com/kamto6/pod_test.git /var/folders/7k/1bb1m_fx0xs3r8pzl28mykz00000gn/T/d20180301-13092-1rajecj --template= --single-branch --depth 1 --branch 0.1.0

Cloning into '/var/folders/7k/1bb1m_fx0xs3r8pzl28mykz00000gn/T/d20180301-13092-1rajecj'...
warning: Could not find remote branch 0.1.0 to clone.
fatal: Remote branch 0.1.0 not found in upstream origin


[!] Automatically assigning platform `ios` with version `9.3` on target `pod_test_Example` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

cd 到pod_test目录

$ git tag -m "first release" "0.1.0"
$ git push --tags

然后再执行

$cd Example/
$ pod update

再打开Example工程,发现库文件都被加载到Pods项目下

6、把pod_test.podspec提交到Spec Repo仓库
pod repo push [本地Spec Repo名称][podspec文件路径]
$ pod repo push mySpecs pod_test.podspec  
Validating spec
 -> pod_test (0.1.0)

Updating the `mySpecs' repo

Your configuration specifies to merge with the ref 'refs/heads/master'
from the remote, but no such ref was fetched.

Adding the spec to the `mySpecs' repo

 - [Add] pod_test (0.1.0)

Pushing the `mySpecs' repo

表示这个库已经提交到Spec Repo上了

 
 

在对应的spec仓库地址也能看到提交记录。

这里说的添加到私有的Spec Repo,如果要添加到CocoaPods的官方库,可以用到trunk工具。

使用终端 pod search pod_test

注意:这里搜索repos目录,如果缓存里没有该spec文件,则不会搜索到

-> pod_test (0.1.0)
   私有库项目demo
   pod 'pod_test', '~> 0.1.0'
   - Homepage: https://github.com/xxx/pod_test
   - Source:   https://github.com/xxx/pod_test.git
   - Versions: 0.1.0 [mySpecs repo]

此时到~/.cocoapods/repos/mySpecs

如图:

查看本地repo信息

$ pod repo

接下来我们再新建一个工程来测试一下

新建一个工程,在项目的podfile里添加

#私有spec仓库的地址,而不是某个pod仓库的地址
source 'https://github.com/xxx/mySpecs'
pod 'pod_test'

注意:如果该仓库没有权限,则会失败

到此,算是完成pods私有库的搭建了。

注:如果添加到CocoaPods官方库上,别人都可以search或者install

cd 到pod_test目录,执行

pod trunk push pod_test.podspec

红色警告:

[!] You need to register a session first.
$ pod trunk register xxx@163.com 'xxx' --description='pro'

再执行

$pod trunk push pod_test.podspec

如果提示:

[!] You (xxx@163.com) are not allowed to push new versions for this pod. The owners of this pod are aaaa@126.com.

呵呵。大致的意思是repo 已经被这个所有者占有了

$pod trunk info pod_test
 
终端打印:

pod_test
    - Versions:
      - 0.0.2 (2017-01-13 07:03:27 UTC)
    - Owners:
      - aaa <aaa@126.com>
 

如果没有问题,那么发布到Cocoapods后,在终端更新本地pods仓库信息

$ pod setup

然后查询

$ pod search pod_test



作者:kamto
链接:https://www.jianshu.com/p/006d6ab89c3c
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
posted @ 2019-01-18 16:00  FakeCoder  阅读(1346)  评论(0编辑  收藏  举报