CocoaPods创建私有库

iam小码哥| 阅读:1309 发表时间:2018-03-01 19:49:17 ios
摘要:最近在架构公司项目,运用组件化开发,我把我如何创建私有Spec Repo 和私有组件分享给大家。后续还会更新组件化开发的更多内容,敬请期待。
一、创建私有的Spec Repo
Spec Repo 是所有的Pods的一个索引,是所有公开的Pods 的podspec 文件的一个仓库,其实就是一个部署在服务器的Git仓库,当你使用CocoaPods 后它会被Clone到本地.
1、首先在coding.net上创建一个MyRemoteSpecs项目.(GitHub 创建私有需要收费,选择的是coding.net)。创建好后,在终端cd到你想创建的目录下然后输入命令:
# pod repo add [私有库名字] [私有库clone链接]
eg: pod repo add MyRemoteSpecs https://git.coding.net/iamXK/MyRemoteSpecs.git
这个时候你本地~/.cocoapods/repos目录下面就会多出一个 MyRemoteSpecs 的文件目录。
至此,第一步创建私有Spec Repo就完成了。

二、创建Pod项目工程

1.创建Pod项目工程
首先,在coding.net上创建一个MyRemoteLib项目。然后,使用Cocoapods提供的一个Using Pod Lib Create 工具创建一个工程。终端输入命令
pod lib create MyRemoteLib
控制台会输出
What language do you want to use?? [ Swift / ObjC ]开发语言选择 (ObjC)
Would you like to include a demo application with your library?是否需要测试demo (Yes)
Which testing frameworks will you use?是否选择一个测试框架 (None)
Would you like to do view based testing?是否基于View测试 (No)
What is your class prefix?类的前缀 (XK)
成功之后会创建一个MyRemoteLib的工程。

2、添加实现代码

在你的目录下找到MyRemoteLib文件,里面有两个文件夹 Assets 和 Classes,然后把Classes里面的replace.m删除。 添加你自己的代码。

3.开发模式下测试pod库的代码

在Example工程目录下执行 pod update命令安装依赖。 打开项目工程,可以看到库文件都被加载到Pods子项目中了。

4.提交Pod库到MyRemoteSpecs

终端 cd进入MyRemoteLib项目根目录,然后执行命令
$ git add .
$ git commit -s -m "初始化MyRemoteLib 库"
$ git remote add origin git@git.coding.net:iamXK/MyRemoteLib.git #添加远端仓库
$ git push origin master #提交到远端仓库
$ git tag -m " release version" "0.1.0" #打上标签,这个很重要
$ git push --tags #推送tag到远端仓库

到这里,成功提交到远程MyRemoteSpecs仓库。 MyRemoteLib库就初步完成了代码实现。

三、创建并提交MyRemoteLib Pod库的podspec文件到私有Spec Repo仓库

1.配置MyRemoteLib Pod库的podspec 文件.我的配置如下

#
# Be sure to run `pod lib lint MyLib.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 = 'MyRemoteLib'
s.version = '0.1.0'
s.summary = '这个是我的私有库项目Demo.'

# 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
这个是私有库项目Demo.
DESC

s.homepage = 'https://coding.net/u/iamXK'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'king' => '393264532@qq.com' }
s.source = { :git => 'https://git.coding.net/iamXK/MyLib.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

s.ios.deployment_target = '8.0'

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

# s.public_header_files = 'Pod/Classes/**/*.h'
s.frameworks = 'UIKit'
# s.dependency 'AFNetworking', '~> 2.3'
end

2.编辑完MyRemoteLib.podspec文件后,需要验证一下这个MyRemoteLib.podspec文件是否可用

终端 cd进入MyRemoteLib项目根目录,然后执行命令
pod lib lint
当你看到 Terminal 中输出:
-> MyRemoteLib (0.1.0)
MyRemoteLib passed validation.
表示这个MyRemoteLib.podspec 验证通过,是一个符合CocoaPods规则的配置文件。

3.本地测试MyLib.podspec文件

打开Example工程目录Podfile文件修改下pod 的引用

pod 'MyRemoteLib', :podspec => '../MyRemoteLib.podspec' # 指定podspec文件

然后在Example工程目录下执行pod update命令安装依赖,打开项目工程,现在可以看到库文件都被加载到Pods子项目中了。

4.向Spec Repo提交podspec

测试库文件没有问题我们就把MyRemoteLib.podspec提交到远程Spec Repo仓库中,就是本文开头说的MyRemoteSpecs私有库
在Terminal中执行 cd进入MyRemoteLib项目根目录然后,执行以下命令:

# pod repo push [Repo名] [podspec 文件名字]
$ pod repo push MyRemoteSpecs MyRemoteLib.podspec。

这个时候组件库就添加到我们的私有Spec Repo中了,可以进入到~/.cocoapods/repos/MyRemoteSpecs目录下查看。
我们的Spec Repo远端仓库 MyRemoteSpecs,也有了一次提交,这个podspec也已经被Push上去了。

四、使用制作好的Pod

在你自己的项目的Podfile里添加私有Spec Repo

source 'https://git.coding.net/iamXK/MySpecs.git'

platform :ios, '8.0'

use_frameworks!

target 'XKTest' do

pod 'MyRemoteLib'

end

然后执行pod update,更新库依赖,然后打开项目可以看到你自己的私有组件了。

各位老板,喜欢就点个赞。
本文为iam小码哥原创,转载请注明出处!
如果您觉得好,可以打赏作者:
如果您觉得累了,是否想看点美女养养眼:猛戳>>朋友帮
如果您觉得皮了,是否想来点神吐槽:猛戳>>iPhone查询中

已有0条评论

昵称:
邮箱:

  • 最新评论

iPhone查询中 - bbs.ipcxz.com 朋友帮 - www.pengyb.cn iPhone查询中 - bbs.ipcxz.com
反馈
微信订阅号