Skip to content
Khoa Pham@onmyway133

How to conditionally render widgets in iOS

1 min read

The WidgetBundle lets us expose multiple widgets from a single widget extension

It uses WidgetBundleBuilder to constructs a widget bundle’s body.

In iOS 18, if we include ControlWidget then we need to check iOSApplicationExtension iOS 18. For now in Xcode 16 there’s a bug that prevents existing widgets from appearing in iOS 17.

We can leverage WidgetBundleBuilder to conditionally render widgets for each iOS version

import SwiftUI
import WidgetKit
import Widgets

@main
struct OurAppWidgetBundle: WidgetBundle {
    var body: some Widget {
        if #available(iOSApplicationExtension 18.0, *) {
            return iOS18Widgets
        } else {
            return iOS17Widgets
        }
    }

    @available(iOSApplicationExtension 18.0, *)
    @WidgetBundleBuilder
    private var iOS18Widgets: some Widget {
        BookingsWidget()
        TransactionsWidget()
        BookControlWidget()
        RefundControlWidget()
    }

    @WidgetBundleBuilder
    private var iOS17Widgets: some Widget {
        BookingsWidget()
        TransactionsWidget()
    }
}

Keep reading

Type to search titles and full text.

↑ ↓ move↵ openPagefind