

Documenting your design system when working with native platforms like iOS and Android can be a bit challenging. On the web, your design system documentation can be dynamic and show off exactly what your components look like. You can embed them in Storybook, or even use the code blocks in zeroheight to show exactly how a component will look (and how it works). How do you show native components beyond just screenshots and videos?
Instead of bringing native components to our documentation, let’s bring documentation closer to our native components by leveraging the zeroheight API, and building a component reference app natively in iOS.
Storybook on the web is a way to show off your components in isolation, to play around with them, and use as a rapid development environment. We’re going to take this idea and build something native for iOS. We’ll display a list of components to choose from, a preview for a component, and documentation for that component.
To keep things simple, we will be leveraging the following packages:
Let’s start with the zeroheight API and see how we can get back some documentation.
The zeroheight API provides access to a page content in Markdown. All we need to provide is a page ID (or UID). This means we can create our documentation on zeroheight and simply copy the UID from of the URL in the zeroheight page we want. We’re following a similar approach to the Storybook add-on.
To get the documentation, we have to do the following:
We need a Client ID and Access Token to call the zeroheight API, these can be created in your organization settings. These credentials need to be input and stored in the app so we can reuse them whenever we want to request content from the API.
We’ll create a view to ask the user for these details:
import KeychainSwift
import SwiftUI
struct AuthCredentialsView: View {
@Environment(\.dismiss) private var dismiss
@State private var accessToken: String = ""
@State private var clientId: String = ""
let keychain = KeychainSwift()
var body: some View {
VStack {
Form {
TextField("Client ID", text: $clientId)
SecureField("Access Token", text: $accessToken)
}
Button("Login") {
keychain.set(accessToken, forKey: "accessToken")
keychain.set(clientId, forKey: "clientId")
}
}
}
}This is a simple form where the user of the app will be able to input their credentials. We leverage KeychainSwift to store them in Keychain so it’s secure and can be used next time we open the app without having to input the credentials again.

To encapsulate all the logic we need for making a request, I’m creating an APIServicestruct with two static methods:
APIService.retrieveCredentials — this will access Keychain and pull out the Client ID and Access Token that was saved by the user.APIService.request — this will retrieve the credentials and make the request. import Foundation
import KeychainSwift
struct APIService {
static func retrieveCredentials() -> (String?, String?) {
let keychain = KeychainSwift()
let storedClientId = keychain.get("clientId")
let storedAccessToken = keychain.get("accessToken")
return (storedClientId, storedAccessToken)
}
static func request(_ path: String) async throws -> (Data, URLResponse) {
let credentials = retrieveCredentials()
let url = URL(string: "https://zeroheight.com/open_api/v2/\(path)")!
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue(credentials.0, forHTTPHeaderField: "X-API-CLIENT")
request.setValue(credentials.1, forHTTPHeaderField: "X-API-KEY")
return try await withCheckedThrowingContinuation { continuation in
URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
continuation.resume(throwing: error)
return
}
guard let data = data, let response = response else {
continuation.resume(throwing: URLError(.badServerResponse))
return
}
continuation.resume(returning: (data, response))
}.resume()
}
}
}It’s generic enough that we could also leverage it to bring in more content from zeroheight in the future. For now, we can use this service to make a request to the pages endpoint.
We require some models to represent the data we get back from the zeroheight API. When we make a request like: GET https://zeroheight.com/open_api/v2/pages/7735a1?format=markdown
We’ll get a JSON response back that looks like this:
{
"status": "success",
"data": {
"page": {
"id": 1234,
"uid": "7735a1",
"slug": "7735a1-my-page",
"url": "https://zeroheight.com/123abc/v/latest/p/7735a1",
"name": "My page",
"introduction": "This is my page",
"hidden": false,
"locked": false,
"created_at": "2024-11-05T16:09:28.678Z",
"updated_at": "2024-11-05T16:09:28.678Z",
"content": "## My page<strong>\n\n</strong>Some content here"
}
}
}For us to be able to use this in our app, we’ll need a representation in Swift. Let’s create some simple models to handle deserializing this data.
struct ZHPage : Codable{
let id: Int
let content: String
let url: String
}
struct ZHPageWrapperResponse : Codable {
let page: ZHPage
}
struct ZHPageResponse : Codable {
let status: String
let data: ZHPageWrapperResponse
}We’re only interested in a few properties, so I’m skipping all properties of the page except for the ID, the content and the URL for the page.
Now we have a simple representation of a response and the page, let’s see how we might make a request within a view.
func getDocumentation() async {
do {
errorMessage = nil
let (data, _) = try await APIService.request(
"pages/\(id)/?format=markdown"
)
let decoder = JSONDecoder()
let pageResponse = try decoder.decode(
ZHPageResponse.self,
from: data
)
page = pageResponse.data.page
} catch {
errorMessage =
"Failed to get documentation. Check your auth credentials and try again."
print("Error: \(error)")
}
}Stepping though the code we:
page or errorMessage state This is everything we need to start getting real data into our iOS app.
Like Storybook, we want to be able to navigate between different components and to achieve this we need a predictable structure.
Here is something simple to start with:
struct DocumentedComponent : Identifiable {
let id = UUID()
let name: String
let documentationID: String
let view: AnyView
}This structure will be used to define our “stories” for each component. We require an id so we can iterate through them in SwiftUI, the name will be used during navigation, the documentationID will be our zeroheight page UID and finally view, this is the SwiftUI view to render.
Putting it all together, we can start connecting our documentation to our components:
let ComponentDocumentation = [
DocumentedComponent(
name: "Primary Button",
documentationID: "18cce4",
view: AnyView(
PrimaryButton(
text: "Greeting",
action: {
print("action")
}
)
)
),
DocumentedComponent(
name: "Toggle",
documentationID: "2166a9",
view: AnyView(
VStack {
Toggle(isOn: .constant(false)) {
Text("Enable")
}.toggleStyle(BrandedToggleStyle())
Toggle(isOn: .constant(true)) {
Text("Enable")
}.toggleStyle(BrandedToggleStyle())
}.padding()
)
),
]Here, we have two components, a button and a toggle. The button shows just the component in one state, and the toggle shows a couple of different states (on and off).
In our SwiftUI view, we can then render a list of our components like so:
NavigationSplitView {
List(ComponentDocumentation, selection: $activeComponentUUID) {
documentedComponent in
NavigationLink(
documentedComponent.name,
value: documentedComponent.id
)
}
.navigationTitle("Components")
} content: {
VStack(alignment: .leading) {
activeComponent.view
.padding(50)
Spacer()
}
.navigationTitle(activeComponent.name)
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
Button {
displayDocumentation = true
} label: {
Image(systemName: "text.document")
}
}
.toolbarTitleDisplayMode(.inlineLarge)
} detail: {
Text("Choose a component")
}You’ll notice a button in the toolbar that controls displayDocumentation, we’ll use this to present an action sheet without our documentation in it.

Now we can navigate to a component, the final piece of the puzzle is showing documentation from zeroheight in our app. When a user taps the documentation toolbar button, we want to present a sheet.
.sheet(isPresented: $displayDocumentation) {
displayDocumentation = false
} content: {
DocumentationView(id: activeComponent.documentationID)
.presentationDetents([.medium, .large])
.presentationDragIndicator(.visible)
.presentationContentInteraction(.scrolls)
}The DocumentationView will handle fetching the markdown from the zeroheight API (using our getDocumentation function from earlier)and displaying the markdown simply with Markdown(page.content).

There we have it, we have documentation for our component, inside a native iOS app. Check out the project if you want to have a look at the code and see how this was done.
I’ve shown a simple implementation of how to get documentation out of zeroheight and into iOS app, but there is lots more we could do.
Here are some ideas on what you could do to go even further:
As long as the platform you want to target supports saving credentials, making a network request and displaying markdown — we can use this approach to view zeroheight documentation anywhere.