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

If you're having custom Run Script Phases in Build Phases and you don't want (or can't) remove them, then try to check checkbox "Run script only when installing".

enter image description here

Tags:

Xcode

Swiftui