Over the Christmas break, I usually take time off to reflect on the past year and year to come, spend time with family and friends, and take on a project I put off until I have more time to focus. That project in 2022 was finally adding Widgets to GodTalk.
I know I’m late to the widget game and missed out on all the “check out these 15 apps that support Widgets” articles posted when iOS 14 released, but better late than never right? Plus, it was a chance to learn something new and add more ways to experience content within the app.
What surprised me was that the toughest part wasn’t implementing the framework but updating some of the app’s design and infrastructure to enable Widgets.
Here are a few things I learned:
Accent Color Update
After watching the WWDC videos on Widgets and getting an understanding of their intended use, I started with designing widgets for GodTalk. For the most part I used the same design concepts from the app to design the widgets. The Canvas view for SwiftUI has a mode that allows you to see the widget in different aspects, side by side. One of the modes was Color Scheme Variants, which lets you see the widget in Dark Mode and Light Mode at the same time.
This highlighted a problem I didn’t know I had, the app’s Accent Color in Dark Mode was 😍 but in Light Mode it was 😕. If I thought about seeing the color schemes side by side, this wouldn’t have been a problem.
Migrating Core Data Model to App Group
Up until this point, I had only heard of others implementing a shared Core Data model and persistent container with App Extensions, this was my first need for this. I was going down the wrooooong way for a couple days before I found an article from SwiftLee and shockingly a thread on the Apple Developers forum that put me on to what really I needed. Let’s just say there ain’t no need to create a static framework add the Core Data Model to a static framework 🤦🏾♂️.
After adding the App Group capability (in Signing & Capabilities) and adding the App Group identifier (in Certificates, Identifiers, & Profiles), I was able to do a Lightweight migration of the data model to a new persistent store in the App Group. Then on startup check to see if the shared persistent store exists before using it.
WidgetKit
Also while designing, I wanted to change the background color of the widget. Well, WidgetBackground is not what you think it is for. This article from the impressive Use Your Loaf helped me realize I just needed to use a ZStack with Color as the first/bottom view.
Deep Linking using SceneDelegate
Up until now the app did not support Deep Linking and since the the app was built using SwiftUI’s ScenDelegate, it was quite different from my jobby job’s way of handling deep links.
The goal was to take the user to the specific verse in the app when a widget was tapped. To make this happen I had to wrap the widget in a Link SwiftUI control and make the destination unique to the verse’s identifier. Then I had to add a URL Type for the app’s target and finally resolve deep links by using an @EnvironmentObject to pass the identifier down to the specific verse view. These articles from Fernando Moya de Rivas, Donny Wals, and Alexey Naumov were instrumental in helping me understand this process. Then I added a ScrollViewReader to animate moving to the specific verse within the collection.
