To answer your question, I'm going to use a situation that happened to me and it worked out very well in the end, although it seems a bit difficult, but it's very simple.
Let's imagine the following situation: Your app interacts with 4 environments, local , development , staging and production > and you want Xcode to export 1 app to each environment but you want to have the 4 versions on your device, can you do that?
Well to start, I think it's worth downloading a project that already uses this kind of feature in Xcode, I suggest using the Facebook POP because it was from there that I learned how to use settings for specific targets in the project.
The commit I used to give examples is: 038f29b2de47db3fce803c5a180eee604ebe1977
.
After downloading the project, open the workspace and select the project file, select the "Info" tab and see the "Configurations" item. >
ThisitemliststheamountofconfigurationtypesthatXcodewillusetocompileyourproject,youcanduplicatehowmanyyouneedfromthetwodefaultsettingsthatareDebugandRelease.
InthisexampleIdidthefollowing:
- Local&DevelopmentreplicaofDebug
- Staging&ProductionreplicaofRelease
Foreachconfiguration,youcanalsoaddthexcconfig
extensionfile,thisfileisakindofconfigurationmodifierlisted.Inthisexample,itwillbenecessarytoeditsomeparametersviaxcconfig
thataresharedbetweenthetargets,andinothercases,eachtargetwillhaveitsparticularity,egPRODUCT_NAME
istheattributethatdefinesthenamethatappearsinthehomescreenofiOS,sothateachversionoftheappappearswithadifferentnameonthehomescreen,Ineedtopointaxcconfig
totherespectivesetting.
Itisalsopossibletogiveinclude
ofaxcconfig
totheotherandthisiswhatmakesitpossibletoshareparametersbetweenthem,ifyouseeinPop,theBase-iOS.xcconfig
fileisexactlythat.
Wellcontinuing,assumingwecreateaxcconfig
foreachtarget,Icannowdefineanindividualnameforeachonelikethis:
PRODUCT_NAME=AppLocal//Dentrodo'xcconfig'delocalPRODUCT_NAME=AppDev//Dentrodo'xcconfig'dedevelopmentPRODUCT_NAME=AppStg//Dentrodo'xcconfig'destagingPRODUCT_NAME=Appname//Dentrodo'xcconfig'deproduction
Thisisjustoneexample,youcandoalotmore,suchascreatingapreprocessingmacrothatcompilesyourprofileapptolocal,devstg,prod.Thecodewillcontinuetobesharedbecausethecompilerwillignorethepartsthatdonotinterestyouincertainenvironments.
InPopit'sprettyclearwhyitusesexactlythesamesourcecodefor3differenttargetsanditsharesnotonlythestaticlibbutalsotheuncompiledcode.
Onemorething,inthisexampleIgave,thebiggestinconveniencewasthatfirst,IhadtocreateanAppIdinthedeveloperportalforeachconfiguration,secondly,Ihadtocreateaprovisioningprofileforeachoftheconfigurationsalso,inthecaseoftestingIn-AppPurchasesalsohadtocreatean"beta" of the app in iTunes Connect.
In the end I had 4 apps that were the same source code, with some differences related to the environment profile, running on the same device.
You may not need as much, but the architecture used with xcconfig
+ configuration profiles may be enough for you.
Bonus
If you use entitlements in your project, you will need to run a script so that it is copied to your target, so if you do this setup in your Xcode, it remains an executable (.app) and not several .
The script below you can add as a new Run script in the project:
# Copy entitlements file
echo "Copy entitlements file for configuration ${CONFIGURATION} to path: ${SRCROOT}/[NOME DO ARCHIVE GERADO PELO XCODE]/$PRODUCT_NAME.entitlements ${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app"
cp -r "${PROJECT_DIR}/[NOME DO ARCHIVE GERADO PELO XCODE]/$PRODUCT_NAME.entitlements" "${BUILT_PRODUCTS_DIR}/$PRODUCT_NAME.app"
I hope you have helped.