Design and develop Live Activities with ActivityKit for iOS 18

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 startActivity function creates attributes and content values before calling try activity.request.
  • Ending a lab activity can be done using the end method 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

Video description

This is a full walkthrough of how to design, implement and use live activities for iOS, using the latest capabilities for iOS 18 to start the activities via push notifications and how those notifications need to be structured for this to work. -------------------------------- ๐Ÿ”— Links -------------------------------- - My fully working sample project https://github.com/fespinoza/Youtube-SampleProjects/tree/main/LiveActivityDemo - Apple documentation to start/update live activities via push notifications https://developer.apple.com/documentation/activitykit/starting-and-updating-live-activities-with-activitykit-push-notifications - Apple docs to set up terminal to send pushes https://developer.apple.com/documentation/UserNotifications/sending-push-notifications-using-command-line-tools#Send-a-push-notification-using-a-JSON-web-token-JWT -------------------------------- ๐Ÿ‘‹ You can find me in -------------------------------- - BlueSky: https://bsky.app/profile/fespinozacast.bsky.social - Github: https://github.com/fespinoza - BuyMeACoffee: https://buymeacoffee.com/craftingswift -------------------------------- ๐Ÿ“ธ Camera Gear -------------------------------- - Camera: Sony A6700 (https://amzn.to/4iYkLYR) - Lens: Sigma 16mm 1.4 for Sony E (https://amzn.to/4bYAmFz) - Elgato Prompter (https://amzn.to/3A2ZLiY) - Key light: Godox ML100Bi (https://amzn.to/41YCW9Y) - Mic #1: DJI mic 2 + DJI lavalier mic (https://amzn.to/4hL8F4m) - Mic #2: Elgato Wave DX + Elgato Wave XLR (https://amzn.to/4hjvbSR) - M1 Max Macbook Pro (https://amzn.to/3NG5ZIv) - Screen Recording: CleanShot X -------------------------------- ๐Ÿ“– Reading Tools -------------------------------- I have an affiliate link, but I honestly recommend trying readwise. I like how helps you remember what you read. The reader app is excellent for articles too! https://readwise.io/craftingswift/ It gives you an extra month of your trial period -------------------------------- ๐Ÿท๏ธ Chapters: -------------------------------- 00:00 - Intro 01:42 - Design 04:23 - Modeling the data 05:32 - Implementation 07:32 - Starting Live Activity in Code 08:18 - Starting with push notifications (Concept) 09:40 - Starting with push notifications (Practice) 15:10 - Debugging Live Activities with Push Notifications 16:52 - Outro