Take your app on a most wondrous adventure to the home and Today screens of iPhone, iPad, and Mac. Grab the starter project and code along with us! We will guide you through the process of creating a widget for your app from start to finish so that you can provide people with beautiful views and glanceable information in an easily-accessible place. Discover how to create a widget project, learn fundamental concepts for widgets and their structure, configure the widget and its provider, and start exploring timeline concepts.
Once you're finished, travel onward with us to part two of the Widgets Code-along where we'll learn more about timelines, system intelligence, and configuration.
♪ Hello and welcome to WWDC. Izzy Fraimow: Hi, I'm Izzy. I'm an engineer on the iOS System Experience team. Thanks for joining me. Let's take a look at what we will be covering today. This is a code-along, a new format for WWDC 2020. We'll go over what that means and how you can participate, we'll cover the core concept of what a widget is, and then we'll build our first widget together. I'm excited -- let's get started! This session is a code-along. There's a project available on the developer portal that we'll start with and we will build a widget for it from scratch together. I invite you to start that download now, and while that's going, we'll cover a couple of concepts. What is a widget at its core? A widget is just a SwiftUI view. SwiftUI, so it's the same declarative code that generates gorgeous native views on iOS, iPadOS, and macOS. And it's a SwiftUI view that updates over time. Exactly how and when it updates is what we'll be looking at in this code-along. So this is the downloaded project. We're in the first part of our code-along, so let's open that project. Before we get in the widget, let's build and run this to see what we have.
This is the Emoji Rangers app. It lets us keep track of our favorite Emoji Rangers. You can see we have Power Panda, Spouty, and Egghead. Let's look at their details. Here we see that our Emoji Rangers are saving the world from waves of attacks. Power Panda is recharging right now but will be ready soon. Now I've seen the keynote, I've watched Welcome to WidgetKit, and I'd really like a widget for this game. It looks like we have the perfect view for it already. The status square in the top left looks ideal for a widget, so let's make it one. Let's check out how this view works in code first. Here's our SwiftUI AvatarView. If you're new to SwiftUI, widgets are a perfect place to get started. You can see we have just a few lines of code to generate the widget we want. And because we're expressing the layout semantically with stacks, SwiftUI knows how to set the padding just right for us. You'll also notice on the right hand side, there's a SwiftUI preview which is a live representation or a review that updates as we type. It kind of looks like a widget already. Let's go ahead and make it one for real. First, we need to create a widget target and do that by going to File > New Target and search for widget.
Let's name it EmojiRangerWidget, and finish. Activate our target.
Now that we have our target, I know that we want that view from earlier in it and I'm going to go ahead and add the files that I want to the target.
SwiftUI previews work for widgets as well. Let's take a look. I can click the Create Preview to automatically insert a preview for us and we can use that AvatarView.
In order to see exactly what it'll look like as a widget, we use the WidgetPreviewContext as an argument to the previewContext.
That looks great. It's almost like we already have a widget; making it real is just a matter of filling in some information. We have a display name and a description that are easy to fill in. Now let's look at our main entry view, the EmojiRangerWidgetEntryView. Let's use that AvatarView, which is what we previewed before. Now, it wants me to pass a character in here. So where should that character come from? Well, the EmojiRanger's WidgetEntryView already comes with an entry so let's add it to the entry. And now I can pass it directly into my AvatarView. But where does this entry come from? Entries come from a timeline provider, which is the core engine of a widget. The timeline provider provides snapshots when WidgetKit wants just one entry, like in the widget gallery, and it provides a full timeline when the user has added a configured widget to their device. So here for the snapshot, all we need to do is pass a character. We can pass our character in here. Timeline is used once a user has actually added a widget from the gallery. We don't need a full timeline right now so let's just use one entry again. OK, that's great. Let's build and run and see what we have. So, I built my widget target and it automatically added the widget to my Home screen for me. We have a full widget now, but I noticed some things that I'd like to tweak. First, in the add sheet, our widget has a small, medium, and large size. These work OK, but it's not the best use of space right now and I'm not ready to support those yet. So let's set our supported families.
That's just an extra modifier on our widget configuration.
One other thing you may have noticed is that there's this PlaceholderView in our template. Our widget is so fast that we can't actually see it, but this is what shows up while WidgetKit is waiting on a timeline. We want it to be our AvatarView, and we can preview what that looks like with SwiftUI previews. Let's make it our AvatarView. There's no entry for the PlaceholderView, so we can pass in Panda again. And now in our preview, we can make this a group.
And we can add in our Placeholder. So now we have two views that look the same, but we want our Placeholder to indicate pending content and not our actual content. So, there's new SwiftUI API that makes this super easy, it's just the isPlaceholder modifier. And now you can see in our previews, we have our full widget and a Placeholder with our text automatically replaced with gray rounded rectangles and our images automatically replaced as well. Amazing! Let's take one last look at our new widget. Here's our EmojiRangers app. Now when I go to the widget gallery, I can see the single supported size. I can add it right on my Home screen. Perfect. For insight on how to approach widget design, see the Designing Great Widgets talk, and my teammate Nils is giving a talk about how to make the most of SwiftUI in widgets to really nail those designs. I'd also love for you to join me in the next section of this Code-Along. Thanks for joining me and have a great WWDC.
Looking for something specific? Enter a topic above and jump straight to the good stuff.
An error occurred when submitting your query. Please check your Internet connection and try again.