iOS 16 beta 4 is the primary SDK launch that helps Dwell Actions. A Dwell Exercise is a widget-like view an app can place in your lock display screen and replace in actual time. Examples the place this may be helpful embody stay sports activities scores or practice departure instances.
These are my notes on enjoying with the API and implementing my first Dwell Exercise (which has a barely atypical use case, however that shouldn’t matter).
A motorcycle pc in your lock display screen
My Dwell Exercise is a show for a motorcycle pc that I’ve been creating with a gaggle a mates. Right here’s a video of it in motion:
And right here with simulated knowledge within the simulator:
I haven’t talked a lot about our bike pc venture publicly but; that may hopefully change sometime. Briefly, a gaggle of mates and I designed slightly field that connects to your bike’s hub dynamo, measures velocity and distance, and sends the information by way of Bluetooth to an iOS app. The app information all of your rides and may act as a stay speedometer when mounted in your bike’s handlebar. It’s this final characteristic that I wished to copy within the Dwell Exercise.
Comply with Apple’s information
Including a Dwell Exercise to the app wasn’t arduous. I discovered Apple’s information Displaying stay knowledge on the Lock Display screen with Dwell Actions simple to observe and fairly complete.
No specific consumer approval
iOS doesn’t ask the consumer for approval when an app desires to indicate a Dwell Exercise. I discovered this odd because it appears to ask builders to abuse the characteristic, however possibly it’s OK due to the foreground requirement (see under). Customers can dismiss an energetic Dwell Exercise from the lock display screen by swiping (like a notification).
Most apps will most likely must ask the consumer for notification permissions to replace their Dwell Actions.
The app have to be within the foreground to begin an exercise
To begin a Dwell Exercise, an app have to be open within the foreground. This isn’t supreme for the bike pc as a result of the speedometer can’t seem magically on the lock display screen when the consumer begins using (despite the fact that iOS wakes up the app within the background at this level to ship the Bluetooth occasions from the bike). The consumer has to open the app manually at the very least as soon as.
Alternatively, this limitation might not be a difficulty for many use instances and can most likely minimize down on spamming/abuse considerably.
The app should hold operating within the background to replace the exercise (or use push notifications)
So long as the app retains operating (within the foreground or background), it may replace the Dwell Exercise with a single technique name. That is supreme for the bike pc because the app retains operating within the background processing Bluetooth occasions whereas the bike is in movement. I assume the identical applies to different apps that may stay alive within the background, similar to audio gamers or navigation apps doing steady location monitoring.
Updating the Dwell Exercise as soon as per second was no drawback in my testing, and I didn’t expertise any price limiting.
Most apps get suspended within the background; they need to use push notifications to replace their Dwell Exercise (or background duties or another mechanism to have the system wake you up). Apple launched a brand new form of push notification that’s delivered on to the Dwell Exercise, bypassing the app altogether. I haven’t performed with push notification updates, so I don’t know the advantages of utilizing this technique over sending a silent push notification to wake the app and updating the Dwell Exercise from there. In all probability much less aggressive price limiting?
Lock display screen shade matching
I haven’t discovered a great way to match my Dwell Exercise’s colours to the present system colours on the lock display screen. By default, textual content in a Dwell Exercise is black in mild mode, whereas the built-in lock display screen themes appear to favor white or different mild textual content colours. If there’s an API or atmosphere worth that enables apps to match the colour type of the present lock display screen, I haven’t discovered it. I experimented with varied foreground kinds, similar to supplies, with out success.
I ended up hardcoding the foreground shade, however I’m not happy with the end result. Relying on the consumer’s lock display screen theme, the Dwell Exercise can look misplaced.
Animations can’t be disabled
Apple’s information clearly states that builders have little management over animations in a Dwell Exercise:
Animate content material updates
Whenever you outline the consumer interface of your Dwell Exercise, the system ignores any animation modifiers — for instance,
withAnimation(_:_:)
andanimation(_:worth:)
— and makes use of the system’s animation timing as a substitute. Nonetheless, the system performs some animation when the dynamic content material of the Dwell Exercise adjustments. Textual content views animate content material adjustments with blurred content material transitions, and the system animates content material transitions for photographs and SF Symbols. In the event you add or take away views from the consumer interface based mostly on content material or state adjustments, views fade out and in. Use the next view transitions to configure these built-in transitions:opacity
,transfer(edge:)
,slide
,push(from:)
, or mixtures of them. Moreover, request animations for timer textual content withnumericText(countsDown:)
.
It makes whole sense to me that Apple doesn’t need builders to go loopy with animations on the lock display screen, and maybe having full management over animations additionally makes it simpler for Apple to combine Dwell Actions into the always-on show that’s most likely approaching the subsequent iPhone.
What stunned me is that I couldn’t discover a option to disable the textual content change animations altogether. I discover the blurred textual content transitions for the massive velocity worth fairly distracting and I believe this label would look higher with none animations. However no mixture of .animation(nil)
, .contentTransition(.id)
, and .transition(.id)
would do that.
A Dwell Exercise may be very very similar to a widget: the UI should stay in your app’s widget extension. You begin the Dwell Exercise with code that runs in your app, although. Each targets (the app and the widget extension) want entry to a standard knowledge sort that represents the information the widget shows. It is best to have a 3rd goal (a framework or SwiftPM package deal) that comprises such shared varieties and APIs and that the downstream targets import.
Availability annotations
WidgetBundle
apparently doesn’t help widgets with totally different minimal deployment targets. In case your widget extension has a deployment goal of iOS 14 or 15 for an present widget and also you now wish to add a Dwell Exercise, I’d count on your widget bundle to seem like this:
@fundamental
struct MyWidgets: WidgetBundle {
var physique: some Widget {
MyNormalWidget()
// Error: Closure containing management movement assertion can't
// be used with end result builder 'WidgetBundleBuilder'
if #obtainable(iOSApplicationExtension 16.0, *) {
MyLiveActivityWidget()
}
}
}
However this doesn’t compile as a result of the end result builder sort utilized by WidgetBundle
doesn’t help conditionals. I hope Apple fixes this.
This wasn’t an issue for me as a result of our app didn’t have any widgets till now, so I simply set the deployment goal of the widget extension to iOS 16.0. You probably have present widgets and may’t require iOS 16 but, a workaround is so as to add a second widget extension goal only for the Dwell Exercise. I haven’t tried this, however WidgetKit explicitly helps having a number of widget extensions, so it ought to work:
Sometimes, you embody all of your widgets in a single widget extension, though your app can include a number of extensions.