Я использую XCode 15.0 beta 8 и тестирую на реальном устройстве iOS 17.0
Ниже приведен мой файл NotificationLiveActivity.swift
Код: Выделить всё
import ActivityKit
import WidgetKit
import SwiftUI
struct NotificationAttributes: ActivityAttributes {
public struct ContentState: Codable, Hashable {
var title: String
var label: String
}
var id: String
}
@available(iOS 16.2, *)
struct NotificationLiveActivity: Widget {
var body: some WidgetConfiguration {
ActivityConfiguration(for: NotificationAttributes.self) { context in
Text(context.state.title)
} dynamicIsland: { context in
DynamicIsland {
// Expanded UI goes here. Compose the expanded UI through
// various regions, like leading/trailing/center/bottom
DynamicIslandExpandedRegion(.leading) {
Text(context.state.title)
}
DynamicIslandExpandedRegion(.trailing) {
Text(context.state.label)
}
DynamicIslandExpandedRegion(.bottom) {
Text(context.state.title)
}
} compactLeading: {
Text(context.state.title)
} compactTrailing: {
Text(context.state.label)
} minimal: {
Text(context.state.title)
}
.widgetURL(URL(string: "http://www.apple.com"))
.keylineTint(Color.red)
}
}
}
Код: Выделить всё
func startLiveActivity(title: String, label: String) {
let attributes = NotificationAttributes(id: "")
let state = NotificationAttributes.ContentState(
title: title, label: label
)
do {
stopwatchActivity = try Activity.request(
attributes: attributes, contentState: state, pushType: nil)
NSLog("activity id: \(stopwatchActivity?.id ?? "")")
NSLog("description: \(stopwatchActivity?.content.description ?? "")")
} catch(let e) {
NSLog("error: \(e)")
}
}

Ожидается, что в центре уведомлений будет отображаться активность в режиме реального времени.
Подробнее здесь: https://stackoverflow.com/questions/771 ... ion-center
Мобильная версия