Design and develop Live Activities with ActivityKit for iOS 18
How to Design and Develop Live Activities for iOS 18
Introduction to Live Activities
- The video introduces the concept of designing and developing live activities for iOS 18 using Activity Kit, specifically focusing on a football launch activity.
- Felipe, an experienced iOS and macOS developer from Norway, presents his background in Apple platform development.
Understanding Live Activities
- Live activities were introduced in iOS 16 and updated in iOS 17, aiming to replace multiple push notifications by tracking live events for up to 8 hours.
- Use cases include food delivery updates, match scores, bus tickets, etc. They can be initiated when the app is in the foreground or via push notifications starting from iOS 17.2.
Designing Live Activities
- Four required states must be assigned when creating a live activity: lock screen state, dynamic island expanded state, dynamic island compact state, and minimal state.
- Each state has specific design requirements; for example, the expanded state requires defining five regions.
State Management
- The compact state displays information on both sides of the dynamic island if itโs the only running activity; otherwise, it shows a minimal representation.
- A fifth optional design case exists for Apple Watch presentations that adapt based on compatibility with smart stacks.
Example Project Overview
- Felipe plans to build a sports app live activity that tracks match scores through various states: pre-match alerting, match playing (two halves), pause states, and end states.
- Simplifications exclude time extensions like extra periods or penalties while focusing on essential domain model states.
Data Modeling for Live Activities
- Essential data includes team names/images, scores, current match status (period name/time left), start time details.
- Distinction between attributes (constant data throughout duration) and content states (data changing over time).
Structuring Data for Implementation
- Emphasizes importance of structuring data correctly due to limitations (4KB max for content state).
- Defines structures conforming to activity attributes including team info and match start times as well as nested content states within protocols.
Final Steps in Development
Live Activity Implementation in Swift
Overview of Live Activity Attributes
- The score activity attributes are declared to work with specific data types, utilizing two required closures for defining the UI for both the lock screen and dynamic island configurations.
- In the lock screen closure, a SwiftUI view must be compatible with WidgetKit, limiting the use of certain views like AsyncImage.
Dynamic Island Presentation
- For dynamic island presentations, multiple regions (leading, trailing, center, bottom) are defined instead of returning a single view.
- A simple demo app is created to allow users to start and update lab activities manually; it utilizes an activity family environment for custom watch presentation.
Starting and Updating Live Activities
- To start a live activity, the app must be in the foreground. The
startActivityfunction creates attributes and content values before callingtry activity.request.
- Ending a lab activity can be done using the
endmethod on the activity itself, specifying how long to keep its UI after completion.
Push Notifications for Live Activities
- Since iOS 17.2, live activities can be initiated via push notifications. An overview of this process includes obtaining a start token when launching the app.
- This start token must be communicated to a backend server for use in push notifications through APNs to identify which device and lab activity to initiate.
Managing Multiple Instances
- Each instance of a live activity requires unique identification; thus, managing multiple instances (e.g., observing two football matches simultaneously) necessitates distinct tokens.
- The demo app prints out the start token into the console upon launch. Observing this token allows further actions within Xcode's project file settings.
Sending Push Notifications
- In Xcode's capabilities section, developers can create notifications directly linked to their application bundle ID.
- JSON content is structured specifically for starting an activity; it includes event properties such as time since 1970 format and relevant attributes like team info and match state.
JSON Content Creation Utility
- A command-line utility is developed in Swift to generate JSON data based on predefined structures for live activities.
- This utility ensures accurate serialization from score attributes without errors when receiving JSON data from push notifications.
Live Activity Updates in iOS
Overview of Push Notifications for Live Activities
- The process begins with obtaining an update token from the excot console, which is essential for updating the lab instance via push notifications.
- A new push notification is created to indicate that a match has started, utilizing JSON content where the event value is set to "update" and attributes are adjusted accordingly.
Updating Live Activity States
- To inform users about significant events like goals during a match, a new JSON payload can be generated and sent as a push notification.
- The live activity can be updated multiple times, such as setting the match state to halftime or notifying users of subsequent goals.
Finalizing Match Updates
- As the match concludes, another push notification can signal its end while providing details on how long before it is removed by iOS.
- Demonstrations show that even with an ongoing live activity, it's possible to send pushes that restart or launch new instances of live activities.
Challenges Encountered with Live Activities
- Issues arise when decoding content states or attributes; specific problems include date formatting discrepancies leading to failures in starting activities.
- All dates in JSON must adhere to Unix timestamp format (seconds since 1970), which differs from other encoding strategies and may cause issues if not correctly formatted.
Best Practices and Observations
- Ensure timestamps are close to real-time; otherwise, updates may be discarded without warning if they appear outdated.
- Apple recommends using console apps to monitor processes related to live activities for troubleshooting purposes.
Conclusion and Resources