欢迎来到DrivenUI SDK
坦白说,它不是HTML……但它在某种程度上又有点像。但是实际上,它不是。
DrivenUI是一个iOS SDK,它使得在iOS应用中引入和构建服务器驱动的UI功能变得非常方便。目前它是基于JSON响应格式来在屏幕上渲染SwiftUI视图的。
目录
需求
- Xcode 11+
- Swift 5.1+
- Cocoapods 1.9+
- 支持SwiftUI的应用程序。
安装
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。关于使用和安装说明,请访问他们的网站。要使用 CocoaPods 将 DrivenUI 集成到您的 Xcode 项目中,请在 Podfile
中指定它
pod 'DrivenUISDK', '~> 0.1.0'
Swift 包管理器
Swift 包管理器 是一个用于自动分发 Swift 代码的工具,并集成到 swift
编译器中。它目前处于早期开发阶段,但 Driven 支持在支持的平台上使用它。
一旦您设置了 Swift 包,将 DrivenUI 添加为依赖项就像将其添加到您的 Package.swift
的 dependencies
值一样简单。
dependencies: [
.package(url: "https://github.com/KarimEbrahemAbdelaziz/DrivenUI.git", .exact("0.1.0"))
]
用法
初始化 SDK
-
因为我们在
BETA
版本中,请安装一个有关您的应用程序Bundle Id
的问题,并留下您的邮箱,我们将立即发送您的 DrivenUI SDK 密钥。 -
您有两种方式在应用程序中初始化 DrivenUI。
- 如果您正在使用 SwiftUI 新的应用程序生命周期,请将配置方法添加到您的主 App 的
init
中。 - 如果您使用
AppDelegate
,请在didFinishLaunchWithOptions
方法中添加配置方法。
- 如果您正在使用 SwiftUI 新的应用程序生命周期,请将配置方法添加到您的主 App 的
Driven.configure(with: YUOR_SDK_KEY)
基本使用
连接到您的后端并检索您的 JSON 响应(请检查支持的查看 JSON 结构 格式)。
在您的 ViewModel 中解析您的 JSON 响应
// Add published property into your ViewModel
// so that you views can react on it's changes ;)
@Published var serverView: DrivenComponent?
// Parse JSON response from API to DrivenComponent
let response = try? JSONDecoder().decode(DrivenComponent.self, from: data)
if let response = response {
DispatchQueue.main.async {
self?.serverView = response
}
} else {
DispatchQueue.main.async {
self?.serverView = nil
}
}
在您的 Content View 中添加此构建函数
@ViewBuilder func buildView() -> some View {
if let serverView = viewModel.serverView {
DrivenComponentFactory(material: serverView).toPresentable()
} else {
Text("No views from server.")
}
}
在您的视图层次结构中使用这个 buildView
var body: some View {
NavigationView {
buildView()
.padding([.top], 48)
.navigationBarTitle("DrivenUI SDK")
}
}
然后 WOW 您的视图已经在屏幕上渲染了
JSON 结构
每个支持的视图组件都由4个键组成,每个键根据您需要渲染的组件占据一个位置
{
// type values are the Supported Views (Please check below section).
"type": "",
// properties values are pair of key-value of Supported Properties (Please check below section).
"properties": { },
// values are pair of key-value of Supported Values for each View (Please check below section).
"values": { },
// subviews values are pair of key-value of another component should be rendered inside one of those views: HStack, VStack, ZStack, and List.
"subviews": { }
}
文本视图的示例后端响应
{
"type": "Text",
"properties": {
"foregroundColor": "000000",
"font": "title",
"fontWeight": "regular",
"width": 300
},
"values": {
"text": "It's simple version working! 😎"
}
}
图像视图的示例后端响应
{
"type": "Image",
"properties": {
"height": 90
},
"values": {
"imageUrl": "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
}
}
HStack 视图的示例后端响应
{
"type": "HStack",
"properties": {
"spacing": 16,
"verticalAlignment": "top"
},
"subviews": [
{
"type": "Image",
"properties": {
"height": 40,
"width": 40
},
"values": {
"localImageName": "hiking"
}
},
{
"type": "VStack",
"properties": {
"padding": 8,
"horizontalAlignment": "leading"
},
"subviews": [
{
"type": "Text",
"properties": {
"font": "title",
"fontWeight": "bold"
},
"values": {
"text": "Hiking"
}
},
{
"type": "Text",
"properties": {
"foregroundColor": "333333"
},
"values": {
"text": "This is Hiking sport."
}
}
]
}
]
}
支持的视图
- 列表
- 图像
- 支持的值
imageUrl
用于从URL检索图像。systemIconName
用于使用系统图像。localImageName
用于使用资产目录图像。
- 支持的值
- 文字
- 支持的值
text
将显示的文本值。
- 支持的值
- 堆叠(HStack、VStack 和 ZStack)
- 形状(矩形和圆形)
- 空格和分隔符
支持的属性
width
和height
- 浮点值。
foregroundColor
、backgroundColor
和borderColor
- 十六进制颜色字符串值(例如,“ffffff”)。
borderWidth
、spacing
(用于 HStack 和 VStack)和padding
- 整数值。
水平对齐
leading
center
trailing
垂直对齐
top
bottom
center
firstTextBaseline
lastTextBaseline
font
largeTitle
title
headline
subheadline
body
callout
footnote
caption
fontWeight
ultraLight
thin
light
regular
medium
semibold
bold
heavy
black
待办事项
请查看 变更日志 文件以获取有关每个版本包含内容的更多信息。
- 为 DrivenUI SDK 创建网站。
- 使用 Bitrise 设置 CI/CD 工作流程。
- 为 JSON 视图添加向后兼容性。
- 支持不同的操作系统(tvOS、macOS 和 watchOS)
- 支持更多视图。
- 支持更多视图修饰符。
- 支持视图的动作。
- 支持导航。
- 支持Protocol Buffers。
- 详细列出所有支持的视图,并添加更多示例和用例。
- 构建材料组件库。
许可证
- 您可以在此处找到许可证信息。