Detecting SwiftUI's preview mode

May 27, 2022 · Follow on Twitter and Mastodon

In this article, we’ll take a look at how to determine if code is running in a SwiftUI preview, which may be nice if you need to disable certain things, like network calls.

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, it may be a good alternative.

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 {}

You can now use ProcessInfo as the inspector in your app, to avoid relying on it directly.

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.