In iOS 8, when we create a new extension, we have to decide which target it is attached to. The extension will have the same bundle ID's prefix as the target.
- Is there any way to change the target afterward?
- If my project contains 2 (or more) targets (for example one for debug/simulator, one for production/device), what's the best way to work with extensions? Do I need to create another extension and duplicate the code (very bothersome to keep the same code for both targets)?
To share one widget among a lot of targets one should only add widget.appex target
to Embedded Binaries
for every parent target in General
configuration tab
Then you'll get Embed App Extensions
area at Build Phases
automatically
It seems like you should be able to just duplicate the Extension target with its own Info.plist, but not anything else.
However, when you create an Extension, Xcode adds "Embed App Extensions" to the Build Phases of the app's target, as seen below, and there's no UI to do that yet.
Still, you can create the extension for the second target, then delete all the files except the .plist, and fix what needs to be fixed. Here's a step-by-step:
- Create "Extension 1" for "Target 1"
- Create "Extension 2" for "Target 2"
- Delete all files created for "Extension 2", except its Info.plist
- Make the "Build Phases" for "Extension 2" target the same as the build phases for "Extension 1". Usually that's adding the necessary .m files to the "Compile Sources" phase, and resources to the "Copy Bundle Resources" phase
I have create a Run Script to support this requirement
#!/bin/sh
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${SRCROOT}/ImagePush/Info.plist"
buildVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$INFOPLIST_FILE")
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $buildVersion" "${SRCROOT}/ImagePush/Info.plist"
buildID=${PRODUCT_BUNDLE_IDENTIFIER}
/usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier $buildID.ImagePush" "${SRCROOT}/ImagePush/Info.plist"
ImagePush is my Extension
add to the target which one you need and add make sure this script run before your extension setting in Build Phases, then you just need to do the build action twice (PS: first time it will fail, will try to improve) and it will support multiple target
This is my setup: I have 3 targets (production, staging, local) and an extension target that I don't want to duplicate 3 times.
Just to clarify Neo Chen's answer, edit each of your parent targets' schemes:
Build > Pre-actions > New Run Script Action > Provide build settings from (parent scheme).
Paste this for each extension:
#!/bin/bash
buildID=${PRODUCT_BUNDLE_IDENTIFIER}
extId="notification-service"
/usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier $buildID.$extId" "${SRCROOT}/${extId}/Info.plist"
Seems to work on first build.
You need to create multiple extensions for each ID, but you can create dynamic framework and just link it with each extension. Then you will not need to duplicate your code.