Advanced WidgetKit Configuration with AppIntents

Result builders make this API hard to figure out, so here’s some sample code

December 26, 2024 - 2 minute read -
swift ios uikit

WidgetKit & AppIntents


iOS 17 introduced a new way to configure widgets using AppIntents. I’m generally a big fan - entities and configuration are all defined in code instead of an intentsdefinition file. However, some of the APIs are not very discoverable, nor well documented. Here are some neat things you can do that were pretty difficult for me to find examples of.

The API we’re using is only available starting with iOS 17.

Varying configuration parameters by widget family


The parameterSummary syntax can sometimes boggle the mind. It uses Result Builders, which do provide a lot of power and flexibility, but also make for a bit of a blackbox when it comes to API discoverability.

Let’s say you want to show certains parameters only for larger widgets, because the data it affects is not displayed in the smaller variant. You don’t need to provide two separate widgets; you can adjust the summary you provide using a Switch over the .widgetFamily:

static var parameterSummary: some ParameterSummary {
    Switch(.widgetFamily) {
        Case(.accessoryInline) {
            Summary {
                \.$account
            }
        }

        // Can match against multiple values with an array
        Case([.accessoryRectangular, .accessoryCircular]) {
            Summary {
                \.$account
                \.$range
            }
        }

        DefaultCase {
            Summary {
                \.$account
                \.$range
                \.$showChart
            }
        }
    }
}


More to come…