Displaying live data with Live Activities

Abdur Rehman
4 min readJul 10, 2023

--

Live Activities display your app’s most current data on the iPhone or iPad Lock Screen and in the Dynamic Island. This allows people to see live information at a glance and perform quick actions that are related to the displayed information.
One of the notable features introduced in iOS 16 is live activity widgets. iOS 16 enables the display of real-time updates and ongoing activities from our apps on the lock screen or in the Dynamic Island of the new iPhone 14 Pro. In this week’s tutorial, we will learn how to develop live activity widgets for our apps using the ActivityKit framework.

Constraints

A Live Activity can be active for up to eight hours unless your app or a person ends it before this limit. After the 8-hour limit, the system automatically ends it. When a Live Activity ends, the system immediately removes it from the Dynamic Island. However, the Live Activity remains on the Lock Screen until a person removes it or for up to four additional hours before the system removes it — whichever comes first. As a result, a Live Activity remains on the Lock Screen for a maximum of twelve hours.

Add Support to Live Activities

To add support for Live Activities to your app:

  1. Create a widget extension if you haven’t added one to your app and make sure “Include Live Activity” is selected when you add a widget extension target to your Xcode project. For more information on creating a widget extension, see WidgetKit and Creating a widget extension.
  2. If your project includes an Info.plist file, add the Supports Live Activities entry to it, and set its Boolean value to YES. Alternatively, open the Info.plist file as source code, add the NSSupportsLiveActivities key, then set the type to Boolean and its value to YES. If your project doesn’t have an Info.plist file, add the Supports Live Activities entry to the list of custom iOS target properties for your iOS app target and set its value to YES.
  3. Add code that defines an ActivityAttributes structure to describe the static and dynamic data of your Live Activity.
  4. Use the ActivityAttributes you defined to create the ActivityConfiguration.
  5. Add code to configure, start, update, and end your Live Activities.

Define a set of static and dynamic data

After you add a widget extension target that includes Live Activities to your Xcode project, describe the data that your Live Activity displays by implementing ActivityAttributes. The ActivityAttributes inform the system about static data that appears in the Live Activity. You also use ActivityAttributes to declare the required custom Activity.ContentState type that describes the dynamic data of your Live Activity.

Let’s model our football match using the new ActivityKit framework.

import ActivityKit

struct FootballMatch: ActivityAttributes {
typealias ContentState = Score

struct Score: Codable, Hashable {
let score1: Int
let score2: Int
}

let team1: String
let team2: String
}

Live activity presentation

We have to use the WidgetKit framework to display a live activity. WidgetKit provides us with the particular ActivityConfiguration type allowing us to define a live activity widget.

struct FastingActivityWidget: Widget {
var body: some WidgetConfiguration {
ActivityConfiguration(for: FastingAttributes.self) { context in
LiveActivityView(context: context)
.padding(.horizontal)
} dynamicIsland: { context in
DynamicIsland {
DynamicIslandExpandedRegion(.center) {
LiveActivityView(context: context)
}
} compactLeading: {
Image(systemName: "circle")
.foregroundColor(.green)
} compactTrailing: {
Text(verbatim: context.state.progress.formatted(.percent)
.foregroundColor(context.state.stage?.color)
} minimal: {
Image(systemName: "circle")
.foregroundColor(.green)
}
}
}
}

If your app already offers widgets, add the Live Activity to your WidgetBundle. If you don’t have a WidgetBundle — for example, if you only offer one widget — create a widget bundle as described in Creating a widget extension and then add the Live Activity to it. The following example shows how you can use an if statement with an availability clause to only add a Live Activity to your widget bundle if the device supports Live Activities:

@main struct FastBotWidgetBundle: WidgetBundle {
var body: some Widget {
FastBotWidget()

if #available(iOS 16.1, *) {
FastingActivityWidget()
}
}
}

Conclusion

Live activities in iOS 16 are a powerful feature that enables developers to display real-time updates on ongoing activities from their apps on the lock screen. By utilizing the ActivityKit framework, developers can track activity states, update live activity widgets, and handle user interactions. Live activities enhance user engagement, provide valuable information, and improve the overall app experience. If you want to keep users informed and engaged with your app, consider incorporating live activities using the ActivityKit framework.

Thanks

--

--