Skip to content

Instantly share code, notes, and snippets.

@yannxou
Created April 5, 2024 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yannxou/1cae00676811a893cf8bab4c57fe6ab7 to your computer and use it in GitHub Desktop.
Save yannxou/1cae00676811a893cf8bab4c57fe6ab7 to your computer and use it in GitHub Desktop.
Swift: Detect code running in SwiftUI preview
extension ProcessInfo {
var isSwiftUIPreview: Bool {
environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
}
// or:
static func isOnPreview() -> Bool {
return processInfo.processName == "XCPreviewAgent"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment