创建podSpec,使用pod管理第三方库

24 篇文章 0 订阅

转自:https://blog.csdn.net/weixin_44758107/article/details/127639308

提要:

  podfile文件会先读取.podspec文件,根据.podspec文件的指向来下载第三方库到项目中。

  本文先通过一、二、三这三个步骤讲解了如何建立一个"podspec文件在本地.cocoaPod库,第三方库在远程机器"的例子。

  后文中的第四项,讲解了"podspec文件在本地项目中,第三方库在远程机器"的设置方法;最后讲了"podspec文件在本地项目中,第三方库也在本地项目中"的设置方法。第五项,讲解了"podspec文件的语法知识"。

正文讲解:

一、创建需要pod管理的第三方库

(1) 本地创建第三方库起名为lvPodLibrary,用命令创建如下:

sheron_lv@MacLv:~/codeLv/github$ pod lib create lvPodLibrary//输入命令

根据提示回答四个问题,1.是否需要一个例子工程;2.选择一个测试框架;3.是否基于View测试;4.类的前缀。

加入我们要用Pod管理的类,这里直接起名为lvPodLibrary.h和lvPodLibrary.m。

(2)在github上创建New repository,即远端lvPodLibrary库。拿到地址,比如我的是https://github.com/SheronLv/lvPodLibrary.git。把上面创建的本地库push到远端:

sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ git add .

sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ git commit -m "Initial Commit of Library"

On branch master

nothing to commit, working directory clean

sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ git remote add origin https://github.com/SheronLv/lvPodLibrary.git //添加远端仓库

sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ git push origin master //提交到远端仓库

因为podspec文件中获取第三方库lvPodLibrary这个Git版本控制的项目还需要tag号,所以我们要打上一个tag

sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ git tag -m "first release" "0.1.0"

sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ git push --tags//推送tag到远端仓库

也就是说,我现在推送的lvPodLibrary第三方库是0.1.0版本的。

(3)编辑lvPodLibrary.podspec文件,或者如果这个库是通过其他方式创建的没有这个文件的话,创建lvPodLibrary.podspec文件

sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ pod spec create lvPodLibrary https://github.com/SheronLv/lvPodLibrary.git

内容如下:

Pod::Spec.new do |s|

s.name = "lvPodLibrary" #名称

s.version = "0.1.0"

s.summary = "Just Testing" #简短介绍,下面是详细介绍

s.description = <<-DESC

            Testing Testing Testing

DESC

s.homepage = "https://github.com/SheronLv/lvPodLibrary"

s.license = 'MIT'

s.author = { "Sheron lv" => "email@address.com" }

s.platform = :ios, "7.0"

s.source = { :git => "https://github.com/SheronLv/lvPodLibrary.git", :tag => s.version }

s.source_files = "lvPodLibrary", "lvPodLibrary/**/*.{h,m}"

end

检验.podspec文件是否可用可用

sheron_lv@MacLv:~/codeLv/github/lvPodLibrary$ pod lib lint

-> lvPodLibrary (0.1.0)

lvPodLibrary passed validation. // 此提示信息表示可用

然后把所有文件push到远端。

二、创建podSpec仓库

(1)在github上创建New repository,作为podSpec的远程仓库,如下图,我在github上创建了一个名为lvPodSpec的仓库。

(3)将远程podSpec仓库添加到本地仓库目录下,即~/.cocoapods/repos目录下。

sheron_lv@MacLv:~$ pod repo add lvPodSpec https://github.com/SheronLv/lvPodSpec.git//输入命令

Cloning spec repo `lvPodSpec` from `https://github.com/SheronLv/lvPodSpec.git` //执行过程、结果

此时本地~/.cocoapods/repos目录下已经有了名为lvPodSpec的仓库。通过pod search命令可以查到这个库的信息:

sheron_lv@MacLv:~/Desktop/lvDemo$ pod search lvPodLibrary

//查到信息如下

-> lvPodLibrary (0.1.0)

Just Testing

pod 'lvPodLibrary', '~> 0.1.0'

- Homepage: https://github.com/SheronLv/lvPodLibrary

- Source: https://github.com/SheronLv/lvPodLibrary.git

- Versions: 0.1.0 [lvPodSpec repo]

把之前的lvPodLibrary.podspec文件拷贝到本地的lvPodSpec项目中.

然后把所有的内容push到远程lvPodSpec下。(其实可以删掉lvPodLibrary项目下的lvPodLibrary.podspec文件,没有指向它。但现在可以先留着,后面练习直接指向第三方库的.podspec文件可以使用)

(注意:每次更新版本tag,把上级目录的名字也更新,或者建立新的文件夹比如0.1.1、0.1.2,否则无法找到这个版本的.podspec)

三、在新的项目中使用pod管理第三方库

编写这个项目的Podfile如下:

source 'https://github.com/SheronLv/lvPodSpec.git' //注:如果不添加source句有可能找不到,就会出错

platform:ios,"7.0"

inhibit_all_warnings!

target "lvDemo"do

pod 'lvPodLibrary','0.1.0'

end

运行pod install,项目中就出现了要用的lvPodLibrary项目

总结:经过多次实践,以上的三大步骤,其实是以下指向关系:

1)需要使用第三方库的项目中的Podfile指向本地的仓库中的.podspec文件

    例如 Podfile中关键代码

      pod 'lvPodLibrary','0.1.5'

(2)本地的.podspec文件指向远程仓库的第三方库的地址

  例如 本地仓库中~/.cocoapods/repos/项目lvPodSpec的lvPodLibrary.podspec文件

     s.name ="lvPodLibrary"

   s.version = “0.1.5"

s.source = { :git => "https://github.com/SheronLv/lvPodLibrary.git", :tag => s.version }

s.source_files = "lvPodLibrary"

(3)远程仓库lvPodLibrary.git

    关键是:

    push时要打tag;

    存放第三方库lvPodLibrary的目录结构与.podspec文件中指定的s.source_files一致。

我们知道,运行pod install时,运行的是本地.cocoapods下的.podspec,远程对应的.podspec和在你要管理的第三库里创建的.podspec都是不起作用的。

四、 PS: podfile不同写法

(1)

pod 'lvPodLibrary' , :git =>'https://github.com/SheronLv/lvPodLibrary.git', :tag => '0.1.2'

这样,pod会去找远程git地址上,tag是0.1.2的那次提交的代码里的lvPodLibrary.podspec文件,根据lvPodLibrary.podspec文件下载对应的第三方库。

(2)将上文中创建的lvPodLibrary.podspec文件复制到需要依赖该库代码的项目目录下,如本项目demo/spec/lvPodLibrary.podspec,然后修改Podfile中对该库的依赖为:

pod 'lvPodLibrary' , :podspec => './spec/lvPodLibrary.podspec'

执行pod install也会拉到对应的第三方库的代码。

(3)如果第三方库的代码lvPodLibrary不想放到远程,可以通过使用path的方式将代码添加到pod中,如下所示:

#Podfile中这样写

pod 'lvPodLibrary' , :path => './LocalPod/lvPodLibrary.podspec'

#.podspec文件中这样写

s.source = { :tag => s.version}

s.source_files = "lvPodLibrary"

注意:这样写时,将文件lvPodLibrary.podspec和第三方库代码lvPodLibrary并列放在本项目的同一文件夹下,比如本项目是LocalPod文件夹,否则找不到第三方库。

五、 podspec语法知识点

Pod::Spec.new do |s|

s.name = "NVUtils"

s.version = "0.0.1"

s.summary = "NVUtils repo"

s.description = <<-DESC

NVUtils repo

DESC

s.homepage = "http://XXXX/services"

s.license = 'MIT'

s.author = { "SheronLv" => "lvxueyin@hotmail.com" }

s.platform = :ios, "7.0"

s.source_files = "NVUtils/*.{h,m}"

s.resources = 'NVUtils/*.{xib,png}'

//s.source = { :git => 'https://github.com/ADVProgressBar.git', :commit => 'f17b15c15574d6d101cd5fcfd58239e16e806647' }

s.requires_arc = true

s.dependency 'Core1'

s.dependency 'Core2'

end

s.name 声明库的名称

s.summary 库的简短说明文档

s.homepage 声明库的主页(只是告诉了这个url,运行podfile并不会据此把库push到对应的url上)

s.version 库原代码的版本

s.license 所采用的授权版本

s.author 库的作者

s.source 原代码的地址

s.source_files 包含所有源代码的目录,目录的层级关系一定要跟代码文件的保持一致,最后一部分*.{h,m}是一个类似正则表达式的字符串,表示匹配所有以.h和.m为扩展名的文件。

s.resources NVUtils/目录下还有一个NVUtils.bundle目录,该目录存放一些资源文件(如图片等),这些文件并不需要进行编译。可以使用s.resourcs声明。

s.dependency 本库依赖的其他的第三方库

 s.vendored_libraries 指定外部的静态库

对比记忆以下四个设置:

 s.libraries 表示这个pod依赖的 苹果官方的库,也就是类似libstdc++.a ,libsqlite.a 等等的a文件;

s.vendored_libraries 就表示用户自己的a文件,比如新浪微博SDK的libWeiboSDK.a ;

s.frameworks 表示pod依赖的 苹果的framework, 比如 UIKit,SystemConfiguration等等

s.vendored_frameworks, 表示pod依赖的自己的framework,比如QQSDK的TencentOpenAPI.framework;

总结如下(引自:https://www.jianshu.com/p/5c67c41766f6):

# 基本信息的配置

name:框架名

version:当前版本(注意,是当前版本,假如你后续更新了新版本,需要修改此处)

summary:简要描述,在pod search的时候会显示该信息。

description:详细描述

homepage:页面链接

license:开源协议

author:作者

platform:支持最低ios版本

swift_version : swift对应的版本

# 源文件的配置

source:源码git地址

source_files:源文件(可以包含.h和.m)

subspec:子库

public_header_files:头文件(.h文件)

resource_bundles:资源文件(配置的文件会放到你自己指定的bundle中)

# 依赖的配置

frameworks:依赖的系统框架

vendored_frameworks:依赖的非系统框架

libraries:依赖的系统库

vendored_libraries:依赖的非系统的静态库

dependency:依赖的三方库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在开发Flutter插件时,有时候需要使用第三方库来实现一些功能,这时需要在Podspec文件中添加对第三方库的依赖描述。 以下是添加第三方库依赖的示例代码: ``` Pod::Spec.new do |s| s.name = '插件名称' s.version = '插件版本号' s.summary = '插件描述' s.homepage = '插件主页' s.license = '插件许可证' s.author = { '作者名称' => '作者邮箱' } s.source = { :git => '插件源代码仓库地址', :tag => '插件版本号' } s.dependency '库名称', '~> 版本号' # 插件依赖的第三方库 s.source_files = '插件源代码文件路径' # 插件的源代码文件 s.resource_bundles = { '插件资源文件名称' => ['插件资源文件路径'] } # 插件的资源文件 end ``` 其中,`dependency`方法用于添加依赖库的描述,第一个参数是库的名称,第二个参数是库的版本号。`~>`表示兼容某个版本号及以上的库。 当需要使用多个依赖库时,可以依次调用`dependency`方法添加多个依赖库的描述,如下所示: ``` Pod::Spec.new do |s| s.name = '插件名称' s.version = '插件版本号' s.summary = '插件描述' s.homepage = '插件主页' s.license = '插件许可证' s.author = { '作者名称' => '作者邮箱' } s.source = { :git => '插件源代码仓库地址', :tag => '插件版本号' } s.dependency '库1名称', '>= 版本号' # 插件依赖的第三方库1 s.dependency '库2名称', '~> 版本号' # 插件依赖的第三方库2 s.source_files = '插件源代码文件路径' # 插件的源代码文件 s.resource_bundles = { '插件资源文件名称' => ['插件资源文件路径'] } # 插件的资源文件 end ``` 需要注意的是,当使用第三方库时,需要将库的头文件和源文件一起打包到插件中,以便在编译插件时能够正确地链接库的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值