LiveActivities preview in XCode, Missing 'previewContext'

I'm trying to create a preview for my live activities when it's stale, so I created a preview provider. I followed some examples, but XCode says I'm missing preview context. Am I doing something wrong?

I tried adding .previewContext(WidgetPreviewContext(family: .systemSmall)) but that doesn't seem to work.

Answered by Developer Tools Engineer in 814772022

to tell the preview system to render the live activity as if the state was stale you can use this UI toggle in the preview canvas. This will duplicate all the items in the timeline provided to previews with a stale version following the non-stale one.

Hi,

Sorry to hear you are having problems getting live activity previews working. Recent Xcode releases have added the new #Preview macro to replace the older PreviewProvider.

The macro versions are especially important for Widget and LiveActivity previewing as they support features that the PreviewProviders don't. Could you try using a preview of the following type:

#Preview("preview name", as: .content, using: MyLiveActivityAttributes.preview) {
   MyLiveActivity()
} contentStates: {
    MyLiveActivityAttributes.ContentState.mock1
    MyLiveActivityAttributes.ContentState.mock2
}

replacing with variables and types from your project

Accepted Answer

to tell the preview system to render the live activity as if the state was stale you can use this UI toggle in the preview canvas. This will duplicate all the items in the timeline provided to previews with a stale version following the non-stale one.

LiveActivities preview in XCode, Missing 'previewContext'
 
 
Q