For this tutorial, we will be using the source code from our 'Add Core Data to SwiftUI' tutorial.
Download our sample project from here, so you can follow these steps.


See All Source Codes

For 10% OFF - use coupon code ZWV-YJN-TVZ




1) Add the Widget target
Set the deployment version for the app to iOS 17. Select the app target, 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.


You have to add the lines 15 to 17



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.


If you don't have a struct called `TodoWidgetEntryView` then you can simply replace all your struct's code with lines 2 to 15.



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.




FAQ:


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.





If you have any questions or suggestions, please feel free to send an email to support@apps4world.com
Thanks for reading!