How to check if code is running in an XCode Preview in Swift

Code is running in an XCode preview if the process info environment variable "XCODE_RUNNING_FOR_PREVIEWS" is set to "1".

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 08/30/2024 at 12:20
Last Updated on 08/30/2024 at 13:15
<p>Two flowers that represents SwiftUI and XCode side by side with the text “Is XCode Preview” beneath it.</p>

Code is running in an XCode preview if the process info environment variable "XCODE_RUNNING_FOR_PREVIEWS" is set to "1".

Subscribe
<p>A screenshot of XCode showing utility we created to determine if the code is running in an XCode preview.</p>

The code demonstrated above can be found in the Utils/System folder of our open source starter projects linked below.

To use it, simply write isXCodePreview() in your app.

View SwiftUI Starter Project RepositoryView VisionOS Starter Project Repository

Looking to learn more about Design and Technology?

Search our blog to learn more about design and technology.

Search our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe