Building Widgets Using WidgetKit and SwiftUI
At a glance
| Item | Summary |
|---|---|
| Purpose | Create widgets to show your app’s content on the Home screen, with custom intents for user-customizable settings. |
| App architecture | A Swift sample bundle with entry-bearing project variants Game Status Final, Game Status Part 2, Game Status Part 3, Shared Sources, each leading to WidgetKit APIs. |
| Main patterns | No named application pattern supported by the extracted structure |
| Project style | 17 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
Project structure
Source bundle/
├── Game Status Final/
│ ├── EmojiRangerWidget/
│ │ ├── LeaderboardWidget.swift
│ │ └── EmojiRangerWidget.swift
│ └── EmojiRangerIntentHandler/
│ └── IntentHandler.swift
├── Game Status Part 2/
│ └── EmojiRangerWidget/
│ └── EmojiRangerWidget.swift
├── Game Status Part 3/
│ └── EmojiRangerWidget/
│ ├── LeaderboardWidget.swift
│ └── EmojiRangerWidget.swift
└── Shared Sources/
├── App/
│ ├── EmojiRangers.swift
│ ├── ContentView.swift
│ └── DetailView.swift
└── Views/
├── AvatarView.swift
├── AllCharactersView.swift
└── CharacterNameView.swift
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: Swift.
- The verified tree contains 31 project/configuration file(s) and 40 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["Game Status Final"]
V2["Game Status Part 2"]
V3["Game Status Part 3"]
V4["Shared Sources"]
Boundary["WidgetKit APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Bundle --> V3
V3 --> Boundary
Bundle --> V4
V4 --> Boundary
Reference code
Game Status Final/EmojiRangerWidget/LeaderboardWidget.swift:92 — architecture anchor
@main
struct EmojiRangerBundle: WidgetBundle {
@WidgetBundleBuilder
var body: some Widget {
EmojiRangerWidget()
LeaderboardWidget()
}
}Interpretation
The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.
Ownership and state
classDiagram
LeaderboardEntry *-- Date : date
LeaderboardEntry *-- Array : characters
LeaderboardWidgetEntryView o-- Entry : entry
LeaderboardWidget *-- String : kind
Ownership evidence
Game Status Final/EmojiRangerWidget/LeaderboardWidget.swift:40 — stored dependency or nearest verified ownership anchor
struct LeaderboardEntry: TimelineEntry {
public let date: Date
var characters: [CharacterDetail]?
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
LeaderboardEntry |
Date (date) |
owns value state | Initialized by the owner; the binding is immutable |
LeaderboardEntry |
Array (characters) |
owns value state | App/module collaborators |
LeaderboardWidgetEntryView |
Entry (entry) |
stores or receives | App/module collaborators |
LeaderboardWidget |
String (kind) |
owns value state | Initialized by the owner; the binding is immutable |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Class and protocol design
Game Status Final/EmojiRangerWidget/LeaderboardWidget.swift:11 — representative type boundary
struct LeaderboardProvider: TimelineProvider {
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
LeaderboardProvider |
Supplies a capability or framework resource | TimelineProvider |
LeaderboardPlaceholderView |
User-interface presentation and input forwarding | View |
LeaderboardWidgetEntryView |
User-interface presentation and input forwarding | View |
Provider |
Supplies a capability or framework resource | TimelineProvider |
PlaceholderView |
User-interface presentation and input forwarding | View |
EmojiRangerWidgetEntryView |
User-interface presentation and input forwarding | View |
LeaderboardProvider |
Supplies a capability or framework resource | TimelineProvider |
LeaderboardPlaceholderView |
User-interface presentation and input forwarding | View |
LeaderboardWidgetEntryView |
User-interface presentation and input forwarding | View |
Provider |
Supplies a capability or framework resource | IntentTimelineProvider |
No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
date (Game Status Final/EmojiRangerWidget/EmojiRangerWidget.swift:57) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
kind (Game Status Final/EmojiRangerWidget/EmojiRangerWidget.swift:101) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
EmojiRangerWidget.body (Game Status Final/EmojiRangerWidget/EmojiRangerWidget.swift:103) |
public on an internal type |
Implements the Widget.body witness, but the internal EmojiRangerWidget type caps its effective visibility at the target boundary. |
Mirrors the framework conformance surface without creating a public widget API. |
date (Game Status Final/EmojiRangerWidget/LeaderboardWidget.swift:40) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
Reference code
Game Status Final/EmojiRangerWidget/EmojiRangerWidget.swift:100 — internal conforming type with an explicitly public witness
struct EmojiRangerWidget: Widget {
private let kind: String = "EmojiRangerWidget"
public var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: DynamicCharacterSelectionIntent.self, provider: Provider()) { entry in
EmojiRangerWidgetEntryView(entry: entry)
}
.configurationDisplayName("Ranger Detail")
.description("See your favorite ranger.")
.supportedFamilies([.systemSmall, .systemMedium])
}
}Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Handles callbacks or feature events | IntentHandler |
The source’s Handler suffix makes this role explicit. |
| Supplies a capability or framework resource | LeaderboardProvider, Provider |
The source’s Provider suffix makes this role explicit. |
| User-interface presentation and input forwarding | AllCharactersView, AvatarView, CharacterNameView, ContentView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| No named application pattern | Game Status Final/EmojiRangerWidget/LeaderboardWidget.swift:92 |
The verified source directly composes concrete framework types; this document avoids forcing a pattern name. |
Naming conventions
- Types: Handler: IntentHandler; Provider: LeaderboardProvider, Provider; View: AllCharactersView, AvatarView, CharacterNameView, ContentView, DetailView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
placeholder,getSnapshot,getTimeline,character. - Files:
Game Status Final/EmojiRangerWidget/LeaderboardWidget.swift,Game Status Part 2/EmojiRangerWidget/EmojiRangerWidget.swift,Game Status Part 3/EmojiRangerWidget/LeaderboardWidget.swift,Shared Sources/App/EmojiRangers.swift,Game Status Final/EmojiRangerWidget/EmojiRangerWidget.swift,Game Status Part 3/EmojiRangerWidget/EmojiRangerWidget.swift.
Architecture takeaways
LeaderboardWidgetis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, WidgetKit, Intents through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- The source does not justify labeling the design protocol-oriented.
Source map
| Source file | Relevant symbols |
|---|---|
Game Status Final/EmojiRangerWidget/LeaderboardWidget.swift |
LeaderboardProvider, LeaderboardEntry, LeaderboardPlaceholderView, LeaderboardWidgetEntryView, LeaderboardWidget, EmojiRangerBundle |
Game Status Part 2/EmojiRangerWidget/EmojiRangerWidget.swift |
Provider, SimpleEntry, PlaceholderView, EmojiRangerWidgetEntryView, EmojiRangerWidget |
Game Status Part 3/EmojiRangerWidget/LeaderboardWidget.swift |
LeaderboardProvider, LeaderboardEntry, LeaderboardPlaceholderView, LeaderboardWidgetEntryView, LeaderboardWidget |
Shared Sources/App/EmojiRangers.swift |
EmojiRangers |
Game Status Final/EmojiRangerWidget/EmojiRangerWidget.swift |
Provider, SimpleEntry, PlaceholderView, EmojiRangerWidgetEntryView, EmojiRangerWidget |
Game Status Part 3/EmojiRangerWidget/EmojiRangerWidget.swift |
Provider, SimpleEntry, PlaceholderView, EmojiRangerWidgetEntryView, EmojiRangerWidget |
Shared Sources/App/ContentView.swift |
ContentView, TableRow |
Shared Sources/Views/AvatarView.swift |
Avatar, AvatarView |
Shared Sources/App/DetailView.swift |
DetailView |
Shared Sources/Views/AllCharactersView.swift |
AllCharactersView |