With the release of iOS 17, Apple has introduced a game-changing feature that’s set to revolutionize the way we interact with our devices — interactive widgets.
These widgets, gracing your home screen, open up a world of endless possibilities, bringing your favorite apps and features right to your fingertips.
1) Add the Widget target
Set the deployment version for the app to iOS 17. Select the app target, and tap the “+” to add the widget target
Filter the extensions by name and search for “widget”. Select the Widget Extension and tap next.
2) Configure AppGroups
Select the Widget target, click the Capabilities button, and select App Groups. Tap the “+” to create an App Group and give it a name. Do the same step for the TodoList app target, and re-use the same AppGroup.
3) Update Target Membership
Select the Mode.xcdatamodeld and make sure that the TodoWidgetExtension target is selected.
Do the same thing for the DataManager.swift and TodoModel.swift files.
This will allow the app and the widget to share the same files.
4) Update your DataManager file
Select the DataManager Swift file, and update the init function
This will allow CoreData to be shared with the widget via the App Group.
Create the `fetchAllItems` and `updateStatus` functions, that will allow the Widget to fetch CoreData Todo list items and update a task as complete. See the final version of the DataManager file.
5) Add the AppIntent for the iOS 17 Widget Interactivity
Create a new file under the TodoWidget folder, and set the Target Membership to both, the app and the widget.
This must be a regular swift file, which will allow the interactivity for the widget to take place.
6) Update the iOS 17 Widget UI
Select the TodoWidget.swift file and look for TodoWidgetEntryView. You may have a different name, but it’s the struct for your widget UI. Update this to include the Todo items in a VStack, where each item will be using a Button with intent.
7) Build and Run
That’s it! Now you have an iOS 17 Interactive Widget for the Home Screen, using CoreData for storing/updating Todo List items.
Q) When I mark a task as completed, why the widget doesn’t update?
A) Whenever you work with Widgets that are sharing data with the app, make sure that you have configured the App Groups correctly for the app and widget target.
Q) Where can I get the full source code for this tutorial?
A) We always encourage our customers and visitors to follow the tutorial first, so they can learn what each step has to offer and how the code works. Here is the download link to the full source code, with all the changes from this tutorial.