How can I run older Xcode version on new macOS version? - xcode

I am facing a problem running older versions of Xcode on newer MacOS versions.
For example, Xcode 13 on MacOS Ventura.

The solution is very simple. If you have the older version downloaded in your Applications folder for example, lets say 12.5.1 version, you just need to:
Open Terminal
Open Applications folder in Finder
Drag the Xcode app into Terminal so it gets its path
Then add this next to it: /Contents/MacOS/Xcode, so the full command will be something like /Applications/Xcode-12.5.1.app/Contents/MacOS/Xcode
Press enter to run the command
Now you should be able to run it. You will note that when you open this version of Xcode, the Terminal will open too, but don't close Terminal because it will close the Xcode too.
Here you can find older Xcode versions.

Change the paths to OLD/NEW Xcodes and run script. The script will change the build version of the old Xcode to the new one, run it and restore. Script needs to be run once, after that Xcode can be opened via double click
Works on macOS Monterey for Xcode 12.5.1 and Ventura for Xcode 13
#!/bin/sh
set -euo pipefail
# Set the paths to your Old/New Xcodes
OLD_XCODE="/Applications/Xcode_13.4.1.app" # or /Applications/Xcode_12.5.1.app on Monterey
NEW_XCODE="/Applications/Xcode.app" # To get build number
# Get New Xcode build number
OLD_XCODE_BUILD=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" ${OLD_XCODE}/Contents/Info.plist)
NEW_XCODE_BUILD=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" ${NEW_XCODE}/Contents/Info.plist)
echo The Old Xcode build version is $OLD_XCODE_BUILD
echo The New Xcode build version is $NEW_XCODE_BUILD
# Change Old Xcode build version to New Xcode
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${NEW_XCODE_BUILD}" ${OLD_XCODE}/Contents/Info.plist
# Open Old Xcode (system will check build version and cache it)
open $OLD_XCODE
# Revert Old's Xcode's build version
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${OLD_XCODE_BUILD}" ${OLD_XCODE}/Contents/Info.plist

This is how you get your xcode's current build version.
/usr/libexec/PlistBuddy -c "Print CFBundleVersion" /Applications/Xcode_12.4.app/Contents/Info.plist

If you are looking for a solution without using terminal every time, here it is:
Follow https://stackoverflow.com/a/69995053/14199447
Create a bash file with this content
#!/bin/bash
/Applications/Xcode-12.5.1.app/Contents/MacOS/Xcode
Open terminal, run chmod 700 YourBashFile.sh
Change the default opening app of YourBashFile to terminal.
Follow step 1 and 2 of this https://apple.stackexchange.com/a/407885 to create an executable application which you can put on your Dock. After this you should be able to use the new app like any other app.

Related

SwiftUI: Automatic preview updating paused, always

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.
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".
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
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.
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.
If you use Xcode 13 or 14 with custom scripts, you must ensure that the script for install builds only is checked in.
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!
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
Used a pre-build script in the build scheme instead
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.
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).
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

Building Xcode Project in Terminal - Choosing another Xcode

I have two different Xcode version installed on the same mac (Xcode 4 and Xcode 5). Xcode 4 is in the Applications folder.
So whenever I build any projects from the terminal, naturally it compiles by using Xcode 4. Is there any way to use Xcode 5 (which is in desktop) to build a project from the terminal?
I think the command xcode-select is here for this precise reason.
Usage: xcode-select [options]
Print or change the path to the active developer directory. This directory
controls which tools are used for the Xcode command line tools (for example,
xcodebuild) as well as the BSD development commands (such as cc and make).
Options:
-h, --help print this help message and exit
-p, --print-path print the path of the active developer directory
-s <path>, --switch <path> set the path for the active developer directory
-v, --version print the xcode-select version
-r, --reset reset to the default command line tools path
I had the same problem. I had changed the command-line tools option in Xcode's preferences. Here you can choose which version of Xcode, you wanted to use.

Cannot find FileMerge (opendiff tool) but I have xcode 4.6 installed

When I attempt to run FileMerge as a GUI for Opendiff, I receive an error:
$ git mergetool -t opendiff
Merging:
Gemfile
Gemfile.lock
...
Normal merge conflict for 'Gemfile':
{local}: modified file
{remote}: modified file
Hit return to start merge resolution tool (opendiff):
2013-12-26 20:00:20.248 opendiff[22367:e07] Couldn't find FileMerge
Gemfile seems unchanged.
Was the merge successful? [y/n] ^C
$
I have installed XCode 4.6.3 and Command Line Tools for Xcode April 2013. I am running OSX 10.7.5
I have tried solutions from the following two links with no success:
git diff with opendiff gives "Couldn't launch FileMerge" error
Is filemerge still available after Xcode 4.3 installation?
When I go to Xcode -> Open Developer Tool I do not see FileMerge in the list of options. There is a More Developer Tools link that takes me here: https://developer.apple.com/downloads/index.action?name=for%20Xcode%20-
How do I get FileMerge working?
FileMerge is located at within Xcode.
From the command-line:
cd /Applications/Xcode.app/Contents/Applications/
open .
Go into that directory once it opens:
Right-click on the app
Choose "Make alias"
Move that alias to your Applications directory
You're all set.
Strange. "XCode 4.5, where is FileMerge ?" suggests:
The FileMerge that is bundled with Xcode 4.5 doesn't work as a standalone application. I tried compressing it from the application bundle. I was able to expand it to show the FileMerge app on the desktop. But when I tried to run it I got an error saying it couldn't be opened.
Apple's developer downloads site has every version of Xcode. You could try trashing your current version of Xcode 4.5, downloading Xcode 4.5 from the developer downloads site, and see if FileMerge is there. If not, you can download Xcode 4.4 and see if that has FileMerge.
Indeed, I downloaded a fresh new install of XCode 4.5 and put my updated 4.4 to 4.5 upgraded version into the trash and I have now all the developer tools again, including FileMerge. Weird...
The other option, from that same thread:
Why not just install the Command Line Tools? That will give you the "opendiff" tool. Then, you create a little script like this one to make it useful and actually do a merge.
#!/bin/sh
# Get a hold of the last parameter.
eval LAST=\${$#}
# Now run opendiff with the previous version and the current version.
opendiff ${*} -merge "$LAST"
if you have xcode installed, then you can probably run the following from the command line to open FileMerge directly
open /Applications/Xcode.app/Contents/Applications/FileMerge.app/
Try reinstalling Xcode. I had the same issue and it fixed it for me! It appears the Merge utility got corrupt somehow.

How to install native assembler for Mac OSX?

I am interested to know how can I install native assemblar (I think also known as AS) on Mac OSX Lion. Someone suggested that I can use the Xcode to do it but I didn't see any options.Terminal windows is not able to recognize AS as command right now. Can someone provide more insight on this ? I am new to Mac/Unix environment so please provide help accordingly.
If you have Xcode installed, then you have as.There is no option to install specific packages from the Xcode installer.
as should be in /usr/bin/:
ls -l /usr/bin/as
If not, you probably have a problem with your Xcode installation.
Otherwise, also check your PATH environment variable, to ensure /usr/bin/ is in the list.
echo $PATH
If you've installed Xcode 4.3, it doesn't install the command line tools anymore. To get them, you have to launch Xcode, go to Xcode > Preferences... > Downloads tab, then click the 'Download' button for 'Command Line Tools'. You'll need to sign in with an Apple Developer ID.
Using Xcode 4.6 with Command Line Utilities installed
export PATH=/Applications/Xcode.app/Contents/Developer/usr/bin:$PATH
as will execute along with the other command line utilities.

Using Clang Static Analyzer from within Xcode

Since there is no Xcode script variable for "current project directory," how can you create a script menu item to run the Clang Static Analyzer on your current project from Xcode?
From the XCode script menu item, "Edit User Scripts" enter the following script:
#!/bin/bash
result=$( osascript << END
tell application "Xcode"
tell active project document
set projectPath to path as string
end tell
end tell
return projectPath
END
)
cd "$result"
/Developer/clangchecker/scan-build -k -V xcodebuild -configuration Debug -sdk iphonesimulator3.0
Obviously, you will need to adjust the path to your install of Clang, and adjust to the version of the SDK you are using.
Remember to do a "Clean All" immediately before using scan-build, or the results may be incomplete.
FYI, Xcode 3.2 (Snow Leopard only I believe) includes the Clang Static Analyzer in the "Build and Analyze" menu option.
http://iosdevelopertips.com/xcode/static-code-analysis-clang-and-xcode-3-2.html
One downside of Xcode 3.2 (aside from it only working on Snow Leopard) is that the v2.x Simulators don't seem to work - in fact, I've seen posts indicating that v2.x builds are not supported at all.
I believe the ${PROJECT_DIR} environment variable is what you want for the directory of the project running a build-phase script.
Either use the version bundled in XCode 3.2+, or download a newer version at https://clang-analyzer.llvm.org/ then see some additional instructions at https://clang-analyzer.llvm.org/xcode.html to switch XCode to that downloaded version.

Resources