Create Multiple Targets/Apps for 1 Xcode Project

This guide is for building muliple targets/apps for a single project, follow-up of an old post in 2009.
If you have 2 or more versions of the same app eg. a lite and a pro version, or many versions of the same map app but for different country, then this guide is for you.
Let’s use the scenario of an “Awesome” app, which I want to create “Awesome Lite”.

这篇教程针对单个工程创建多个Targets/Apps,是2009年一篇旧博文的后续.
如果同一个应用有两个或两个以上版本,例如精简版和专业版,或是一个针对不同国家开发的多版本地图应用,那么这篇教程就非常适合你.


1. Create New Target

  • Go to Project > Targets > Select the original target “Awesome” > Right click > Duplicate.
    When duplicating a target, the default generates “Awesome copy” .app, .plist, etc. We want to change that.

  • Rename the new target (select and press enter to edit) to “Awesome Lite”.

  • Under Build Settings, search for “Awesome copy”. You need to rename Product Name to “Awesome Lite”, and rename Info.plist to “Awesome Lite-info.plist”.

  • In your project, find “Awesome copy-info.plist”, and rename to “Awesome Lite-info.plist”. After renaming, you would need to delete the “missing one” in your project navigator, and drag the new one in, and select the correct target (Awesome Lite) to copy for.
    Pitfall: Take note of the path for the plist. If you place it in the root folder of the project, then you can just specify the name (no need the path). If not, specify the full path$(PROJECT_DIR)/path/to/Info.plist.

  • Project >Targets > 选择Target"Awesome" > 右击 > 复制.
    当复制Target后,默认会生成一个"Awesome copy".app, .plist, etc. 我们需要修改它.
  • 重命名新的Target(选中并输入)"Awesome Lite".
  • Build Setttings 中搜索"Awesome copy", 重命名Product Name 为"Awesome Lite", Info.plist 为"Awesome Lite-info.plist".
  • 工程中找到"Awesome copy-info.plist",将其重命名为"Awesome Lite-info.plist". 重命名后,你需要删除工程导航栏中"missing"的文件,把新的plist拖入,并选择正确的Target(Awesome Lite)复制. (备注:笔者在此并没发现需删除的"missing one").
    小坑:留意plist的路径,如果你把它放在工程的根文件中,那么你只需要修改文件名即可(无需路径);如果不是,就需指定全局路径 $(PROJECT_DIR)/path/to/Info.plist.

2. Edit Scheme

Go to Product > Scheme > Edit Scheme > change the scheme “Awesome copy” to “Awesome Lite”.
At this point, you should now be able to build and run the new scheme (even though it is exactly the same as the original target). We will modify the lite version from this point onwards.

Product > Scheme > Edit Scheme > 将scheme "Awesome copy"修改为"Awesome Lite".
现在你就可以编译运行新的scheme了(虽然它和原Target一模一样).我们将开始修改精简版信息了。

3. Edit Info.plist

Go to the new target > Info > edit these accordingly:
Bundle Display Name
Bundle Name
Bundle Identifier

找到新Target > Info ,然后修改以下信息:
Bundle Display Name
Bundle Name
Bundle Identifier

4. Writing Preprocessor Codes

Preprocessor codes are used to determine which code would be used during compile time. You may want different targets to run different section of the codes using preprocessor codes.

#ifdef TARGET_LITE
NSLog(@"Lite Verison");
#else
NSLog(@"Original Version");
#endif

Select “Awesome Lite” target > Build Settings > Preprocessing > Preprocessor Macros > Add TARGET_LITE to each of the configuration (eg both Debug and Release configurations).
Warning: You must change for all configurations. The default is Debug and Release, but let’s say you created a “Beta”, then you have to change that too.

在编译期间预处理代码被用来检测那些代码将会被用到. 通过使用预处理代码,你可以让不同target 运行不同代码块.
选择"Awesome Lite"Target > Build Settings > Preprocessing > Preprocessor Macros > 添加TARGET_LITE 到每个配置选项中(包括Debug 和 Release).
注意:你必须修改所有的配置.默认是Debug 和 Release, 但是如果你创建了一个"Beta",那么你同样也需要修改它.

5. Resources, Images and Assets Catalog

For any such resources (except Assets Catalog), the trick here is to specify their targets.
Select the resource > File Inspector > Target Membership > check the targets intended.
For Assets Catalog, you have to create for each target because you cannot specify individual target membership for each of the images in it.
You can add “New App Icon” in the new assets catalog, and simply delete the app icon in the old one.

针对所有的资源(除了Assets Catalog外), 重点在于修改他们的Targets.
选择resource > File Inspector > Target Membership > 勾选对应Targets.
针对Assets Catalog, 你需要为每个Target创建一个新的Assets Catalog,因为你无法针对每张图片对应修改Target关系.
在新的Assets Catalog 中添加"New App Icon",然后删除旧的App Icon.

6. If you are using Pods

If you are using CocoaPods, and assets doesn’t work correctly, read this pod issue.
I added the following to my Podfile and it works:

如果你使用CocoaPods, assets 无法正常使用,你可以读读这篇博文.
我把以下部分添加到我的Podfile 中,然后一切正常.

1 # Append to Podfile
2 post_install do |installer|
3 installer.project.targets.each do |target|
4 %x~ if [ ! -f Pods/#{target.name}-resources.sh.bak ]; then cp Pods/#{target.name}- resources.sh Pods/#{target.name}-resources.sh.bak; fi ~
5
6 %x~ sed '/WRAPPER_EXTENSION/,/fi\\n/d' Pods/#{target.name}-resources.sh > Pods/#{target.name}-resources.sh.temp ~
7 %x~ sed '/*.xcassets)/,/;;/d' Pods/#{target.name}-resources.sh.temp > Pods/#{target.name}-resources.sh ~
8 %x~ rm Pods/#{target.name}-resources.sh.temp ~
9 end
10 end

Tips: I haven't tested sixth, so if you get any problems ,please let me know it. :-)

This post comes from : Junda Ong - Create Multiple Targets/Apps for 1 Xcode Project .

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

推荐阅读更多精彩内容