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

How to get more customers smartly?

If we divide the company's customer base base...

How can a startup brand carry out marketing promotion?

This article combines marketing promotion theory ...

What should I do if Taobao store promotion has no effect?

What should I do if the promotion is ineffective?...

How to optimize Weibo advertising creativity? Analysis of 2 advertising cases

This article shares two Weibo advertising optimiz...

Director A talks about workplace power struggles

Director Lao A's course on workplace power st...

Case analysis of programmatic creative delivery of information flow advertising!

This is the best era. With the support of AI, inf...

25 Psychological Skills That Marketers Must Know

Why do marketers need to understand psychology? B...

No idea for APP promotion? One map helps you get all channels! (Super complete)

Everyone working in the Internet industry knows t...

Top 10 New Media Moments of 2016

In 2016, the new media industry is still hot. Som...