iOS 16 Live Activity + SwiftUI Previews

Is it possible to see a preview of the Live Activity UI we design? For a regular widget, we pass in a WidgetPreviewContext modifier where we specify the size of the widget to preview. Is it possible to do something similar to see how the live activity would appear without having to run the app and then see how the live activity appears on the Lock Screen?

Post not yet marked as solved Up vote post of jabhiji Down vote post of jabhiji
1.7k views
  • did you found any solution to view and test live activity or dynamic island.

Add a Comment

Replies

Did you find the way? I am also looking for... Apple's document is terrible....

@jabhiji @waohqc @amit12we I found a way!

I initialize some dummy ActivityAttributes and ContentState then use attributes.previewContext(contentState, viewKind: .dynamicIsland(.compact))

Full example

struct HuntLiveActivityWidget_Previews: PreviewProvider {

    static let attributes = HuntAttributes.init(huntID: "1234",
                                                huntName: "Hunt name",
                                                numberOfClues: 10)
    static let contentState = HuntAttributes.ContentState(currentClueText: "This is the current clue to be solved.",
                                                          currentClueIndex: 0,
                                                          currentHuntProgress: 0.1)
    static var previews: some View {
        attributes
            .previewContext(contentState, viewKind: .dynamicIsland(.compact))
            .previewDisplayName("Island Compact")
        attributes
            .previewContext(contentState, viewKind: .dynamicIsland(.expanded))
            .previewDisplayName("Island Expanded")
        attributes
            .previewContext(contentState, viewKind: .dynamicIsland(.minimal))
            .previewDisplayName("Minimal")
        attributes
            .previewContext(contentState, viewKind: .content)
            .previewDisplayName("Notification")
    }
}