View in English

  • Apple 开发者
    • 入门汇总

    探索“入门汇总”

    • 概览
    • 学习
    • Apple Developer Program

    及时了解最新动态

    • 最新动态
    • 开发者你好
    • 平台

    探索“平台”

    • Apple 平台
    • iOS
    • iPadOS
    • macOS
    • Apple tvOS
    • visionOS
    • watchOS
    • App Store

    精选

    • 设计
    • 分发
    • 游戏
    • 配件
    • 网页
    • Home
    • CarPlay 车载
    • 技术

    探索“技术”

    • 概览
    • Xcode
    • Swift
    • SwiftUI

    精选

    • 辅助功能
    • App Intents
    • Apple 智能
    • 游戏
    • 机器学习与 AI
    • 安全性
    • Xcode Cloud
    • 社区

    探索“社区”

    • 概览
    • “与 Apple 会面交流”活动
    • 社区主导的活动
    • 开发者论坛
    • 开源

    精选

    • WWDC
    • Swift Student Challenge
    • 开发者故事
    • App Store 大奖
    • Apple 设计大奖
    • Apple Developer Centers
    • 文档

    探索“文档”

    • 文档库
    • 技术概述
    • 示例代码
    • 《人机界面指南》
    • 视频

    发布说明

    • 精选更新
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • Apple tvOS
    • Xcode
    • 下载

    探索“下载”

    • 所有下载
    • 操作系统
    • 应用程序
    • 设计资源

    精选

    • Xcode
    • TestFlight
    • 字体
    • SF Symbols
    • Icon Composer
    • 支持

    探索“支持”

    • 概览
    • 帮助指南
    • 开发者论坛
    • “反馈助理”
    • 联系我们

    精选

    • 《开发者账户帮助》
    • 《App 审核指南》
    • 《App Store Connect 帮助》
    • 即将实行的要求
    • 协议和准则
    • 系统状态
  • 快速链接

    • 活动
    • 新闻
    • 论坛
    • 示例代码
    • 视频
 

视频

打开菜单 关闭菜单
  • 专题
  • 所有视频
  • 关于

更多视频

  • 简介
  • 概要
  • 代码
  • 实时活动基础知识

    为你的 App 提升实时活动体验。探索展示实时活动的多个位置,包括迎来新样式的灵动岛,能在横屏使用 iPhone 时提供更多信息。了解如何为每个空间量身定制实时活动、合理安排内容和数据,并利用 ActivityKit 和推送通知实现全程实时更新。

    章节

    • 0:01 - Introduction
    • 1:53 - Create and update
    • 9:51 - Optimize

    资源

    • Human Interface Guidelines: Live Activities
    • Starting and updating Live Activities with ActivityKit push notifications
    • ActivityKit
      • 高清视频
      • 标清视频

    相关视频

    WWDC24

    • 将实时活动功能引入 Apple Watch
    • SwiftUI 基础知识

    WWDC23

    • 设计动态实时活动
  • 搜索此视频…
    • 4:16 - Define initial Live Activity

      // Define initial Live Activity.
      
      import ActivityKit
      import Foundation
      
      public struct DrinkOrderAttributes: ActivityAttributes {
          let shopName: String
          let drink: Drink
          let orderID: UUID
      
          public struct ContentState: Codable, Hashable {
              var phase: DrinkOrder.Phase = .waiting
              var estimatedReadyDate: Date
              var rating: DrinkOrder.Rating?
          }
      }
    • 5:35 - Create each Live Activity view

      // Create each Live Activity view
      
      import ActivityKit
      import SwiftUI
      import WidgetKit
      
      struct DrinkOrderLiveActivity: Widget {
          var body: some WidgetConfiguration {
              ActivityConfiguration(for: DrinkOrderAttributes.self) { context in
                  ActivityView(context: context)
              } dynamicIsland: { context in
                  DynamicIsland {
                      DynamicIslandExpandedRegion(.leading) {
                          ExpandedLeadingView(context: context)
                      }
                      DynamicIslandExpandedRegion(.center) {
                          ExpandedCenterView(context: context)
                      }
                      DynamicIslandExpandedRegion(.trailing) {
                          ExpandedTrailingView(context: context)
                      }
                      DynamicIslandExpandedRegion(.bottom) {
                          ExpandedBottomView(context: context)
                      }
                  } compactLeading: {
                      CompactLeadingView(context: context)
                  } compactTrailing: {
                      CompactTrailingView(context: context)
                  } minimal: {
                      MinimalView(context: context)
                  }
              }
          }
      }
    • 7:43 - Start and update a Live Activity

      // Start a Live Activity
      
      func launchLiveActivity(order: DrinkOrder) throws {
          guard ActivityAuthorizationInfo().areActivitiesEnabled else { return }
          let attributes = DrinkOrderAttributes(shopName: "Coffee Shop", drink: order.drink, orderID: order.id)
          let estimatedReadyDate = Date.now + (15 * 60)
          let contentState = DrinkOrderAttributes.ContentState(phase: .waiting, estimatedReadyDate: estimatedReadyDate)
          let activityContent = ActivityContent(state: contentState, staleDate: nil)
          let activity = try Activity.request(attributes: attributes, content: activityContent)
      
      }
      
      // Update a Live Activity
      
      await activity.update(
          ActivityContent(
              state: DrinkOrderAttributes.ContentState(
                  phase: .preparing,
                  estimatedReadyDate: estimatedReadyDate
              ),
              staleDate: nil
          )
      )
    • 10:33 - Optimize for limited width in the Dynamic Island

      // Optimize for limited width in the Dynamic Island
      
      struct CompactTrailingView: View {
          @Environment(\.isDynamicIslandLimitedInWidth) var isDynamicIslandLimitedInWidth
          var context: ActivityViewContext<DrinkOrderAttributes>
          var body: some View {
              if isDynamicIslandLimitedInWidth {
                  StepProgressIconView(context: context)
              } else if context.state.phase.showsTimer {
                  EstimatedReadyView(context: context, font: .system(.body).monospacedDigit())
                      .multilineTextAlignment(.trailing)
                      .frame(maxWidth: maximumTimerLabelWidth)
              } else {
                  OrderPhaseLabelView(context: context, font: .caption2.bold(), color: .brown)
                      .multilineTextAlignment(.trailing)
              }
          }
      }
    • 11:34 - Extend background color in StandBy

      // Extend background color in StandBy
      
      struct ActivityView: View {
      
          @Environment(\.showsWidgetContainerBackground) var showsWidgetContainerBackground
          var context: ActivityViewContext<DrinkOrderAttributes>
      
          var body: some View {
              DetailView(context: context)
                  .background {
                      if showsWidgetContainerBackground {
                          LinearGradient.barista
                      }
                  }
                  .activityBackgroundTint(.espresso)
          }
      }
    • 12:30 - Add support for activityFamily small

      // Add support for activityFamily small
      
      import ActivityKit
      import SwiftUI
      import WidgetKit
      
      struct DrinkOrderLiveActivity: Widget {
          var body: some WidgetConfiguration {
              ActivityConfiguration(for: DrinkOrderAttributes.self) { context in
                  ActivityView(context: context)
              } dynamicIsland: { context in
                  DynamicIsland {
                      DynamicIslandExpandedRegion(.leading) {
                          ExpandedLeadingView(context: context)
                      }
                      DynamicIslandExpandedRegion(.center) {
                          ExpandedCenterView(context: context)
                      }
                      DynamicIslandExpandedRegion(.trailing) {
                          ExpandedTrailingView(context: context)
                      }
                      DynamicIslandExpandedRegion(.bottom) {
                          ExpandedBottomView(context: context)
                      }
                  } compactLeading: {
                      CompactLeadingView(context: context)
                  } compactTrailing: {
                      CompactTrailingView(context: context)
                  } minimal: {
                      MinimalView(context: context)
                  }
              }
              .supplementalActivityFamilies([.small])
          }
      }
    • 12:43 - Optimize for small family

      // Optimize for small family
      
      struct ActivityView: View {
          @Environment(\.showsWidgetContainerBackground) var showsWidgetContainerBackground
          @Environment(\.activityFamily) var activityFamily
      
          var context: ActivityViewContext<DrinkOrderAttributes>
      
          var body: some View {
              contentView
                  .background {
                      if showsWidgetContainerBackground {
                          LinearGradient.barista
                      }
                  }
                  .activityBackgroundTint(.espresso)
          }
      
          @ViewBuilder
          var contentView: some View {
              if activityFamily == .small {
                  SmallView(context: context)
              } else {
                  DetailView(context: context)
              }
          }
      }
    • 13:36 - Add interactivity with App Intents

      // Add interactivity with App Intents
      
      struct RateDrinkIntent: LiveActivityIntent {
          static var title: LocalizedStringResource = "Rate Drink"
      
          @Parameter(title: "Order ID")
          var orderID: String
      
          @Parameter(title: "Positive")
          var isPositive: Bool
      
          func perform() async throws -> some IntentResult {
              await updateLocalDatastore(rating: isPositive ? .great : .poor, dismissPolicy: .after(.now + 15))
              return .result()
          }
      }
    • 14:06 - Associate an intent with a button

      // Associate an intent with a button
      
      struct RatingButtons: View {
          var context: ActivityViewContext<DrinkOrderAttributes>
          var body: some View {
              HStack(spacing: 12) {
                  Button(intent: RateDrinkIntent(
                      orderID: context.attributes.orderID.uuidString, isPositive: false)) {
                      Label("Not Good", systemImage: "hand.thumbsdown.fill")
                  }
                  .buttonStyle(RatingButtonStyle(color: .red))
      
                  Button(intent: RateDrinkIntent(
                      orderID: context.attributes.orderID.uuidString, isPositive: true)) {
                      Label("Great", systemImage: "hand.thumbsup.fill")
                  }
                  .buttonStyle(RatingButtonStyle(color: .green))
              }
          }
      }
    • 0:01 - Introduction
    • Live Activities keep people up-to-date about ongoing tasks or events with progressing information over time, appearing on the Lock Screen, in the Dynamic Island, on Apple Watch, and on the CarPlay Dashboard.

    • 1:53 - Create and update
    • Start by defining an efficient data model using ActivityAttributes for static data and ContentState for dynamic data. Construct tailored UI for each presentation and manage the activity's lifecycle locally via ActivityKit or remotely using push notifications.

    • 9:51 - Optimize
    • Refine the Live Activity experience by adapting layouts to accommodate specific constraints, such as limited width in the Dynamic Island in landscape, or adopting the small activity family for Apple Watch. Integrate App Intents to let people perform quick, contextual actions.

Developer Footer

  • 视频
  • WWDC26
  • 实时活动基础知识
  • 打开菜单 关闭菜单
    • iOS
    • iPadOS
    • macOS
    • Apple tvOS
    • visionOS
    • watchOS
    打开菜单 关闭菜单
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • SF Symbols
    打开菜单 关闭菜单
    • 辅助功能
    • 配件
    • Apple 智能
    • App 扩展
    • App Store
    • 音频与视频 (英文)
    • 增强现实
    • 设计
    • 分发
    • 教育
    • 字体 (英文)
    • 游戏
    • 健康与健身
    • App 内购买项目
    • 本地化
    • 地图与位置
    • 机器学习与 AI
    • 开源资源 (英文)
    • 安全性
    • Safari 浏览器与网页 (英文)
    打开菜单 关闭菜单
    • 完整文档 (英文)
    • 部分主题文档 (简体中文)
    • 教程
    • 下载
    • 论坛 (英文)
    • 视频
    打开菜单 关闭菜单
    • 支持文档
    • 联系我们
    • 错误报告
    • 系统状态 (英文)
    打开菜单 关闭菜单
    • Apple 开发者
    • App Store Connect
    • 证书、标识符和描述文件 (英文)
    • 反馈助理
    打开菜单 关闭菜单
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program (英文)
    • Mini Apps Partner Program
    • News Partner Program (英文)
    • Video Partner Program (英文)
    • 安全赏金计划 (英文)
    • Security Research Device Program (英文)
    打开菜单 关闭菜单
    • 与 Apple 会面交流
    • Apple Developer Center
    • App Store 大奖 (英文)
    • Apple 设计大奖
    • Apple Developer Academies (英文)
    • WWDC
    阅读最近新闻。
    获取 Apple Developer App。
    版权所有 © 2026 Apple Inc. 保留所有权利。
    使用条款 隐私政策 协议和准则