How to run Script from "Build Phases" section in archived (deployed) app? - xcode

so I'm trying to build a macOS menu bar app that executes a binary (a small server) and closes it when the app is closed. I added "path/to/server >/dev/null 2>&1 &" to the "Run Script"-section in "Build Phases" and it works perfectly when executing the app from Xcode. Now I wanted to store it locally in my Applications-Folder ("Archive" -> "Distribute App" -> "Copy App"). But when I start the app from there, the server does not start. I only found solutions for executing the script before archiving, but it needs to run every time I open the application. (macOS Ventura, Xcode 14)
What do I need to do?
Thanks a lot,
Nicolas

Related

programmatically xcode build(ios library) -is it possible?

I have mac descktop app(flutter, small obfuscator for objective-c code). after obfuscation of the source code, the user builds a ios library in XCode. is it possible to start the build process from my application?
These my options are:
Xcode Command Line Tools - it's very similar, but looks a bit complicated.
Also, I don't know yet how to use the terminal (terminal commands) in the desktop application - is it even possible?
fastlane -this tool is more for deployment. can i use this to build locally?
What do you recommend? maybe there are other options?
read below documentaion and set flutter path so it can be runable in terminal or you can get help from youtube to set flutter path
then simply open terminal and run below comands one by one
1.open -a simulator
this command open ios emulator
write cd and drag your project folder path
cd .../example
flutter run
✅your app launch in emulator
flutter path set details
https://docs.flutter.dev/get-started/install/macos

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

Copy iOS App bundle to another folder during Xcode build

I would like to copy the contents of my iOS App Bundle to a particular directory during the Xcode build stage. Can anyone tell me how to do that?
A solution using a script in "Build Phases" does not work properly since Xcode is not finished building the app when running the script. Here is a solution with a script that runs after all build tasks are finished:
Go to "Edit Scheme"
Click on the triangle next to "Build"
Select "Post-action"
Press the + button and select "New Run Script Option"
Select your app name in "Provide build settings from"
Add the following shell script:
Script:
PRODUCT="${BUILT_PRODUCTS_DIR}/${TARGET_NAME}.app"
cp -R "${PRODUCT}" ~/Desktop

Adding external scripts to Xcode4

I need to do some tasks before any Archive launched in Xcode.
Is there any way in Xcode to add a .sh or .py script run before Archive ?
PS : I can complete these tasks by hand but it requires time, some tasks might be forgotten or if the process is done by someone else all the tasks can be forgotten. The problem is that these tasks are required to have a successfull Apple validation.
Thanks
You can add script build phases at any point between the existing phases. Here is a post which shows you how. In Xcode 4 you select your project, then go to the build phases tab were you usually have the "target dependencies", "compile sources", "Link binary with libraries" and "copy bundle resources" phases, just click on "Add build phase" and select "Add run script". You can select your shell, but not script in python from there.
You can also look at building xcode projects from the command line. You could call that command line from a python script and do anything else you want from "outside" the Xcode project.

Terminal Command or Apple Script to run XCode?

Is is possible to make XCode run by executing an AppleScript or some sort of terminal command?
Can you pass XCode startup arguments, like a project to open, or to build a project on startup?
Edit:
Please excuse my laziness, but Apple Script samples are appreciated.
It's fairly simple to run Xcode from the Terminal: open -a Xcode to simply open it, or open yourproject.xcodeproj to open your project in Xcode. As for getting it to build on startup, you'd probably have to turn to AppleScript for that:
tell application "Xcode"
build
launch
end tell

Resources