How do you check if SwiftUI is in preview mode?
You can detect this using ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"]
. The value will be "1"
at runtime when running in the canvas.
How to get Previews working again in a SwiftUI project
I think I have solved this. I had made what probably sounds like a stupid mistake. The project uses CoreData and I had given one of the Entities the same name as the project.
Whilst that might sound crazy, the project would build and run (simulator or device) without a problem. It was just the preview that was getting confused.
I have renamed the Entity and at least some of the Views can be previewed, including the TestView mentioned above.
There are still a couple of Views not previewing correctly, but I think that is due to another issue.
Xcode 12.3 SwiftUI Preview 1 of Content View Previews not found in any targets
By chance, I recognized why the previews were broken in my from-scratch-project. Reason was the "Versioning" build phase that I originally added during setup:
xcrun agvtool next-version -all
After wrapping that into a Previews check, everything's fine again:
if [ $ENABLE_PREVIEWS == "NO" ]
then
xcrun agvtool next-version -all
fi
SwiftUI: Automatic preview updating paused, always
The problem with all the given answers is that you need to check or uncheck your script in debug mode if you want to make the preview work.
Here is a convenient alternative using the environment variables.
This is really simple
Embed all the content of your script in an if
statement that check if we're using the preview or not. If we're in preview, then don't run the content of your script, otherwise, let's run it. And you don't have to sacrifice your script for release versions only.
Here is the template :
if [ $ENABLE_PREVIEWS == "NO" ]
then
# your code to execute here
else
echo "Skipping the script because of preview mode"
fi
And below a full example that I use to bump my build version number
# xcode-build-bump.sh
# @desc Auto-increment the build number every time the project is run.
# @usage
# 1. Select: your Target in Xcode
# 2. Select: Build Phases Tab
# 3. Select: Add Build Phase -> Add Run Script
# 4. Paste code below in to new "Run Script" section
# 5. Drag the "Run Script" below "Link Binaries With Libraries"
# 6. Insure that your starting build number is set to a whole integer and not a float (e.g. 1, not 1.0)
if [ $ENABLE_PREVIEWS == "NO" ]
then
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"
else
echo "Skipping Bump of version"
echo $ENABLE_PREVIEWS
fi
Related Topics
Optional Field Type Doesn't Conform Protocol in Swift 3
How to Change Navigation Bar & Back Button Colour iOS 15
Implementing Nscopying in Swift with Subclasses
Grab Frames from Video Using Swift
Convert Character to Integer in Swift
Swift 4.2 Setter Getter, All Paths Through This Function Will Call Itself
Swift 2 Protocol Extension Not Calling Overridden Method Correctly
Draw a Hole in a Rectangle with Spritekit
Get Image from Calayer or Nsview (Swift 3)
Background Request Not Execute Alamofire Swift
Back Button Image - What Is It Called in Swift
Only First Track Playing of Avmutablecomposition()
Swift Map(_:) Extension for Set()
How to Disable Vertical Scroll in Tabview with Swiftui
How to Implement Copy Constructor in Swift Subclass
Trying to Stream Audio from Microphone to Another Phone via Multipeer Connectivity
How Do People Deal with Iterating a Swift Struct Value-Type Property