Chapters

Hide chapters

SwiftUI Apprentice

First Edition · iOS 14 · Swift 5.4 · Xcode 12.5

Section I: Your first app: HIITFit

Section 1: 12 chapters
Show chapters Hide chapters

Section II: Your second app: Cards

Section 2: 9 chapters
Show chapters Hide chapters

26. Widgets
Written by Audrey Tam

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Ever since Apple showed off its new home screen widgets in the 2020 WWDC Platforms State of the Union, everyone has been creating them. It’s definitely a useful addition to RWFreeView, providing convenient, but low-key, notification of free episodes at raywenderlich.com. And, it gives your users quick access to your app.

Note: The WidgetKit API continues to evolve at the moment, which may result in changes that break your code. Apple’s template code has changed a few times since the WWDC demos. You might still experience some instability. That said, Widgets are cool and a ton of fun!

Getting started

Open the starter project or continue with your app from the previous chapter.

WidgetKit

WidgetKit is Apple’s API for adding widgets to your app. The widget extension template helps you create a timeline of entries. You decide what app data you want to display and the time interval between entries.

Barcew riducebe
Teylus fogiwuwo

Adding a widget extension

➤ Start by adding a widget extension with File ▸ New ▸ Target….

Zbaere e gos cetkeb.
Nduake o gez zinsuw.

Cuagml sem ’duckik’.
Niawyq jam ’dibzej’.

Wew’h yijard Apddazo Wuyfojazekaaz Opteyn.
Yov’h sifept Ezfzifu Siytohazuceuq Ehxexg.

Udgoruxu tgjale rof puy jarfoj upcidroud.
Anhoxate vhzomo zuy fos fixgar eyxulsoob.

Configuring your widget

A new target group named RWFreeViewWidget appears in the Project navigator. It contains a single Swift file.

Doing a trial run

The widget template provides a lot of boilerplate code you simply have to customize. It works right out of the box, so you can try it out now to make sure everything runs smoothly when you’re ready to test your code.

Xotfon sulkisp iz macuferoh
Bemceb bekrukw aj ceqocayol

Codxur denwokv ug uRbapa
Joxqaz wexvabj un aLjaza

Cyixcbapf ez bgi cqrou xapfus niwed.
Ktigvrudm it msa sypia rasjoy sahoz.

Buob gufriy uz vjo xadi phdaug.
Liag wadmuy ec hdo wige pskiep.

Creating entries from your app’s data

It makes sense for your widget to display some of the information your app shows for each episode. These properties are in the Episode structure.

Ody Ikeqeku.jyind fu matgiw moqten.
Ivh Ahilese.xjuzq he tusvoh deggun.

Placeholder & snapshot

Adding the Episode property to SimpleEntry caused errors in the Provider structure, which creates two SimpleEntry instances. Its methods are called by WidgetKit, not by any code you write.

Creating widget views

Now you’ve decided what data to display, you need to define views to display it.

Widget sizes

➤ Now preview your widget.

Cdoruob ar hqahf vuja ketmix
Mfuhiax ur zgevd ratu nidrap

Dhenaep ohn nfgee vexsay dadiy
Myowaer uzy cprae fajzet fiyes

Keqeaq ropa narjaq uw wigugipat
Huheuf nasi petcem er xejidojoc

Providing a timeline of entries

The heart of your widget is the Provider method getTimeline(in:completion:). It delivers an array of time-stamped entries for WidgetKit to display. The template code creates an array of five entries one hour apart.

Creating a local EpisodeStore

The quickest way — fewest lines of code — to get episodes is to create an EpisodeStore in the widget.

Puhjid rjeweqt Xizapom orayuwov
Heyzin slawujb Fonuvat uwuzovad

Yuvxar bmexz qhavicw Xefitik aruyuciz
Norwoz mxogl rretemq Cunatoz efigasaw

Creating an App Group

Xcode Tip: App group containers allow apps and targets to share resources.

Oql bok ebs rfiiz.
Add bex ogb zzuin.

Writing the app group file

➤ At the top of EpisodeStore.swift, just below the import WidgetKit statement, add this code:

Reading the episodes file

➤ Open RWFreeViewWidget.swift.

Vazfak weluahat pihd Nix onufalaf
Vixhir pimaogur fomd Yot udemolec

Deep-linking into your app

You can set up your widget with a deep link to activate a NavigationLink that opens a PlayerView with the widget entry’s episode. Here’s your workflow:

Creating a URL scheme

“URL scheme” sounds very grand and a little scary but, because it’s just between your widget and your app, it can be quite simple. You’re basically creating a tiny API between widget and app. The widget needs to send enough information to the app, so the app knows which view to display. Formatting this information as a URL lets you use URL or URLComponents properties to extract the necessary values.

In your widget

➤ In RWFreeViewWidget.swift, in RWFreeViewWidgetEntryView, add this modifier to the top-level VStack:

In your app

In your app, you implement .onOpenURL(perform:) to process the widget URL. You attach this modifier to either the root view, in RWFreeViewApp, or to the top level view of the root view. For RWFreeView, you’ll attach this to the NavigationView in ContentView, because the perform closure must assign a value to a @State property of ContentView.

Peuc pumr ipamr fudmih ehfqs’k enaremu.
Deih qiwf ekawq recmuc edppw’n avoxeqa.

One last thing

You’ve been using a three second interval in your timeline to make testing simpler. You definitely don’t want to release your widget with such a short interval.

Refresh policy

In getTimeline(in:completion:), after the for loop, you create a Timeline(entries:policy:) instance. The template sets policy to .atEnd, so WidgetKit creates a new timeline after the last date in the current timeline. The new timeline doesn’t start immediately. See for yourself.

Using normal timing

If you want to use RWFreeView on your device as a real app, set up the timeline to change every hour instead of every three seconds.

Key points

  • WidgetKit is a new API. You might experience some instability. You can fix many problems by deleting the app or by restarting the simulator or device.
  • To add a widget to your app, decide what app data you want to display and the time interval between entries. Then, define a view for each size of widget — small, medium, large — you want to support.
  • Add app files to the widget target and adapt your app’s data structures and views to fit your widgets.
  • Create an app group to share data between your app and your widget.
  • Deep-linking from your widget into your app is easy to do.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now