watchOS

watchOS 4: Frontmost App State

There was certainly a trend to some of the new versions of operating systems coming out this year at WWDC. Like macOS High Sierra (which I still can’t believe is the real name), watchOS 4 feels like another refinement version. It seems as if the team has been hard at work making watchOS even more performant than last year’s step up with watchOS 3. The unified process runtime, where watch apps and extensions are now running on the same process, makes touch latency and performance even better. When it comes to new tools developers can take advantage of, the most compelling thing has to be the new frontmost app state.

The frontmost app is defined as the app that’s on the watch screen when the user drops their wrist. If a user raises their wrist within 2 minutes, they’ll see this same app again (and it’ll become the active app).

What does this mean for us as developers?

This new state allows us to bridge the user experience gap, and engage with and notify users even when they’re not looking directly at the watch app. I’ve talked before about how you can drive user engagement outside of the active app experience for watchOS 3. With watchOS 4, this becomes even easier. You can take advantage of this by using these new options:

Extended wrist-raise time

By default, there is a 2-minute timeout for apps to stay on the screen after the user drops their wrist. Anything past 2 minutes, it’ll go back to the user’s clock face. However, if the user starts an activity or engages with the app in some way where a longer period of time is needed (e.g. they’ve ordered a Lyft and they’re waiting for their car to show up), you can extend the period of time to 8 minutes using this one-liner:

WKExtension.shared().isFrontmostTimeoutExtended = true

Once the user is done with their activity, it is best practice to set this timeout property back to false and resume the 2-minute timeout.

Playing haptics

A surprising miss in the previous versions of watchOS is the ability to tap users when the app isn’t in an active state. You primarily had to rely on notifications to update the user if anything has changed. Now, as the frontmost app, whenever you need to notify users of something, e.g. a status change in their order, you can now play haptics to tap them.

if order.isSuccessful {
    WKInterfaceDevice.current().play(.success)
}

Waking from background to receive notifications

Like the ability for iOS apps to intercept notifications if the app is in the foreground, frontmost apps can also intercept watch notifications and decide to change behavior accordingly. Instead of showing a regular notification, you can now customize this experience. A great example of this would be to pay a haptic to get your user’s attention. When they raise their wrist, you can show an updated app UI with the change that just occurred.

Use the

func userNotificationCenter(_ center: UNUserNotificationCenter, 
                         willPresent notification: UNNotification, 
               withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

function from the UserNotifications framework to intercept the local or remote notification and decide which user experience you’d like to show.

Finishing up tasks (increased runtime priority)

Last in this blog post (and in the watch app life cycle) is the ability to get increased runtime priority to finish up any remaining tasks after a user drops their wrist. You can use ProcessInfo’s function

func performExpiringActivity(withReason reason: String, 
                       using block: @escaping (Bool) -> Void)

to wrap up one last API call or finish writing data to the database. As the frontmost app, the system knows that your app is more important to the user and you are more likely get time to finish up these tasks in the end.

For those interested in learning more, aside from What’s New in watchOS, I’d highly recommend watching The Life of a watchOS App from this year’s WWDC to understand the full lifecycle of a user’s interaction with watchOS apps and how to best take advantage of frontmost app state.

1 thought on “watchOS 4: Frontmost App State”

Leave a Reply

Your email address will not be published. Required fields are marked *