4️⃣Configuring Widgets
Fall 2023 | Reade Plunkett
AppIntents
AppIntents is the framework developed by Apple that allows us to configure our widgets with custom information provided by the user.
Creating a New Intent
We will create a new intent that allows the user to input a location and have the weather widget update its displayed conditions.
Let’s start by creating a new Swift file, which we will call LocationAppIntent
.
Within that file, we will define a new structure that conforms to the WidgetConfigurationIntent
protocol. This protocol provides us with an interface for configuring a WidgetKit widget.
This protocol requires us to implement a title for this Intent. Additionally, we can also implement a description on what this intent does.
Next, we can define a parameter for this intent with a default value. The parameter will contain the location that use inputs.
Changes to WeatherEntry
Since our intent can make changes to a widget entry within our timeline, we must include it inside of the WeatherEntry model.
Changes to WeatherTimelineProvider
We must also update our timeline provider to support this new app intent. We will change our provider to conform to the AppIntentTimelineProvider
protocol to do so.
Additionally, we will need to update the function headers for fetching a snapshot and timeline entry.
These two functions provide us with the intent called configuration
that contain user-customized values for the location.
Within both of these functions, we have to update our WeatherEntry
instantiation to pass in the configuration.
Additionally, we also have to update the entry created within our placeholder function with a new intent object.
Changes to Widget Configuration
Within WeatherWidget.swift
, we also have to change our widget’s configuration from StaticConfiguration
to AppIntentConfiguration
.
Changes to View
Finally, within WeatherWidgetView
, we can update the “Ithaca, NY” string to display the location passed in through the app intent associated with the timeline entry being displayed.
Additionally, to view the widget in the Preview window, we will need to provide it with a default configuration. We can define a default intent as an extension of our LocationAppIntent
within our view. Since we do not want this default value being used anywhere outside this file, we mark it with fileprivate
.
We then update the preview entries to use this new configuration.
Last updated