已知UniApp的插件开发方式有两种, 第一种 Xcode Framework的方式,这种方式是官方指定的方式。 这种方式的优点就是简单直接, 但也有不足,比如当插件需要引入一些三方库时,操作起来就不是那么方便。 而使用Cocoapods则可以很方便的引入三方库。 接下来,本文探索使用Cocoapods的方式来进行UniApp插件开发。
探索之前可以先了解一下官方插件开发的流程及步骤:
iOS插件开发教程
如果是首次使用Cocoapods ,则需要先安装cocoapods。具体的安装步骤非本文重点,可参考如下文章:
安装Cocoapods的步骤
找到UniApp iOS SDK下载的目录,看一下官方提供的目录结构
打开“命令行”工具,cd
到 HBuilder-uniPlugin.xcodeprj
工程所在目录下, 并执行
pod init
来创建Podfile 文件模板
此时目录下会多出一个 ‘Podfile
’ 的文件
打开Podfile
文件, 配置相关设置如下 :
# Uncomment the next line to define a global platform for your project
platform :ios, '10.0'
#忽略pod所有库警告
inhibit_all_warnings!
workspace 'uniPlugins'
#关闭所有pod库的BITCODE
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO'
end
end
end
target 'HBuilder' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
end
配置忽略Pod所有库的警告
inhibit_all_warnings!
配置workspace名称
workspace 'uniPlugins'
关闭所有pod库的BITCODE
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO'
end
end
end
在HBuilder-uniPluginDemo目录下创建一个自定义插件目录 ‘custom-plugins
’ 用来存放自定义的插件工程,目录结构如下:
使用命令行工具, cd
到 custom-plugins
目录下, 并执行
pod lib create rz-testplugin
按回车,执行命令。 会从github上加载创建pod工程的模板。 模板下载结束后,会出现如下引导:
按上述引导完成配置,在完成创建后会自动打开pod 工程, 目前用不到此工程,接着关闭即可
回到目录 HBuilder-uniPluginDemo下,找到Podfile
文件,并打开
将本地新创建的pod工程配置进去
target 'HBuilder' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
#重要: 导入自定义组件库
pod 'rz-testplugin', :path =>'./custom-plugins/rz-testplugin'
end
完整的Podfile文件如下:
# Uncomment the next line to define a global platform for your project
platform :ios, '10.0'
#忽略pod所有库警告
inhibit_all_warnings!
workspace 'uniPlugins'
#关闭所有pod库的BITCODE
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO'
end
end
end
target 'HBuilder' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
#重要: 导入自定义组件库
pod 'rz-testplugin', :path =>'./custom-plugins/rz-testplugin'
end
Podfile配置完成后, 使用命令行工具在Podfile所在目录下,执行命令
pod install
此时的目录结构如下:
我们看到在HBuilder-uniPluginDemo目录下 多了一个 uniPlugins.xcworkspace
的文件, 而这个文件名不正是我们在podfile中配置的吗
workspace 'uniPlugins'
双击此文件打开, 工程结构如下
目前会有两个工程如下: 一个DCTestUniPlugin, 一个HBuilder。 HBuilder 为主工程, DCTestUniPlugin为 SDK自带的插件工程
默认如果在DCTestPlugin上,切换到HBuilder工程
好了,通过上面的操作,我们基本上已经把使用pod lib方式创建的插件工程集成到了workspace上了。 接下来就是插件开发了
引用官方文档:
插件扩展方式
原生插件是基于 DCUniPlugin 规范来实现,扩展原生功能有两种方式:
- module:不需要参与页面布局,只需要通过 API 调用原生功能,比如:获取当前定位信息、数据请求等功能,通过扩展module的方式来实现;
- component:需要参与页面布局,比如:map、image等需要显示UI的功能,通过扩展component即组件的方法来实现;
您需要根据实际的情况选择扩展方式,当然插件中可以同时存在 module 和 component,也可以是多个 module 和 多个 component;
更多内容详情: https://zhanglei.blog.csdn.net/article/details/123221947
更多内容详情: https://zhanglei.blog.csdn.net/article/details/123221947
本文来自博客园,作者:reyzhang,转载请注明原文链接:https://www.cnblogs.com/reyzhang/p/17094730.html
参与评论
手机查看
返回顶部