Introduction: The development of widgets is the same as the logic of App development under normal circumstances, which is divided into network request, data model,view and rendering. It’s just that widget development is written in the swiftUI language, so be aware of the swiftUI space.

Good! Let’s start our widget development.

First, create the Widget Extension

Then select the certificate and give it a name. A folder will then appear at the bottom of the list of files on the left

This is our team piece! Now you can run it directly. The system displays a time View by default

Then we go through the internal structure of the widget one by one. First find the program execution entry

@main: represents the main entrance to the Widget from which the system loads

Kind: is the unique identifier of the Widget

StaticConfiguration: Initialization configuration code

ConfigurationDisplayName: Adds a title to the edit screen display

Description: Adds the description displayed on the editing interface

SupportedFamilies can limit which of the three styles to provide

And then we move on to the next Provider

Provider: a structure that provides all necessary information for widget display and implements the TimelineProvider protocol

Placeholder: Provides a default view that can be displayed when a network data request fails or some other exception occurs

GetSnapshot: To display widgets in the widget library, WidgetKit requires the provider to provide a preview snapshot that can be seen on the component’s Add page

GetTimeline: In this method, network requests can be made, and the data obtained is saved in the corresponding entry, called

After completion, the widget will be refreshed

And the thing inside the red circle, which we’ll talk about in a moment, is to hold the data, and the reason it’s closed is because I dropped this hole. Oh, my God!

Let’s start with the Poetry object: this is the data model object we’re asking for

After looking at the model, it’s time for the data request

Now we go back to the getTimeline method in the PoetryProvider:

We’re going to record when the data is first loaded, and then we’re going to have the widget periodically refresh the data based on that point in time. And just for the record,It is important to note the format of the data returned by each method!!That’s why I dropped it so many times in the red circle.Ok, so once we get to this point, our data request comes down, and the time is fixed, but there’s no place to put it, where the data is storedPoetryEntry This is also a data Model, but this is the Model that the system needs. It has time points and data, but you can also add additional fields that you need.

So we get the data and then we have the ViewThe space inside is SwiftUI space. I need to know about this, but I am not very familiar with it.

So what we’re going to do is we’re going to send the data, the view, the timeline back, back to our entry.

The first widget is complete, let’s run it!

Refer to the article: www.jianshu.com/p/55dce7a52…