Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

8secz-johndpope commented Jul 23, 2020

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

bogdancochioras commented Feb 5, 2021

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

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