How to implement a lock screen widget for our App

How to implement a lock screen widget for our App

One of the most requested features of iOS is a customizable lock screen. Finally, it is available in the latest release of iOS 16. We can populate the lock screen with browsable widgets. Implementing lock screen widgets is easy because its API shares the same code as home screen widgets. This week we will learn how to implement lock screen widgets for our app.

Let's start with the app home screen widget code you probably already have.

 struct WidgetView : View {
let entry : Entry
@Environment ( \ .widgetFamily ) private var family
var body : some View {
switch family {
case .systemSmall :
SmallWidgetView ( entry : entry )
case .systemMedium :
MediumWidgetView ( entry : entry )
case .systemLarge , .systemExtraLarge :
LargeWidgetView ( entry : entry )
default :
EmptyView ( )
}
}
}

In the example above, we have a typical view that defines a widget. We use the Environment to know the widget family and display the appropriate size. All we need to do is remove the default statements and implement all the new use cases that define lock screen widgets.

 struct WidgetView : View {
let entry : Entry

@Environment ( \ .widgetFamily ) private var family

var body : some View {
switch family {
case .systemSmall :
SmallWidgetView ( entry : entry )
case .systemMedium :
MediumWidgetView ( entry : entry )
case .systemLarge , .systemExtraLarge :
LargeWidgetView ( entry : entry )
case .accessoryCircular :
Gauge ( value : entry .goal ) {
Text ( verbatim : entry .label )
}
.gaugeStyle ( .accessoryCircularCapacity )
case .accessoryInline :
Text ( verbatim : entry .label )
case .accessoryRectangular :
VStack ( alignment : .leading ) {
Text ( verbatim : entry .label )
Text ( entry .date , format : .dateTime )
}
default :
EmptyView ( )
}
}
}

It's good to remember that the system uses different rendering modes for lock screen and home screen widgets. The system provides us with three different rendering modes.

  1. Home screen widgets and Watch OS support full color mode. Yes, starting with watchOS 9, you can also use WidgetKit to implement watchOS complications.
  2. Vibrant mode means that the system restores text, images, and instruments to monochrome and correctly colors the lock screen background.
  3. The accented mode is only used on watchOS, where the system divides the widgets into two groups, default and accented. The system colors the accented portion of the widget using the tint color selected by the user in the watch face settings.

Rendering modes are available through SwiftUI Environment variables, so you can always check which rendering mode is active and reflect it in your design. For example, you can use different images with different rendering modes.

 struct WidgetView : View {
let entry : Entry
@Environment ( \ . widgetRenderingMode ) private var renderingMode
var body : some View {
switch renderingMode {
case . accented :
AccentedWidgetView ( entry : entry )
case . fullColor :
FullColorWidgetView ( entry : entry )
case . vibrant :
VibrantWidgetView ( entry : entry )
default :
EmptyView ()
}
}
}

As shown above, we use the widgetRenderingMode environment value to get the actual rendering mode and exhibit different behaviors. As mentioned before, in accented mode, the system divides the widget into two parts and applies special tinting to them. You can use the widgetAccentable view modifier to mark part of the view hierarchy. In this case, the system will know which views to apply the tinting color.

 struct AccentedWidgetView : View {
let entry : Entry
var body : some View {
HStack {
Image ( systemName : "moon" )
.widgetAccentable ( )
Text ( verbatim : entry .label )
}
}
}

Finally, we need to configure the support type for the widget.

 @main
struct MyAppWidget : Widget {
let kind : String = "Widget"

var body : some WidgetConfiguration {
StaticConfiguration ( kind : kind , provider : Provider ( ) ) { entry in
WidgetView ( entry : entry )
}
.configurationDisplayName ( "My app widget" )
.supportedFamilies (
[
.systemSmall ,
.systemMedium ,
.systemLarge ,
.systemExtraLarge ,
.accessoryInline ,
.accessoryCircular ,
.accessoryRectangular
]
)
}
}

If you’re still supporting iOS 15, you can check the availability of the new lock screen widgets.

 @main
struct MyAppWidget : Widget {
let kind : String = "Widget"

private var supportedFamilies : [ WidgetFamily ] {
if #available ( iOSApplicationExtension 16.0 , * ) {
return [
.systemSmall ,
.systemMedium ,
.systemLarge ,
.accessoryCircular ,
.accessoryRectangular ,
.accessoryInline
]
} else {
return [
.systemSmall ,
.systemMedium ,
.systemLarge
]
}
}

var body : some WidgetConfiguration {
StaticConfiguration ( kind : kind , provider : Provider ( ) ) { entry in
WidgetView ( entry : entry )
}
.configurationDisplayName ( "My app widget" )
.supportedFamilies ( supportedFamilies )
}
}

<<:  Speed ​​and security are both available! Transforming asynchronous layout greatly improves client layout performance

>>:  Apple iOS 16.2 / iPadOS 16.2 Developer Preview Beta Released: New Borderless Notes App

Recommend

Bemisia tabaci: How the world's only "super pest" came to be

Bemisia whitefly is recognized as the world's...

Is focus the only way to make money? What to do to make money?

I've been feeling a bit complicated lately, b...

Danlel Le·Script Killing Creation Writing and Cashing Camp Video

Danlel Le·Script Killing Creation Writing and Cas...

Cook's naked donation: Apple embraces the world

Apple's current market value is $720 billion ...

Can you really avoid nutritional deficiencies by taking supplements alone?

The pace of life of modern people is indeed getti...

Deconstruction: The logic behind the design of Toutiao

The emergence of Toutiao has subverted traditiona...

The Perseid meteor shower is coming! Why do meteors have different colors?

Produced by: Science Popularization China Author:...

Is it true that the more Wenchang Towers there are, the better?

Which floor is best for Wenchang Tower? Which flo...