Setting up iOS 16 Live Activities on App that support iOS 15 and below

Jefferson Setiawan
2 min readAug 2, 2022

--

Apple just release the beta of Live Activities together with the release of Xcode 14 beta 4.

Apple new Live Activities (image by Apple)

Live Activities is like a widget that live in Lock screen and notification center.

It will show you the realtime data from your favorite feature in your app such as tracking food order, the progress of music, etc.

Live activities have different data update mechanism. It can’t request data nor have timeline for updating the data. Instead, it can be updated via push notification or programmatically called update on that activity instance.

Problem

When trying this feature, it works well on my new demo app.

But the problem arise when I try to implement it to the app that still support iOS < 16, how we can add that Live activities in the WidgetBundle builder?

’FoodLiveActivities’ is only available in application extensions for iOS 16 or newer

I’ve tried using if #available(iOSApplicationExtension 16, *) {...} but there spawn another error

Closure containing control flow statement cannot be used with result builder 'WidgetBundleBuilder'

Trying the other tricks using #if canImport(ActivityKit) still no luck because at the end we need to wrap the Widget into the if available iOSApplicationExtension 16, as it needs ActivityConfiguration that only exists in iOS 16.

After several hours of researching, I found an idea by just creating a new extension target that only support iOS 16. By doing so, there is no need to add if iOS check again. And it works flawlessly.

You can check the code in Github sample project.

--

--