How to perform file configuration for many Targets?

Asked

Viewed 158 times

1

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

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

  • I think what you really want to do is, create a target like "framework", take a look at that link maybe I can help.

  • 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.

  • 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.

  • 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!"

  • I had a mega rush day (and I’m still in the rush), I’ll write the answer now.

1 answer

1

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".

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’ll try it and tell you how it turned out!!

  • In my case, the targets will be really different apps.... I will have to replicate the same product many times. : What I’m looking for is a possible solution for support and fixes that can be applied to all apps if needed. : D So I’m looking for a way to minimize my work time for common things for all apps.

  • I would like to talk to you at any time, for me it would be a good time at night, and we can exchange an idea, that thinks?

  • Sure, next week it’s quiet, you’ll find my contacts in my Stack Overflow profile. Good weekend, hugs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.