72

I have an existing App, basically a shopping list app, to which I'm trying to add some sweet sweet SwiftUI lovin.

My issue is the real time preview updating doesn't work - the warning "Automatic preview updating paused" continually shows. I hit the resume button, it builds the app, it shows the current view, and that warning immediately shows again. I can never see changes to the code reflected in the canvas without using the resume button.

This is happening in Xcode 11.1, and 11.2 beta 2. I can find literally no other mention of this either here on SO, and there's one thread with no answers on Apple's Dev forums.

Brian M
  • 3,812
  • 2
  • 11
  • 31
  • 1
    I noticed the live preview also pauses quite often for me... The only things that I can change that don't pause are very minor things like adjusting the text that is displayed in a text view... Do minor changes like that also cause it to pause for you? – Quinn Oct 23 '19 at 14:12
  • @Quinn - yes, before I even make a change, the warning and resume button appear. They show up even before the resume build has finished and the preview shows. I can't make any changes at all that are visible without using resume. – Brian M Oct 23 '19 at 16:44
  • After trying every answer in this list, nothing worked. Open Apple Feedback, still open. The only thing that finally get the preview to 'reset' and back working was this: https://stackoverflow.com/a/70617215/3276518 – bauerMusic Feb 02 '22 at 07:17

9 Answers9

68

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

Kacper Dziubek
  • 1,513
  • 1
  • 11
  • 16
63

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
Vaseltior
  • 1,226
  • 15
  • 27
  • I do not understand where/when I would have to execute the script to enable the automatic resuming of previews. Can you please clarify that? – tbrodbeck Sep 13 '20 at 19:17
  • I mean, that if you're not using the `ENABLE_PREVIEWS` value to check if preview is currently running. then for the preview, not to stop, you should disable bumping. next you'll be able to see the preview without abort signal or resume signal. and since after that you could want to activate the bumping mechanism again... you may enter a painful back and forth situation. – Vaseltior Sep 17 '20 at 12:52
  • Thanks for the answer, but I still do not really understand what you mean.. I tried to reproduce the steps `# 1. Select: your Target in Xcode # 2. Select: Build Phases Tab`, but I could not find those options in xcode (sorry for being new) – tbrodbeck Sep 21 '20 at 15:32
  • 1
    What are you trying to achieve @tillus? Here is a link to how to add a build script phase in Xcode : https://stackoverflow.com/a/39633955/402214 – Vaseltior Sep 24 '20 at 09:22
  • 2
    Thanks @Vaseltior! This annoyed me for days! I wish Apple provide a real description of why the preview is not working instead of a meaningless phrase... – zysoft Jan 22 '21 at 03:03
  • It's kind of tedious to do it at every step in a complex project but sure enough, it really works! This should be the accepted answer since it scales better. – Lucas van Dongen Jan 26 '21 at 15:32
  • 1
    I have summarized your input here: https://github.com/Maschina/update_version_numbers – Maschina Jan 19 '22 at 11:53
13

I ended up sending in feedback to Apple, and they responded with a fix. I have a build script in the target that auto-increments the build number. If I remove that script then previewing works as intended.

So if you're having this issue remove anything in Target -> Build Phases -> Run Script and try again. The canvas preview should update as you would expect.

Brian M
  • 3,812
  • 2
  • 11
  • 31
  • 6
    I don't have any build scripts but I still get this issue in Xcode 11.2.1. – Isuru Nov 23 '19 at 05:57
  • Same thing. I'm running Xcode 11.3 and all of a sudden this started happening and removing script phases does not fix it. – Pacu Jan 02 '20 at 12:27
  • 3
    Same problem here with Xcode 11.3.1 running on 10.15.2 (no scripts). If I click "Resume", the canvas pane simply disappears, and the warning comes back when canvas is re-opened. I gave up on SwiftUI in beta last year after encountering too many bugs, came back to it thinking surely it would work now, but no. The tools are complete trash and do not work at all. Even creating a brand new, clean Swift UI project, it is broken right off the bat. I hope Apple fixes these awful bugs because SwiftUI has so much promise, but it's leaving a bad taste in developers' mouths. Maybe Xcode 12 + 10.6. – Ben Stahl Jan 17 '20 at 01:22
  • Thanks for the hint, this helped. But please note that this might affect every run script in every target that will be contained in your app like app extensions and Watch apps. – Koraktor Jan 28 '20 at 18:16
  • Thanks, I was having this problem but it was a version script running in an embedded framework which would then pause the preview. I had been on a completely wrong track thinking it was something to do with Core Data and previews. – dlemex Feb 10 '20 at 22:58
  • I only have a single target and I don't even have the Run Scripts section in Build Phases. Needless to say, the previews do not work for me either. I wish there was a way to force it to reload no matter what. This is in 11.3.1. – Tomáš Hübelbauer Mar 14 '20 at 08:26
11

For me, Canvas did not work when I had Legacy Build System.

You can change it via,

File -> Project Settings (or Workspace Settings) -> Build System -> Choose "New Build System(Default).

As it says, it is the default option. If for any reason Legacy build system was chosen, Canvas won't work.

Edit on June 30, 2020: We no longer have Legacy Build System in Xcode 12 beta.

imthath
  • 1,353
  • 1
  • 13
  • 35
  • But in this case canvas says about Legacy Build System explicitly. – imike May 15 '20 at 08:47
  • 1
    It's worth noting that (Xcode 11.5) new projects do not explicitly say new build system, and switching over (project settings, not workspace in the case of a new project, but either as appropriate) fixes this – Feldur Jun 08 '20 at 18:08
2

What worked for me was to "clean" Xcode

On the Mac

Open Xcode

command+k (Clean console)

command+option+k (Reload console)

command+option+shift+k (Clean build folder)

Exit Xcode

From a terminal window, clean the derived data. I run the following based on where my Xcode is installed. I believe its the base location

rm -rf ~/Library/Developer/Xcode/DerivedData

Re-open xcode and it worked great!

Jacksonsox
  • 1,114
  • 15
  • 25
2

If you use Xcode 13 or 14 with custom scripts, you must ensure that the script for install builds only is checked in.

enter image description here

Pankaj Gaikar
  • 2,357
  • 1
  • 23
  • 29
1

In my experiements I found that ENABLE_PREVIEWS is always set to YES in a SwiftUI project. Instead I found that in normal builds Xcode sets TARGET_DEVICE_MODEL and in SwiftUI it does not.

So the solution is like the one described in this answer: https://stackoverflow.com/a/62216533/833197 but using a different variable.

On another note, setting anything in the Info.plist in a build script seems to be "too late" in recent Xcode versions. It will not be used until next build. Also you end up with a modified version control working copy of your files which might not be what you want.

To resolve this I have

  1. Used a pre-build script in the build scheme instead
  2. Generated a xcconfig with the build number and made it ignored by the version control system (git in my case).

The variables set in a xcconfig file can be referenced in the Info.plist file.

Jon Brooks
  • 2,472
  • 24
  • 32
Nicolai Henriksen
  • 1,324
  • 1
  • 13
  • 37
  • This answer seemed more helpful than using `ENABLE_PREVIEWS` but I found `TARGET_DEVICE_MODEL` to not be set during my archive build scripts. Ultimately, I used my own environment var that I passed to xcodebuild via an xcarg param. – Jon Brooks Jan 12 '22 at 23:29
0

It's strange. But for me automatic preview always fails when I name projects using digits only (i. e. "111"). When naming using letters (with or without digits), everything is ok. 12.3 beta (12C5020f), Big Sur beta 11.1 (20C5048k).

0

For me it was because i had a pre-actions script in the Build section in the Edit Scheme screen, removing this script got the preview to work as intended

JAHelia
  • 6,934
  • 17
  • 74
  • 134