Widget Extension in iOS Development
In this article
A widget extension in iOS development is a separate, lightweight process that lets your app display small, glanceable pieces of information on the Home Screen, Lock Screen, or Today View. Introduced in iOS 14 (with Lock Screen widgets arriving in iOS 16), widget extensions are built using SwiftUI and are managed by the system, not your main app. They are read-only, meaning they cannot accept user input like buttons or scroll views, and they update on a schedule set by the system or via timeline entries you provide.
Background: From Today View to Home Screen
Widgets existed before iOS 14, but only in the Today View (the screen you reach by swiping right on the Home Screen or Lock Screen). Those older widgets were built with UIKit and were essentially mini-views of your app. They were also more interactive, but they had limitations: they couldn't be placed on the Home Screen, and they were often slow to update.
With iOS 14, Apple introduced the modern widget system, built entirely on SwiftUI. These widgets are not full views; they are timeline-based displays that render a series of snapshots. The system decides when to show each snapshot, and it can pause or defer updates to save battery. This is a fundamental shift: you don't control when your widget updates; you provide a timeline and the system decides when to advance it.
How a Widget Extension Works
A widget extension is a separate target in your Xcode project. It contains its own SwiftUI views, a timeline provider, and an entry model. The extension runs in its own process, which means it cannot access your main app's data directly unless you share it via App Groups or a shared container.
The core components are:
- TimelineProvider: This object tells the system when to update the widget. It returns a timeline with entries, each containing a date and the data to display. The system will render the entry that corresponds to the current time.
- Entry: A struct that conforms to
TimelineEntry. It holds the date and any custom data your widget needs to display. - View: A SwiftUI view that renders the widget's content. It receives an entry and displays it.
- WidgetConfiguration: A static property that defines the widget's kind, supported families (systemSmall, systemMedium, systemLarge, accessoryCircular, etc.), and the view builder.
When you add a widget to the Home Screen, the system asks the extension for a timeline. The extension provides a set of entries, and the system displays them in order, advancing as the dates pass. When the timeline runs out, the system requests a new one. You can also use WidgetCenter.shared.reloadTimelines(ofKind:) to force a reload from your main app, but the system still controls the actual timing.
Choosing Widget Families and Sizes
iOS supports several widget families, each with a specific size and purpose:
- systemSmall: A 2x2 grid icon-sized widget, ideal for a single piece of information.
- systemMedium: A 4x2 rectangle, good for more detail or a small list.
- systemLarge: A 4x4 square, suitable for richer content like a calendar or weather forecast.
- accessoryCircular, accessoryRectangular, accessoryInline: These are Lock Screen widgets (iOS 16+) and also appear on the Apple Watch face.
You must implement all the families you want to support; the system will not scale your widget automatically. You can check the widget family in your view using @Environment(\.widgetFamily) and adjust the layout accordingly. For example, a small widget might show just a temperature icon, while a medium widget shows a 5-day forecast.
Common Problems and Misconceptions
One common misconception is that widgets can run code in the background. They cannot. They are passive displays. Any network calls or data processing must happen in the timeline provider, which is called by the system when it needs new entries. The system may call it at unpredictable times, so you cannot rely on real-time updates.
Another frequent issue is that widgets don't update immediately when your app changes data. Because the system controls the timeline, there is a delay. You can request a reload, but the system may defer it. To minimize delay, use WidgetCenter.shared.reloadAllTimelines() only when necessary, and design your timeline to include future updates (e.g., a weather widget can provide entries for the next 6 hours).
Also, widgets are not interactive. Tapping a widget opens your app, but you cannot have buttons or sliders inside the widget. If you need interactivity, consider a Lock Screen widget with a button (iOS 17 added Button support for Lock Screen widgets, but Home Screen widgets remain non-interactive).
Comparing Widget Extensions to Other App Extensions
Widget extensions are one of several extension points in iOS. Others include:
- Share Extensions: Let users share content from your app to other apps.
- Action Extensions: Provide custom actions in the share sheet.
- Notification Service Extensions: Modify push notifications before they are displayed.
- Intents Extensions: Power Siri Shortcuts and suggestions.
Unlike those, widget extensions are purely visual and read-only. They don't receive user input, and they don't have a full view controller lifecycle. They are also unique in that they are rendered by the system, not by your app, which is why they must be lightweight and fast.
Costs and Performance Considerations
Widget extensions are generally low-cost, but they do consume system resources. Each widget uses memory and CPU when it renders, and the system may keep multiple widgets alive if the user has several. To keep performance high:
- Keep your widget views simple; avoid heavy animations or complex drawing.
- Precompute data in the timeline provider; don't fetch data in the view.
- Use
.widgetURL()to deep-link to a specific part of your app when the widget is tapped. - Limit the number of entries in a timeline; the system may discard entries far in the future.
There is no direct monetary cost to add a widget extension; it's part of your app bundle. However, it does increase your app's size slightly, and you must maintain it separately. Also, if your widget uses network calls, be mindful of battery usage; the system will throttle background refreshes if it detects excessive energy use.
Alternatives to Widget Extensions
If you need more interactivity or real-time updates, widgets may not be the right choice. Alternatives include:
- Live Activities: Introduced in iOS 16.1, these appear on the Lock Screen and in the Dynamic Island, and they can update frequently (e.g., sports scores, ride progress). They are more interactive and can show a progress bar.
- In-App Views: If the information is only relevant while the app is open, just display it in your main interface.
- Notifications: For time-sensitive updates, push notifications can deliver information directly to the user.
- App Clips: If you need to provide a small part of your app's functionality without installing the full app, an App Clip might be more appropriate.
Each of these has different capabilities and limitations, so choose based on what your users need.
Frequently Asked Questions
Can a widget extension access the internet?
Yes, a widget extension can make network requests, but only when the system asks for a timeline. You cannot force a network call at a specific time; the system decides when to refresh. To avoid delays, you can pre-fetch data and store it in a shared container, or use a background fetch in your main app to update the data.
How do I share data between my app and widget?
Use App Groups. In Xcode, add an App Group capability to both your app target and the widget extension target, then use UserDefaults(suiteName:) or write files to the shared container. This allows both processes to read and write the same data.
Why isn't my widget updating?
Check that your timeline provider returns entries with future dates. If all entries are in the past, the system will not display them. Also, ensure you call WidgetCenter.shared.reloadTimelines(ofKind:) when your data changes, and remember that the system may defer updates to save battery. If the widget is on the Lock Screen, it may update less frequently.
Can I have multiple widgets of the same type?
Yes, users can add multiple copies of the same widget to their Home Screen. Each copy will display the same timeline unless you use a custom intent to allow configuration. For configurable widgets, you can use a ConfigurationIntent to let users pick different data sets.
Do widgets work on iPad and Mac?
Yes, widgets are available on iPadOS (since iOS 14) and macOS (since macOS 11). The same extension code works across platforms, but you may need to adjust the layout for different screen sizes. On Mac, widgets appear in the Notification Center.