Skip to content

Instantly share code, notes, and snippets.

@kenthumphries
Last active October 20, 2023 14:10
Show Gist options
  • Save kenthumphries/cf04683184217c7331f9c213c556c65a to your computer and use it in GitHub Desktop.
Save kenthumphries/cf04683184217c7331f9c213c556c65a to your computer and use it in GitHub Desktop.
Script to be called as part of an Xcode Run Script build phase. This will ensure that script Input File is copied (and code signed) to script Output File.
#!/bin/sh
# This script embeds (and codesigns) a framework within an iOS app binary, but only when the configuration is Debug.
# It must be called from, or copied into an Xcode Run Script build phase with following setup:
# Input Files:
# - Path to framework within project folder (source path)
# - For example: $(SRCROOT)/ThirdPartyFrameworks/SimulatorStatusMagiciOS.framework
# Output Files:
# - Desired path to framework within ${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH} (destination path)
# - For example: ${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}/SimulatorStatusMagiciOS.framework
#
# For explanation of Input & Output Files, check out: https://indiestack.com/2014/12/speeding-up-custom-script-phases/
# This script adapted from: https://stackoverflow.com/a/40484337/9051514 (thanks Ricardo Duarte!)
if [[ -z ${SCRIPT_INPUT_FILE_0} || -z ${SCRIPT_OUTPUT_FILE_0} ]]; then
echo "This Xcode Run Script build phase must be configured with Input & Output Files"
exit 1
fi
echo "Embed ${SCRIPT_INPUT_FILE_0}"
if [[ $CONFIGURATION == 'Debug' ]]; then
FRAMEWORK_SOURCE=${SCRIPT_INPUT_FILE_0}
FRAMEWORK_DESTINATION=${SCRIPT_OUTPUT_FILE_0}
DESTINATION_FOLDER=`dirname ${FRAMEWORK_DESTINATION}`
mkdir -p ${DESTINATION_FOLDER}
cp -Rv ${FRAMEWORK_SOURCE} ${FRAMEWORK_DESTINATION}
CODE_SIGN_IDENTITY_FOR_ITEMS="${EXPANDED_CODE_SIGN_IDENTITY_NAME}"
if [ "${CODE_SIGN_IDENTITY_FOR_ITEMS}" = "" ] ; then
CODE_SIGN_IDENTITY_FOR_ITEMS="${CODE_SIGN_IDENTITY}"
fi
BINARY_NAME=`basename ${FRAMEWORK_DESTINATION} .framework`
codesign --force --verbose --sign "${CODE_SIGN_IDENTITY_FOR_ITEMS}" ${FRAMEWORK_DESTINATION}/${BINARY_NAME}
echo " ✅ Embedded successfully"
else
echo " ❌ Non Debug build detected - do not embed"
fi
@rcfrias
Copy link

rcfrias commented Sep 18, 2018

not working on XCode 10, needs to be updated (NVM) I ran the wrong target

@8secz-johndpope
Copy link

I tried this too - not working for me on ios app
https://raw.githubusercontent.com/CocoaDebug/CocoaDebug/master/CocoaDebug/copy_and_codesign.sh

maybe try targeting ${CODESIGNING_FOLDER_PATH}
app_frameworks_dir="${CODESIGNING_FOLDER_PATH}/Frameworks"

@lukas2
Copy link

lukas2 commented Sep 15, 2020

If it doesn't work for you maybe you need to check your output path.

Check if you need to use CONFIGURATION_BUILD_DIR. For example my "Output Files" was: ${CONFIGURATION_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MyFancyFramework.framework

@bogdancochioras
Copy link

How would this work for xcframeworks since those are split under different architectures?

@kasrababaei
Copy link

We noticed that the script doesn't work when targeting a device. What happens is that Xcode successfully installs and runs the app when doing a clean build (or can just delete the app file from the build folder). But on a consecutive build, it shows an error when trying to install the app on the phone. Looking at the details of the error, noticed that it mentions No code signature found..

As a temporary fix, we skip running the script when targeting anything other than a simulator but I was wondering if you ever ran into it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment