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 814756022

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

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.

Is it possible there are issues with previewing Live Activities in Xcode 16.2?

I believe I have everything set up correctly, and the project builds properly with no errors, but the preview canvas is always blank

struct RestTimerLiveActivity: Widget {
    var body: some WidgetConfiguration {
        if #available(iOS 18.0, *) {
            return ActivityConfiguration(for: RestTimerActivityAttributes.self) { context in
                RestTimerActivityContent(context: context)
            } dynamicIsland: { context in
                dynamicIslandContent(context: context)
            }
            .supplementalActivityFamilies([.small])
        } else {
            return ActivityConfiguration(for: RestTimerActivityAttributes.self) { context in
                RestTimerMediumView(context: context)
            } dynamicIsland: { context in
                dynamicIslandContent(context: context)
            }
        }
    }
    
    private func dynamicIslandContent(context: ActivityViewContext<RestTimerActivityAttributes>) -> DynamicIsland {
        DynamicIsland {
            DynamicIslandExpandedRegion(.leading) {
                Text("Hello")
            }
            DynamicIslandExpandedRegion(.trailing) {
                Text("0:15")
            }
            DynamicIslandExpandedRegion(.center) {
                Text("Timer")
            }
            DynamicIslandExpandedRegion(.bottom) {
                Text("Rest Timer")
            }
        } compactLeading: {
            Text("Hello")
        } compactTrailing: {
            Text("0:15")
        } minimal: {
            Text("Hello")
        }
    }
}


@available(iOS 18.0, *)
struct RestTimerActivityContent: View {
    @Environment(\.activityFamily) var activityFamily
    var context: ActivityViewContext<RestTimerActivityAttributes>
    
    var body: some View {
        switch activityFamily {
        case .medium:
            RestTimerMediumView(context: context)
        case .small:
            RestTimerSmallView(context: context)
        @unknown default:
            fatalError()
        }
    }
}

struct RestTimerMediumView: View {
    var context: ActivityViewContext<RestTimerActivityAttributes>
    var body: some View {
        Text("Medium!")
    }
}

struct RestTimerSmallView: View {
    var context: ActivityViewContext<RestTimerActivityAttributes>
    var body: some View {
        Text("Small!")
    }
}

#Preview("Live Activity Content", as: .content, using: RestTimerActivityAttributes.middle) {
    RestTimerLiveActivity()
} contentStates: {
    RestTimerActivityAttributes.ContentState.idle
    RestTimerActivityAttributes.ContentState.paused
    RestTimerActivityAttributes.ContentState.running
}


#Preview("Live Activity Dynamic Island Compact", as: .dynamicIsland(.compact), using: RestTimerActivityAttributes.middle) {
    RestTimerLiveActivity()
} contentStates: {
    RestTimerActivityAttributes.ContentState.idle
    RestTimerActivityAttributes.ContentState.paused
    RestTimerActivityAttributes.ContentState.running
}

regarding:

previewing Live Activities in Xcode 16.2

I had to fill in some blanks in your code for RestTimerActivityAttributes, but once I did the live activity rendered fine for me.

I think to ensure that we are trying to hit the same issue as you it'd be best to file a feedback with a sample project attached that reproduces the problem for you, and then we can see if we see the same issue.

Ah, actually this was a human error on my part. Please disregard!

LiveActivities preview in XCode, Missing 'previewContext'
 
 
Q