Xcode: XCConfig files for managing targets configurations


Let’s take a look how XCConfig files can be useful when working on multiple targets with some differences in configuration.

I browsed mozilla/firefox-ios repository today with plan that I’ll learn something new, and I found that they are using a lot of Configuration Settings Files.

I used xcconfig files in the past in some project, but do not use them in current project that I am working on daily. In the project there are multiple targets with some differences in configuration and I was thinking how could I manage these things efficiently and easily.

Use case

The project has been inherited by my team. Customer’s team worked on it first for like half a year and decided to outsource it completely. One of the poor things of this project are targets with differences in configurations and how the problem has been solved - or better, not yet solved.

The project consists of 10 application targets, 2 aggregate targets that do some stuff and 1 testing target. Every target is using different backend and different pair of let’s say “api keys” and other keys like tokens for hockeyapp. Every target has own preprocessor macro like “TARGET_A”, “TARGET_B”, etc.. (fictional names). Next, details like tokens, api keys, urls of backends are stored in plist file, so naturally there need to be some class that is wrapper for this file and can parse it and give us proper keys. And this class has over 200 lines of code, which for me is a way to much just for reading such data.

So, I thought that maybe I could use xcconfig files and keep this simple, instead of using parsers and juggle of 10 preprocessor macros (one per target) to decide which values from plist file should be returned. You can find my solution below. It might not be the best one, but for this moment it is. If you have better one, I’ll willingly read about it :)

Overview

The idea is to have few levels of configuration files. First level is for storing most common data, second level is for differentiating between debug and release modes and last level is for settings related to specific targets.

Common.xcconfig

This file stores informations like app name, app version and bundle version, and other most commmon settings that are the same for debug and release targets.

//
//  Common.xcconfig
//  <truncated>
//

APP_NAME = App
APP_VERSION = 1.6
APP_BUNDLE_ID = 153

Changing app version and bundle for every target may be time consuming when considering doing it for 10 targets. Other option may be creating Aggregate target that could update Info-plist files every Cmd+B but I would like to avoid this and do not complicate the project more than it is now.

Common.debug and Common.release

These files can store most common configuration for both debug and release targets. Files includes Common.xcconfig and can override its variables. E.g. you can easily change app’s name for every debug target to “App Debug” by overriding one variable. This is also good place to store common API Keys that are used by each develop or release targets.

Tip: Using custom configuration files and CocoaPods

If you’re using CocoaPods you should include Pods.debug.xcconfig or Pods.release.xcconfig respectively in one of your configuration files. I recommend to set your configuration file first in Project Info tab and the execute pod install to let the Pods project reconfigure. After installation is ended you’ll be prompted to include one of Pods’ configuration files to your file.

Error:

CocoaPods did not set the base configuration of your project because your project already has a custom config set. In order for CocoaPods integration to work at all, please either set the base configurations of the target TARGET_NAME to Pods/Target Support Files/Pods/Pods.debug.xcconfig or include the Pods/Target Support Files/Pods/Pods.debug.xcconfig in your build configuration.

//
//  Common.debug.xcconfig
//  <truncated>
//

#include "Common.xcconfig"
#include "Pods/Target Support Files/Pods/Pods.debug.xcconfig"

APP_NAME = App Debug
API_KEY_A = API_KEY_HERE
API_KEY_B = API_KEY_HERE

PerTarget.xcconfig

I do not need debug/release configuration files on this level (because of other legacy issues in the project), so I just have PerTarget.xcconfig files that include proper Common.debug.xcconfig or Common.release.xcconfig file to it. But better would be to have debug and release configuration files. On this level you can keep things related to a specific target.

//
//  Develop.xcconfig
//  <truncated>
//

#include "Common.debug.xcconfig"

BACKEND_URL = http:\/\/develop.api.szulctomasz.com
SOME_KEY_A = VALUE_HERE
SOME_KEY_B = VALUE_HERE

Accessing variables

All configuration data is stored. Now is time to use them. With so many targets like in my case I can reduce number of Info.plist files to just 1 instead of having multiple files because all differences are in xcconfig files now.

You can see that after you build the app with such configuration files there will be some values in Project’s Build Settings in “User-Defined” section.

If you want to use variable from configuration file e.g. in Info.plist file of a target you have to use this pattern: $(VARIABLE). This way you can set “Bundle Identifier”, “Bundle name”, “Bundle version” and other stuff you need to configure.

Accessing other variables in the code looks different and a way that I found the easiest one is to create additional fields in Info.plist with the same name as a variable and assign value to them using pattern presented above. Then you can read the value in the code.

if let dictionary = NSBundle.mainBundle().infoDictionary {
    let appName = dictionary["APP_NAME"] as! String
    let appVersion = dictionary["APP_VERSION"] as! String
    let appBuildVersion = dictionary["APP_BUILD_VERSION"] as! String
    print("\(appName) \(appVersion) (\(appBuildVersion))")

    let backend = (dictionary["BACKEND_URL"] as! String).stringByReplacingOccurrencesOfString("\\", withString: "")
    print("backend: \(backend)")
}

Here is the tomkowz/demo-xcconfig repository where you can find example of using xcconfig files.

Conclusion

Xcode Configuration Settings files give easy way of configuring targets and allow to maintain configurations easily. In my use case it is great to switch to these files, because now maintenance of configurations is really easy compared to what I had before using this solution.

From:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,671评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,442评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,524评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,623评论 1 275
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,642评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,584评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,953评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,621评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,865评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,608评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,698评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,378评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,958评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,940评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,173评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,419评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,425评论 2 342

推荐阅读更多精彩内容