I merely attempt to comply with Apple docs
That is my Stay Exercise Code (copy and paste from Docs):
import SwiftUI
import WidgetKit
import ActivityKit
@obtainable(iOS 16.1, *)
@obtainable(iOSApplicationExtension 16.1, *)
struct PizzaDeliveryAttributes: ActivityAttributes {
public typealias PizzaDeliveryStatus = ContentState
public struct ContentState: Codable, Hashable {
var driverName: String
var deliveryTimer: ClosedRange<Date>
}
var numberOfPizzas: Int
var totalAmount: String
var orderNumber: String
}
@obtainable(iOS 16.1, *)
@obtainable(iOSApplicationExtension 16.1, *)
struct PizzaDeliveryActivityWidget: Widget {
var physique: some WidgetConfiguration {
ActivityConfiguration(for: PizzaDeliveryAttributes.self) { context in
LockScreenLiveActivityView(context: context)
} dynamicIsland: { context in
DynamicIsland {
DynamicIslandExpandedRegion(.main) {
Label("(context.attributes.numberOfPizzas) Pizzas", systemImage: "bag")
.foregroundColor(.indigo)
.font(.title2)
}
DynamicIslandExpandedRegion(.trailing) {
Label {
Textual content(timerInterval: context.state.deliveryTimer, countsDown: true)
.multilineTextAlignment(.trailing)
.body(width: 50)
.monospacedDigit()
} icon: {
Picture(systemName: "timer")
.foregroundColor(.indigo)
}
.font(.title2)
}
DynamicIslandExpandedRegion(.middle) {
Textual content("(context.state.driverName) is on their approach!")
.lineLimit(1)
.font(.caption)
}
DynamicIslandExpandedRegion(.backside) {
Button {
// Deep hyperlink into your app.
} label: {
Label("Name driver", systemImage: "telephone")
}
.foregroundColor(.indigo)
}
} compactLeading: {
Label {
Textual content("(context.attributes.numberOfPizzas) Pizzas")
} icon: {
Picture(systemName: "bag")
.foregroundColor(.indigo)
}
.font(.caption2)
} compactTrailing: {
Textual content(timerInterval: context.state.deliveryTimer, countsDown: true)
.multilineTextAlignment(.middle)
.body(width: 40)
.font(.caption2)
} minimal: {
VStack(alignment: .middle) {
Picture(systemName: "timer")
Textual content(timerInterval: context.state.deliveryTimer, countsDown: true)
.multilineTextAlignment(.middle)
.monospacedDigit()
.font(.caption2)
}
}
.keylineTint(.cyan)
// Create the displays that seem within the Dynamic Island.
// ...
}
}
}
@obtainable(iOS 16.1, *)
@obtainable(iOSApplicationExtension 16.1, *)
struct LockScreenLiveActivityView: View {
let context: ActivityViewContext<PizzaDeliveryAttributes>
var physique: some View {
VStack {
Spacer()
Textual content("(context.state.driverName) is on their approach together with your pizza!")
Spacer()
HStack {
Spacer()
Label {
Textual content("(context.attributes.numberOfPizzas) Pizzas")
} icon: {
Picture(systemName: "bag")
.foregroundColor(.indigo)
}
.font(.title2)
Spacer()
Label {
Textual content(timerInterval: context.state.deliveryTimer, countsDown: true)
.multilineTextAlignment(.middle)
.body(width: 50)
.monospacedDigit()
} icon: {
Picture(systemName: "timer")
.foregroundColor(.indigo)
}
.font(.title2)
Spacer()
}
Spacer()
}
.activitySystemActionForegroundColor(.indigo)
.activityBackgroundTint(.cyan)
}
}
My Widget Bundle:
@fundamental
struct FirstWidgetBundle: WidgetBundle {
@WidgetBundleBuilder
var physique: some Widget {
WidgetLatestNews()
WidgetLatestVideos()
WidgetDailyText()
WidgetOfficialSite()
SecondWidgetBundle().physique
}
}
struct SecondWidgetBundle: WidgetBundle {
@WidgetBundleBuilder
var physique: some Widget {
WidgetStudy()
CurrentMonth()
WidgetComing()
if #obtainable(iOSApplicationExtension 16.1, *) {
PizzaDeliveryActivityWidget()
}
}
}
And that is how I begin it on Button Faucet (copy and paste from Docs):
// ---
if #obtainable(iOS 16.2, *) {
if ActivityAuthorizationInfo().areActivitiesEnabled {
var future = Calendar.present.date(byAdding: .minute, worth: Int(34), to: Date())!
future = Calendar.present.date(byAdding: .second, worth: Int(45), to: future)!
let date = Date.now...future
let initialContentState = PizzaDeliveryAttributes.ContentState(driverName: "Invoice James", deliveryTimer:date)
let activityAttributes = PizzaDeliveryAttributes(numberOfPizzas: 3, totalAmount: "$42.00", orderNumber: "12345")
let activityContent = ActivityContent(state: initialContentState, staleDate: Calendar.present.date(byAdding: .minute, worth: 30, to: Date())!)
// Add code to begin the Stay Exercise right here.
// ...
do {
let deliveryActivity = strive Exercise.request(attributes: activityAttributes, content material: activityContent)
print("Requested a pizza supply Stay Exercise (String(describing: deliveryActivity.id)).")
} catch (let error) {
print("Error requesting pizza supply Stay Exercise (error.localizedDescription).")
}
}
}
// ---
I’ve added NSSupportsLiveActivities
YES for each: App Goal and widget extension Goal. And Stay Actions change can also be turned on in App Settings.
Why it doesnt seem on the Lock Display? Am I nonetheless lacking sth?
The next data is printed on console:
Requested a pizza supply Stay Exercise 9713D828-2C2A-442E-9356-0E1C64F49D4C.