创建私有Spec Repo
先建立一个私有git仓库,用来存放podspec。我是建在码云上。
然后在终端执行命令:
$ pod repo add FCPrivateRepo https://gitee.com/sergeant/fcprivate-repo.git
执行成功后,可以在~/.cocoapods/repos目录下看到FCPrivateRepo文件夹。
也可以在终端执行命令查看所有的repo:
$ pod repo
FCPrivateRepo
- Type: git (master)
- URL: https://gitee.com/sergeant/fcprivate-repo.git
- Path: /Users/zhijiazhong/.cocoapods/repos/FCPrivateRepo
master
- Type: git (master)
- URL: https://github.com/CocoaPods/Specs.git
- Path: /Users/zhijiazhong/.cocoapods/repos/master
trunk
- Type: CDN
- URL: https://cdn.cocoapods.org/
- Path: /Users/zhijiazhong/.cocoapods/repos/trunk
3 repos
创建私有组件
我下面新建一个项目FCPrivateComponentA:
$ pod lib create FCPrivateComponentA
执行过程中会问以下问题,按需回答即可:
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 ]
> none
Would you like to do view based testing? [ Yes / No ]
> no
What is your class prefix?
> FC
完成之后就可以添加组件代码了,加在FCPrivateComponentA/Classes目录下。
然后我们需要再创建一个私有仓库存放这个组件:
https://gitee.com/sergeant/fcprivate-component-a.git
之后我们就可以编辑podspec文件了。
至少需要修改以下4个字段:
- s.summary # 简介
- s.description # 详细描述,要比简介长
- s.homepage # 主页
- s.source # 仓库地址
编辑完如下:
#
# Be sure to run `pod lib lint FCPrivateComponentA.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 https://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'FCPrivateComponentA'
s.version = '0.1.0'
s.summary = 'A private pod for test.'
# 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
A private pod which show a view controller with red background.
DESC
s.homepage = 'https://gitee.com/sergeant/fcprivate-component-a'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'Finger Chung' => '[email protected]' }
s.source = { :git => 'https://gitee.com/sergeant/fcprivate-component-a.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/'
s.ios.deployment_target = '8.0'
s.source_files = 'FCPrivateComponentB/Classes/**/*'
# s.resource_bundles = {
# 'FCPrivateComponentB' => ['FCPrivateComponentB/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
然后我们就可以提交代码了。先在.gitignore文件中忽略Pod/文件夹和.xcworkspace文件。
然后执行命令:
$ git add .
$ git commit -s -m "Initial Commit of Library"
$ git remote add origin https://gitee.com/sergeant/fcprivate-component-a.git #添加远端仓库
$ git push origin master #提交到远端仓库
因为podspec文件中获取Git版本控制的项目还需要tag号,所以我们要打上一个tag:
$ git tag -m "first release" 0.1.0
$ git push --tags #推送tag到远端仓库
做完上面的工作,私有组件就做好了,但是我们要验证一下是否可用。使用命令:
pod lib lint
执行后可能出现警告导致验证不通过:
$ pod lib lint
-> FCPrivateComponentA (0.1.0)
- WARN | url: The URL (https://gitee.com/sergeant/fcprivate-component-a) is not reachable.
如果跟我一样提示主页不可访问,可能是因为我们使用的是私有库,直接忽略就好:
$ pod lib lint --allow-warnings
-> FCPrivateComponentA (0.1.0)
- WARN | url: The URL (https://gitee.com/sergeant/fcprivate-component-a) is not reachable.
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
- NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'FCPrivateComponentA' from project 'Pods')
- NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
- NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')
FCPrivateComponentA passed validation.
这样就验证通过了。
如果是其他警告,则需要一一修改。一般都是podspec文件不规范导致的。
比如我一开始没有修改s.summary,第二次s.description比s.summary短,都被警告了。
验证通过后,就可以尝试使用了。
本地测试
先在本地测试下组件是否可用。
新建一个工程。cd到工程根目录,然后使用命令 pod init 生成Podfile。
使用本地路径导入FCPrivateComponentA:
pod 'FCPrivateComponentA', :path => '../FCPrivateComponentA'
path 既可以使用相对路径,也可以使用绝对路径。
还可以用podsepc文件的路径:
pod 'FCPrivateComponentA', :podspec => '../FCPrivateComponentA/FCPrivateComponentA.podspec'
然后执行 pod install 安装。
$ pod install
Analyzing dependencies
Fetching podspec for `FCPrivateComponentA` from `../FCPrivateComponentA/FCPrivateComponentA.podspec`
Downloading dependencies
Installing FCPrivateComponentA (0.1.0)
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
打开项目工程,可以看到库文件都被加载到Pods子项目中了,不过它们并没有在Pods目录下,而是跟测试项目一样存在于Development Pods/FCPrivateComponentA,这是因为我们是在本地测试,而没有把podspec文件添加到Spec Repo中的缘故。
添加到Spec Repo
现在,我们最开始创建的Spec Repo派上用场了。
只需要一个命令即可:
$ pod repo push FCPrivateRepo FCPrivateComponentA.podspec #前面是本地Repo名字 后面是podspec名字
执行完后,我们可以在~/.cocoapods/repos/FCPrivateRepo目录下看到FCPrivateComponentA。
同样,在FCPrivateRepo的远程仓库也可以看到FCPrivateComponentA.podspec被提交上去。
我们也可以搜索到这个组件:
$ pod search FCPrivateComponentA
-> FCPrivateComponentA (0.1.0)
A private pod for test.
pod 'FCPrivateComponentA', '~> 0.1.0'
- Homepage: https://gitee.com/sergeant/fcprivate-component-a
- Source: https://gitee.com/sergeant/fcprivate-component-a.git
- Versions: 0.1.0 [FCPrivateRepo repo]
到这里组件就制作完成了。
正式使用
重新打开Podfile,将本地测试时指定的本地路径去掉:
pod 'FCPrivateComponentA'
还需要在头部加上我们的私有Spec Repo地址,注意不要写成了私有组件的仓库:
source 'https://gitee.com/sergeant/fcprivate-repo.git'
完整的Podfile:
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
# 这里是私有索引库的地址
# 而不是私有组件的地址 https://gitee.com/sergeant/fcprivate-component-a.git
source 'https://gitee.com/sergeant/fcprivate-repo.git'
target 'TestPrivatePod' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for TestPrivatePod
pod 'FCPrivateComponentA'
end
执行 pod update:
$ pod update
Update all pods
Updating local specs repositories
$ /usr/bin/git -C /Users/sergeant/.cocoapods/repos/FCPrivateRepo fetch
origin --progress
$ /usr/bin/git -C /Users/sergeant/.cocoapods/repos/FCPrivateRepo rev-parse
--abbrev-ref HEAD
master
$ /usr/bin/git -C /Users/sergeant/.cocoapods/repos/FCPrivateRepo reset
--hard origin/master
HEAD is now at 4941c37 [Add] FCPrivateComponentA (0.1.0)
Analyzing dependencies
Downloading dependencies
Installing FCPrivateComponentA 0.1.0
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
如有项目组的其他人需要使用,需要给他对应私有库的权限,然后也执行命令:
$ pod repo add FCPrivateRepo https://gitee.com/sergeant/fcprivate-repo.git