Receigen provides a way to integrate directly into the Xcode build process. The integration is done with a custom build phase, that generates the validation code if needed in a transparent way.
Go the Editor > Add Build Phase and lick on the Add Run Script Build Phase item. Rename the new Build Phase to identify it easier.
Then, move it before the Compile Sources phase, so the generation occurs before the sources are compiled. The custom build phase contains a script that will check if the generation is needed, and if yes, the script will call the Receigen application.
The example below shows how to proceed to generate the receipt validation. Please note that it contains lines for both OS X and iOS, so you have to remove the irrelevant lines. Then paste the script in the custom build phase:
#!/bin/bash
# Receigen binary
RECEIGEN="/Applications/Receigen.app/Contents/MacOS/Receigen"
# Set your own prefix
PREFIX="ReceiptValidation"
# Extract Info.plist information
INPUT="$INFOPLIST_FILE"
BUNDLE_ID="$PRODUCT_BUNDLE_IDENTIFIER"
# On OS X, use CFBundleShortVersionString
BUNDLE_VERSION=`/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$INPUT"`
# On iOS, use CFBundleVersion
BUNDLE_VERSION=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INPUT"`
# Expand information if needed
EXPANDED_BUNDLE_ID=`eval "echo $BUNDLE_ID"`
EXPANDED_BUNDLE_VERSION=$(eval "echo `echo $BUNDLE_VERSION | tr -d '()'`")
# Make sure the destination directory exists
mkdir -p "$DERIVED_FILES_DIR"
HEADER="$DERIVED_FILES_DIR/receipt.h"
# Check if the generation is needed
if [ -e "$HEADER" ]; then
# On OS X, check CFBundleIdentifier and CFBundleShortVersionString
SKIP=`egrep -q "CFBundleIdentifier.+:.+$EXPANDED_BUNDLE_ID" "$HEADER" && egrep -q "CFBundleShortVersionString.+:.+$EXPANDED_BUNDLE_VERSION" "$HEADER" && echo "YES"`
# On iOS, check CFBundleIdentifier and CFBundleVersion
SKIP=`egrep -q "CFBundleIdentifier.+:.+$EXPANDED_BUNDLE_ID" "$HEADER" && egrep -q "CFBundleVersion.+:.+$EXPANDED_BUNDLE_VERSION" "$HEADER" && echo "YES"`
fi
# Generate the header file if needed
if [ "x$SKIP" = "x" ]; then
# If this variable is set, use the StoreKit test certificate
if [ "$STOREKIT_TEST" = "YES" ]; then
# Convert certificate from DER to PEM
CA_CER="$PROJECT_DIR/$PROJECT/StoreKitTestCertificate.cer"
CA_PEM="`openssl x509 -inform DER -in "$CA_CER"`"
# Only keep the Base64 part
CA_PEM_CONTENT=`echo $CA_PEM | grep -v "\-" | tr -d '\n'`
CA_OID=2.5.29.37
# On OS X, call Receigen with options
"$RECEIGEN" --identifier "$EXPANDED_BUNDLE_ID" --version "$EXPANDED_BUNDLE_VERSION" --os osx --prefix "$PREFIX" --signer-oid $CA_OID --root-ca-content $CA_PEM_CONTENT > "$HEADER"
# On iOS, call Receigen with options
"$RECEIGEN" --identifier "$EXPANDED_BUNDLE_ID" --version "$EXPANDED_BUNDLE_VERSION" --os ios --prefix "$PREFIX" --signer-oid $CA_OID --root-ca-content $CA_PEM_CONTENT > "$HEADER"
else
# On OS X, call Receigen with options
"$RECEIGEN" --identifier "$EXPANDED_BUNDLE_ID" --version "$EXPANDED_BUNDLE_VERSION" --os osx --prefix "$PREFIX" > "$HEADER"
# On iOS, call Receigen with options
"$RECEIGEN" --identifier "$EXPANDED_BUNDLE_ID" --version "$EXPANDED_BUNDLE_VERSION" --os ios --prefix "$PREFIX" > "$HEADER"
fi
fi
Of course, you may adapt the command line parameters in order to pass other options.
Add an output file $(DERIVED_FILES_DIR)/receipt.h
.
The output file will be placed in the derived sources folder so you don’t need to explicitly add the result file into the project.
You can choose to put the output file in your source tree if you need to inspect the result of the code generation.
Then, you can import the generated header file in your project like any other include file.