How to detect SwiftUI preview mode

May 27, 2022 · Follow on Twitter and Mastodon swiftui

In this post, we’ll take a quick look at how to determine if code is running as a SwiftUI preview or not.

This information can be fetched from the ProcessInfo environment dictionary, by checking if the XCODE_RUNNING_FOR_PREVIEWS key has the string value "1":

public extension ProcessInfo {
    
    var isSwiftUIPreview: Bool {
        environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
    }
}

You shouldn’t misuse this information, but if you have a preview problem that you need to work around, this property may be a good last resort.

How to make the code testable and easier to use

If you want to make this functionality easier to to find and be able to unit test and mock this information, I suggest adding a protocol that describes this capability:

protocol SwiftPreviewInspector {

    var isSwiftUIPreview: Bool { get }
}

You can then let ProcessInfo implement it:

extension ProcessInfo: SwiftPreviewInspector {}

then create a standard implementation that uses ProcessInfo:

class StandardSwiftPreviewInspector: SwiftPreviewInspector {

    public var isSwiftUIPreview: Bool {
        ProcessInfo.processInfo.isSwiftUIPreview
    }
}

Conclusion

This was a short post, but I hope you found it helpful. You can find the source code in the SwiftUIKit library. Feel free to try it out and let me know what you think.

Discussions & More

Please share any ideas, feedback or comments you may have in the Disqus section below, or by replying on Twitter or Mastodon..

If you found this text interesting, make sure to follow me on Twitter and Mastodon for more content like this, and to be notified when new content is published.

If you like & want to support my work, please consider sponsoring me on GitHub Sponsors.