Server-Driven UI on iOS: Modern Perspective of Dynamic Applications
As applications are growing in size and usage, so does the demand for high flexibility and scalability. One of the upcoming trends, to achieve the aim, is Server Driven UI: a shift of the UI composition responsibility from the client side to the server. In this topic, we will try to address what Server Driven UI means, the advantages it brings as well as the challenges and how you can utilize it in your iOS applications.
In the Server Driven UI design pattern, the server takes control over the structure, layout and content of the user interface. Rather than hardcoded views in the app, the server sends a JSON (or similar) payload describing the UI. The client app interprets this payload and renders the UI dynamically.
How it works
Server Side Definition: The server provides a UI definition as a JSON or XML file.
Client-side Rendering: The client, using components provisioned beforehand, interprets the payload and builds up the UI dynamically.
Update without App Release: The user interface can be entirely changed on the server, in this case there is no required app update.
Now, let us discuss implementing Server Driven UI using Swift.
1. Define a UI Schema
Send a JSON payload from the server having a description of the structure of UI. This is the schema example:
{
"type": "container",
"style": {
"orientation": "vertical"
},
"children": [
{
"type": "text",
"content": "Welcome to Server-Driven UI!",
"style": {
"fontSize": 24,
"textColor": "#000000"
}
},
{
"type": "button",
"content": "Get Started",
"action": "navigateTo: Onboarding"
}
]
}
2. Parse the JSON Payload
Parse the above JSON on the client into Swift model:
struct UIComponent: Decodable {
let type: String
let style: [String: String]?
let content: String?
let action: String?
let children: [UIComponent]?
}
3. Render the UI Dynamically
Use SwiftUI or UIKit to render the UI parsed from within the JSON.
4. Handle Actions
Define the actions: navigation, API calls or any other interaction in your app.
func handleAction(action: String) {
if action.starts(with: "navigateTo:") {
let destination = action.replacingOccurrences(of: "navigateTo:", with: "")
// Navigate to the specified view
}
}
Example with SwiftUI:
import SwiftUI
struct DynamicView: View {
let component: UIComponent
var body: some View {
switch component.type {
case "container":
VStack {
ForEach(component.children ?? [], id: \.content) { child in
DynamicView(component: child)
}
}
case "text":
Text(component.content ?? "")
.font(.system(size: CGFloat(Int(component.style?["fontSize"] ?? "16") ?? 16)))
.foregroundColor(Color(hex: component.style?["textColor"] ?? "#000000"))
case "button":
Button(action: {
handleAction(action: component.action ?? "")
}) {
Text(component.content ?? "")
}
default:
EmptyView()
}
}
func handleAction(action: String) {
if action.starts(with: "navigateTo:") {
let destination = action.replacingOccurrences(of: "navigateTo:", with: "")
// Implement navigation logic to the 'destination' view
print("Navigating to \(destination)")
}
// Implement other actions like show alert, call API, etc.
}
}
// Helper extension for Color to handle hex values
extension Color {
init(hex: String) {
let hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines).replacingOccurrences(of: "#", with: "")
var int = UInt64(0)
Scanner(string: hexSanitized).scanHexInt64(&int)
let red = Double((int & 0xFF0000) >> 16) / 255.0
let green = Double((int & 0x00FF00) >> 8) / 255.0
let blue = Double(int & 0x0000FF) / 255.0
self.init(red: red, green: green, blue: blue)
}
}
Here is a small list of libraries and tools that could do a world of benefit in speeding up the development of Server Driven UI:
Server Driven UI is a potentially powerful paradigm for flexible, dynamic and scalable iOS applications. Production applications could be delivered faster and personalized by dispensing with UI control at the server side. Such great changes are not without problems and should be addressed with keen concern for the whole implementation of the server driven design.
When developing an application that will frequently need updates or changes, Server Driven UI might be for you. You will be doing some small stuff at first, then experimenting before exposing the full potential of this next gen way of doing app development.
Ready to transform your business with our technology solutions? Contact Us today to Leverage Our iOS Expertise.
0