To answer your question, I will use a situation that happened to me and worked very well in the end, although it seems a little laborious, then it becomes 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, you can do this?
Good to start, I think it is worth downloading a project that already uses this type of resource in Xcode, I suggest using the Facebook POP why it was from him that I learned to use the settings for specific targets in the project.
The commit I used for example is : 038f29b2de47db3fce803c5a180eee604ebe1977
.
After downloading the project, open Workspace and select the project file, select the tab "Info" and see the item "Configurations".
This item lists the amount of configuration types that the Xcode will use to compile your project, you can duplicate as many as you need from the two default settings that are Debug and Release.
In this example I did the following:
- Local & Development replica of Debug
- Staging & Production replica of Release
For each configuration, you can also add the extension file xcconfig
, this file is a kind of modifier of the listed configuration. In this example, there will be the need to edit some parameters via xcconfig
that are shared between targets, and in other cases, each target will have its own particularity, example, PRODUCT_NAME
is the attribute that defines the name that appears on the iOS home screen, so that each version of the app appears with a different name on the home screen, need to point a xcconfig
for the respective configuration.
It is also possible to give include
of a xcconfig
on the other and that’s what makes it possible to share parameters between them, if you see in Pop, the file Base-iOS.xcconfig
for example is exactly that.
Good continuing, assuming we create a xcconfig
for each target, I can now define an individual name for each target thus:
PRODUCT_NAME=App Local // Dentro do `xcconfig` de local
PRODUCT_NAME=App Dev // Dentro do `xcconfig` de development
PRODUCT_NAME=App Stg // Dentro do `xcconfig` de staging
PRODUCT_NAME=Appname // Dentro do `xcconfig` de production
This is just one example, you can do a lot more, such as creating a pre-processing macro that compiles your app with profile for local, dev, stg, Prod. The code will continue shared but the compiler will ignore the parts that do not matter to you in certain environments.
In Pop it’s very clear that, because it uses exactly the same source code for 3 different targets and it shares not only Static lib but also uncompiled code.
One more thing, in this example I gave, the biggest inconveniences were that first, I had to create an Appid in the Developer portal for each configuration, second, I had to create a provisioning profile for each of the settings too, in case of testing In-App Purchases I also had to create a "beta" from the iTunes Connect app.
In the end I had 4 apps that were the same source code, with some differences concerning the environment profile, running on the same device.
Maybe you don’t need so much, but the architecture used with the xcconfig
+ the configuration profiles, may be enough for you.
Bonus
If you use entitlements in your project, you will need to run a script for it to be copied to your target, because if you do this setup in your Xcode, for it remains an executable (.app) and not multiple.
The script below you can add as a new Script run 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 it helped.
I think what you really want to do is, create a target like "framework", take a look at that link maybe I can help.
– Jan Cássio
I do not know if it is a framework. But it would be a way to reflect the inclusion of a new class in a target template, and automatically occur a include in the other targets already created.Although Xcode offers this option when we create a class, I would like it to be automated, not manual.
– Tiago Amaral
So what you really want is, to have more than one target in the same project, these targets share the same code, each target is compiled according to the Xcode configuration it uses, is that it? If it is, yes it is possible and yes I have the answer, because I’ve used something like this in a project I developed.
– Jan Cássio
Yes, the important thing is that I can share new files to all targets automatically, as it will be many verses. And with that reduce production time and minimize failures like "ah forgot to copy that class!"
– Tiago Amaral
I had a mega rush day (and I’m still in the rush), I’ll write the answer now.
– Jan Cássio