How do I configure files for many Targets?

1

How can I configure Xcode to perform the following task:  Use 1 Target as a template for other targets, and some file / class to be included or changed in this template, can be referenced in other targets already created.

And each file / class that is inserted in Target "child" is not referenced by the other targets, only in the one where it was created.

    
asked by anonymous 21.05.2015 / 04:09

1 answer

1

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,youcanalsoaddthexcconfigextensionfile,thisfileisakindofconfigurationmodifierlisted.Inthisexample,itwillbenecessarytoeditsomeparametersviaxcconfigthataresharedbetweenthetargets,andinothercases,eachtargetwillhaveitsparticularity,egPRODUCT_NAMEistheattributethatdefinesthenamethatappearsinthehomescreenofiOS,sothateachversionoftheappappearswithadifferentnameonthehomescreen,Ineedtopointaxcconfigtotherespectivesetting.

Itisalsopossibletogiveincludeofaxcconfigtotheotherandthisiswhatmakesitpossibletoshareparametersbetweenthem,ifyouseeinPop,theBase-iOS.xcconfigfileisexactlythat.

Wellcontinuing,assumingwecreateaxcconfigforeachtarget,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.

    
23.05.2015 / 01:37