Skip to content
Flow-UILive
On this page

Hosting Flow-UI in UIKit

UIKit apps host a Flow page through FlowHostingController. The widgets are still SwiftUI. You do not rewrite the app first.

Ayush MishraPublished 6 min read

Hosting Flow-UI in UIKit is one controller, not a rewrite. FlowHostingController is a UIHostingController that takes a PageStore, an optional ActionDispatcher, and an optional ThemeProvider. The widgets are still SwiftUI. You push or present the controller from the navigation stack you already have.

Pure SwiftUI apps never need this type. They use FlowPageView directly. This post is for the app whose root is still UINavigationController, UITabBarController, or a coordinator that speaks view controllers. Installation is the package. How to implement SDUI on iOS is the iOS tutorial that mentions hosting in passing.

The type#

The initializer is the API. It is compiled only when UIKit is present (#if canImport(UIKit)). init(coder:) is unavailable; this is not a storyboard character.

HomeFlowViewController.swiftswift
import UIKit
import FlowUI
 
func makeHomeController(loader: some PageLoader, registry: WidgetRegistry) -> UIViewController {
    let store = PageStore(pageID: "home", loader: loader, registry: registry)
    let dispatcher = ActionDispatcher()
    dispatcher.register(DeeplinkHandler())
    return FlowHostingController(
        store: store,
        dispatcher: dispatcher,
        theme: BrandTheme()
    )
}

dispatcher and theme default to nil. A minimal call is FlowHostingController(store: store). When a theme is passed, the controller wraps FlowPageView in .flowTheme. When it is omitted, DefaultTheme comes from the environment default: hex colours, system fonts, no token table.

swift
navigationController?.pushViewController(
    FlowHostingController(store: store),
    animated: true
)

The root view is AnyView around FlowPageView. That erasure is the hosting boundary, not a second widget registry. Register widgets on the WidgetRegistry you pass into PageStore, the same as a SwiftUI app.

What stays UIKit, what becomes SwiftUI#

UIKit still owns window, tabs, the navigation controller, status bar style if you set it on the host, your existing modal flows, and any page that is not server driven. Checkout that is already a UIViewController can stay one.

SwiftUI owns the server driven page: nav bar described by the envelope (if you use it), sections, widgets, sheets that Flow-UI presents, toasts. Gestures inside widgets are SwiftUI gestures. Accessibility traits for envelope taps are the SwiftUI modifiers in EnvelopeGestures.

You can hide the UIKit navigation bar and let the envelope draw FlowNavBar, or the reverse: keep UIKit chrome and send a page without nav. Pick one per surface so back buttons do not double.

Interactive pop still comes from the UINavigationController if you push the hosting controller. The SwiftUI page does not replace UIKit navigation unless you present it that way.

Actions and the missing router#

Taps dispatch ActionData through ActionDispatcher. Built-ins cover toast, dismiss, refresh, bottom sheet, and api mutations. A deeplink is data. Your ActionHandler decides whether to pushViewController, select a tab, or open a URL. Flow-UI ships no router. That is deliberate: the UIKit app already has a navigation policy.

API actions round-trip through PageLoader. The framework performs no HTTP. Put auth headers where they already live.

DeeplinkHandler.swiftswift
struct DeeplinkHandler: ActionHandler {
    struct Payload: Decodable { let url: String }
 
    func handle(_ action: ActionData, context: ActionContext) async -> Bool {
        guard action.type == "deeplink",
              let payload = try? action.payload(Payload.self) else { return false }
        // Forward payload.url to the coordinator you already trust.
        return true
    }
}

Wire that handler onto the same dispatcher you passed into FlowHostingController. Unhandled actions show up in the debug overlay's action log during development.

Sheets, toasts, and UIKit modals#

open_bottom_sheet presents FlowSheetView from the SwiftUI tree inside the hosting controller. It is not present(_:animated:) on the nearest view controller unless you write a handler that does that. Default built-ins keep the sheet in SwiftUI. If your app's modal policy is UIKit-only (custom UIPresentationController, shared dimming), register a host handler that consumes open_bottom_sheet and presents your controller, passing the sheet envelope into another FlowHostingController. Two hosting controllers is allowed. Fighting the built-in sheet while it is still registered is how you get two modals.

toast is SwiftUI overlay. It will not appear on a UIKit view that is covering the hosting controller. If you push a UIKit checkout on top of the Flow page, toasts from the Flow page are not visible. Dismiss the UIKit modal first, or handle toast in the host.

dismiss closes the active Flow sheet, or asks the host to dismiss. If the hosting controller is itself a modal, your handler should dismiss(animated:). Built-ins cannot know your coordinator.

Handlers are consulted newest first. dispatcher.register appends; later registrations win for the same type. That is how you override toast without forking.

Lifecycle and Observation#

PageStore is @Observable and @MainActor. FlowPageView loads on appear. Pushing the hosting controller onto a stack starts that lifecycle. Popping it tears down the SwiftUI tree like any other UIHostingController.

Call store.loadInitial() if you need the fetch to start before the view appears. Otherwise let the page view kick the load. Do not fetch in viewDidLoad and again on appear without a policy; you will double-hit PageLoader.

Refresh and pagination are store APIs. The backend still describes the widgets. Pull to refresh is SwiftUI on the page, even though a UIKit UIRefreshControl exists elsewhere in the app. Mixing both on one surface is how users get two spinners.

iOS 17 remains the floor. Observation is how the view tree stays in sync. There is no iOS 16 hosting shim.

Theming and debug from UIKit#

Pass theme: for design tokens. Theming server driven SwiftUI is the protocol. You cannot set flowTheme from a UIKit appearance proxy; it is a SwiftUI environment value.

flowDebugOverlay(store:) is a SwiftUI modifier. The hosting controller does not add it by default. For debug builds, prefer a small SwiftUI wrapper around FlowPageView that applies the overlay, then host that wrapper, or add a SwiftUI View subclass you pass in. Shipping the ladybug in App Store builds is the wrong default; the modifier already compiles out in release.

Decode problems still live on store.diagnostics for telemetry. Decode diagnostics in production is the logging story.

You do not rewrite first#

A common stall: "we will adopt SDUI after we finish the SwiftUI migration." Hosting exists so a merchandising page can go server driven while settings stay UIKit. Register one widget, load one page, push one controller. The rest of the binary can wait.

New widget types still need a binary. Hosting does not change App Review. It only changes whether the server driven page is a UIViewController in your stack.

macOS compiles FlowRender but this controller is UIKit-only. A Mac app uses FlowPageView in SwiftUI. Do not look for an NSHostingController subclass in the package; there is not one with this name.

Image loading remains FlowImageLoader on the environment. Set it in SwiftUI with .flowImageLoader on a wrapper you host, the same as theme. A UIKit image pipeline in a table cell will not intercept FlowImage. Plug the seam or live with the default loader.

The Flow-UI mark: two nodes joined through a filled centre

Hosting Flow-UI in UIKit: FlowHostingController(store:dispatcher:theme:), widgets still SwiftUI, no rewrite first. The navigation policy stays the app's.

More on the blog.

Read the source

Flow-UI is MIT licensed. The schema, renderer and starter widgets live on GitHub.

GitHub

Get started with Flow-UI

Install the Swift package, register a widget, and render a page from JSON.

Get started