iOS cocoaPods 集成本地库
前言:pod化是组件化开始,最近在尝试把本地业务or功能模块集成进pods中,网上的文章参考了很多,但有时也没有解决我的问题,目前简单实现了集成本地库,mark一下
新建lib库
在工程目录新建文件夹,名称XXXLib
(随便什么名字,最好可读性较强,带有集成库的含义即可)
新建同名文件夹
在上述库中新建与你想要集成库同名文件夹
我要集成的文件是ToastView,所以新建了该文件夹
创建 .podspec 文件
pod 命令会生成 .podspec 文件
cd ToastView/
pod spec create ToastView
配置 .podspec 文件
如下简单描述基本 .podspec
配置内容,后面会根据问题补充
Pod::Spec.new do |spec|
spec.name = "ToastView"//库名称
spec.version = "0.0.1"//库版本号
spec.summary = "APP network show toastView."//简单总结库是做什么的
spec.description = <<-DESC
TODO: a toastview for app network
DESC
//一句话详细描述下该库的作用
//简单注意下description要比summary文字描述要长,否则会报警告⚠️,警告无碍
spec.homepage = "http://icode.XXX.com/repos/APPXXX"
//配置当前网络可ping通的地址,我写了公司的icode地址
spec.license = "LICENSE"//许可文件,下面会谈及
spec.author = { "rabbit" => "rabbit@xxx.com" }//作者,一般会默认帮你生成,也可以自己修改
spec.source = { :git => "", :tag => "#{spec.version}" }
//因为配置本地库,没有做网络,git地址为空,你可以加上该库的git地址
spec.source_files = "Classes", "Classes/**/*.{h,m}"
//源文件存放地,存放在哪个文件夹下,该处的名称需要和你文件存放文件夹一致
#spec.exclude_files = "Classes/Exclude"
//exclude注释了,因为没用上
end
新建许可文件
许可文件名称和上述配置文件中相同
touch LICENSE
LICENSE
文件是许可声明文件,里面主要是对库的类似版权声明一样(可以理解成软件著作权声明),网上随便可搜到,我使用的“MIT”,自己公司用的话就无所谓了,有这个文件就行
附上我的LICENSE
文件内容
Copyright (c) 2019 rabbit<rabbit@xxx.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
源文件存放
根据配置文件配置源文件存放地址
新建 Classes
文件夹,把源文件存放里面
检查 .podspec
执行检查 .podspec 文件命令,一般会有错误提示,根据错误提示一步一步解决,直至通过验证
切换到ToastView.podspec
路径,执行命令
下面两句命令都可以
pod lib lint ToastView.podspec
pod lib lint
ERROR 列举
error1.集成库引用了其他库方法
-ERROR | xcodebuild: /Users/xxx/Desktop/xxx/ToastView/Classes/ToastView.swift:293:9: error: use of unresolved identifier 'printLog'
该问题原因是集成的库内引用了其他文件封装的方法,要么依赖库添加进来,要么想办法处理该引用
在. podspec
文件中添加依赖库,例如:
spec.dependency "JSONKit", "~> 1.4"
2.系统库依赖
-ERROR | [watchOS] xcodebuild: /Users/Desktop/ToastView/Classes/ToastView.swift:25:11: error: use of undeclared type 'UILabel'
- ERROR | [watchOS] xcodebuild: /Users/Desktop/ToastView/Classes/ToastView.swift:40:22: error: use of undeclared type 'UIWindow'
- ERROR | [watchOS] xcodebuild: /Users/Desktop/ToastView/Classes/ToastView.swift:290:29: error: use of undeclared type 'UITapGestureRecognizer'
因为这是封装一个加载框的控件,所以用了UIKit
这个框架,但在. podspec 文件中没有指定运行平台
在. podspec
文件中有关运行平台:
spec.platform = :ios, "9.0"
//还可以配置平台 or 配置环境 or 配置系统
# spec.ios.deployment_target = "5.0"
# spec.osx.deployment_target = "10.7"
# spec.watchos.deployment_target = "2.0"
# spec.tvos.deployment_target = "9.0"
3.缺少tag
- ERROR | [iOS] unknown: Encountered an unknown error ([!] /usr/bin/git clone/var/folders/lc/zl333w0s6t12qjdfl0_brmdh0000gn/T/d20170315-37512-16fmh2q --template= --single-branch --depth 1 --branch 0.0.1
Cloning into '/var/folders/lc/zl333w0s6t12qjdfl0_brmdh0000gn/T/d20170315-37512-16fmh2q'...
warning: Could not find remote branch 0.0.1 to clone.
fatal: Remote branch 0.0.1 not found in upstream origin
出现上面错误的原因是没有为项目打上tag,或者打上tag没有推到远程仓库
操作命令
git tag -m"ToastView" "0.0.1"
git push origin --tags
4.swift版本
[!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a .swift-version file to set the version for your Pod. For example to use Swift 2.3, run:
echo "2.3" > .swift-version.
You can use the --no-clean option to inspect any issue.
终端执行
echo 3.0 > .swift-version
5.警告处理
ERROR | [iOS] unknown: Encountered an unknown error (The 'Pods-App' target has transitive dependencies that include static binaries:
[!] ToastView did not pass validation, due to 1 warning (but you can use--allow-warnings
to ignore it).
You can use the--no-clean
option to inspect any issue.
--verbose 显示检查编译的详细信息
--allow-warnings 验证时允许有警告
通过验命令,可忽略警告
pod lib lint --verbose --use-libraries --allow-warnings
通过验证
运行命令,处理问题,直至通过验证
通过验证
Podfile 添加本地库
pod 'ToastView',:path => './XXXLib/ToastView'
path表示库的位置,如果是在git上,需要加git 或者 source
source 'https://xxx/xxxx.git' //你的库的地址
pod 'ToastView','git@xxx/xxx.git'//对自己的库添加git地址
Pod 更新
pod install --verbose --no-repo-update
pods本地库集成
小结:目前仅仅集成了本地库,没有做网络的映射,想配置git的小伙伴可参考其他博客
未完待续ing