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

這篇教程針對(duì)單個(gè)工程創(chuàng)建多個(gè)Targets/Apps,是2009年一篇舊博文的后續(xù).
如果同一個(gè)應(yīng)用有兩個(gè)或兩個(gè)以上版本,例如精簡(jiǎn)版和專業(yè)版轻猖,或是一個(gè)針對(duì)不同國(guó)家開(kāi)發(fā)的多版本地圖應(yīng)用,那么這篇教程就非常適合你.


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" > 右擊 > 復(fù)制.
    當(dāng)復(fù)制Target后展运,默認(rèn)會(huì)生成一個(gè)"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". 重命名后活逆,你需要?jiǎng)h除工程導(dǎo)航欄中"missing"的文件,把新的plist拖入拗胜,并選擇正確的Target(Awesome Lite)復(fù)制. (備注:筆者在此并沒(méi)發(fā)現(xiàn)需刪除的"missing one").
    小坑:留意plist的路徑蔗候,如果你把它放在工程的根文件中,那么你只需要修改文件名即可(無(wú)需路徑)挤土;如果不是琴庵,就需指定全局路徑 $(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".
現(xiàn)在你就可以編譯運(yùn)行新的scheme了(雖然它和原Target一模一樣).我們將開(kāi)始修改精簡(jiǎn)版信息了误算。

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.

在編譯期間預(yù)處理代碼被用來(lái)檢測(cè)那些代碼將會(huì)被用到. 通過(guò)使用預(yù)處理代碼,你可以讓不同target 運(yùn)行不同代碼塊.
選擇"Awesome Lite"Target > Build Settings > Preprocessing > Preprocessor Macros > 添加TARGET_LITE 到每個(gè)配置選項(xiàng)中(包括Debug 和 Release).
注意:你必須修改所有的配置.默認(rèn)是Debug 和 Release, 但是如果你創(chuàng)建了一個(gè)"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.

針對(duì)所有的資源(除了Assets Catalog外), 重點(diǎn)在于修改他們的Targets.
選擇resource > File Inspector > Target Membership > 勾選對(duì)應(yīng)Targets.
針對(duì)Assets Catalog, 你需要為每個(gè)Target創(chuàng)建一個(gè)新的Assets Catalog咖杂,因?yàn)槟銦o(wú)法針對(duì)每張圖片對(duì)應(yīng)修改Target關(guān)系.
在新的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 無(wú)法正常使用,你可以讀讀這篇博文.
我把以下部分添加到我的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 .

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末诉字,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子知纷,更是在濱河造成了極大的恐慌壤圃,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件琅轧,死亡現(xiàn)場(chǎng)離奇詭異伍绳,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)乍桂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門冲杀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人睹酌,你說(shuō)我怎么就攤上這事权谁。” “怎么了憋沿?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵旺芽,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我辐啄,道長(zhǎng)采章,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任则披,我火速辦了婚禮共缕,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘士复。我一直安慰自己图谷,他們只是感情好翩活,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著便贵,像睡著了一般菠镇。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上承璃,一...
    開(kāi)封第一講書(shū)人閱讀 51,125評(píng)論 1 297
  • 那天利耍,我揣著相機(jī)與錄音,去河邊找鬼盔粹。 笑死隘梨,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的舷嗡。 我是一名探鬼主播轴猎,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼进萄!你這毒婦竟也來(lái)了捻脖?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤中鼠,失蹤者是張志新(化名)和其女友劉穎可婶,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體援雇,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡矛渴,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了熊杨。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片曙旭。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖晶府,靈堂內(nèi)的尸體忽然破棺而出桂躏,到底是詐尸還是另有隱情,我是刑警寧澤川陆,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布剂习,位于F島的核電站,受9級(jí)特大地震影響较沪,放射性物質(zhì)發(fā)生泄漏鳞绕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一尸曼、第九天 我趴在偏房一處隱蔽的房頂上張望们何。 院中可真熱鬧,春花似錦控轿、人聲如沸冤竹。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)鹦蠕。三九已至冒签,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間钟病,已是汗流浹背萧恕。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留肠阱,地道東北人票唆。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像辖所,于是被迫代替她去往敵國(guó)和親惰说。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容