譯文:How to build multiple targets from a single Xcode Project

本文翻譯自O(shè)megasoft:http://www.omegasoftweb.com/omega/blog.cfm?p=43

How to build multiple targets (as in Lite Version and Full Version of an iPhone or iPad app) from a single Xcode Project

譯文:怎樣在單一XCode工程中創(chuàng)建多Targets

先挖了一個(gè)坑在這有時(shí)間過來填一下,文章有點(diǎn)老不如Xcode版本什么的嘉抒,但是思路是一樣的沈贝。翻譯拙劣眶蕉,望指正填帽!

There are times you may find yourself needing to create multiple variations of an Xcode project, the most common example of which being a Lite Version and a Full Version of an iPhone app or iPad app. One option to accomplish this is to simply copy the project folder and rename it, working with each project separately from that point on. This is not a good idea, as now you have twice the work to manage things going forward, and confusions are bound to arise. My friend Smasher puts it succinctly: “in that path lies madness!”, and with this he is correct.

譯文:有些時(shí)候你可能需要給你的公程創(chuàng)建一些變體蛛淋,最常見的情形就是一個(gè)iOS App 的精簡(jiǎn)版和完整版。 一種方法是把這個(gè)工程拷貝一份然后重命名篡腌,然后分別處理褐荷。這當(dāng)然不是一個(gè)好主意,因?yàn)楝F(xiàn)在你有了雙倍的工作去做嘹悼,而且緊接著會(huì)產(chǎn)生一大堆問題叛甫。我的朋友Smasher精辟的說:“這么做簡(jiǎn)直是瘋了!”杨伙,很顯然他是對(duì)的其监。

A much better plan is to configure your single Xcode project with multiple targets, so that you can develop multiple versions of the project concurrently and selectively build the one you need at a given moment. Here is a step by step guide to do just this, synthesized from the info in various threads at iphonedevsdk coupled with personal trial and error, and using the case of Lite Version / Full Version:

更好的方法是把你的公程設(shè)置成一個(gè)多Target的公程,這樣一來你就可以同時(shí)開發(fā)不同的版本而且可以隨時(shí)編譯任意一個(gè)限匣。以下就是一個(gè)分步的指導(dǎo):

Step 1: Make a copy of your original target by right clicking the existing target and selecting “duplicate”. Rename this target to YourAppLite

Step1:在你現(xiàn)有的target上右鍵選擇“duplicate”復(fù)制一份出來抖苦,然后重名成“YourAppLite”

Step 2: When you created the second target; a new pList file should have appeared in the resources folder YourApp-info_copy.pList or something similar; rename this to YourAppLite-info.pList

Step2:當(dāng)你復(fù)制玩這個(gè)target后,你會(huì)發(fā)現(xiàn)一個(gè)新的plist文件米死。名字是“YourApp-info_copy.pList”或者是其他一些類似的名字锌历,把它重命名成“YourAppLite-info.pList”

Step 3: Edit this new pList to have a different Bundle Identifier, com.yourcompany.YourAppLite, for example. If you do not do this both versions of your app will be seen as the same app instead of two unique ones, and when you build the light version it will overwrite your full version in the simulator or testing device.

Step3:編輯這個(gè)新的plist文件,修改Bundle Identifier哲身,舉個(gè)例子修改成“com.yourcompany.YourAppLite”辩涝。如果不修改那么這兩個(gè)版本就還是同一個(gè)app而不是兩個(gè)獨(dú)特的app,當(dāng)你編譯這個(gè)精簡(jiǎn)版的時(shí)候就會(huì)覆蓋你的模擬器或者真機(jī)設(shè)備上的完整版本勘天。

Step 4: If you are going to use a different application icon for the light version (as you should), edit this new pList’s Icon file property to reference a different icon from the full version, IconLite.png for example. Obviously you must create this asset and add it to your project for this new icon to appear for the lite version.

Step 4:如果你想讓你的精簡(jiǎn)版本適用不同的應(yīng)用圖標(biāo)的話怔揩,就要編輯這個(gè)plist文件的Icon file屬性指向一個(gè)新的圖標(biāo)。很顯然你必須為你的精簡(jiǎn)版本圖標(biāo)建一個(gè)新的asset并添加到的你的工程當(dāng)中脯丝。

Step 5: Highlight the new target (YourAppLite) and right click to “Get Info”, then navigate to the build tab and set Configuration to “All Configurations”. Now scroll down a bit until you find the “Info.plist file” parameter under the Packaging group; alter this to reference the new lite version pList we created above instead of the original pList.

Warning-Potential Gotcha:You may have noticed when you duplicated the target another product with the same name as the first appeared in the products folder. You may, like I was, be tempted to name the second one YourAppLite and change some properties in the pList to reflect this. Do NOT do this; there appears to be a bug which makes the debugger unable to launch the executable when you build. Furthermore, once you have done this even if you change the product name back it still gives the error. Perhaps this was just me being silly, but others have run into a similar issue; at any rate it is not necessary to change the product name for everything to work correctly, so I recommend leaving it alone.

—————————————-INTERMISSION—————————————

Now you should be all ready to built multiple targets, but there are still some things we want to do to make managing both branches as seamless as possible. Go grab a drink, then resume the adventure.

——————————————————————————————————-


Step 6: Once again highlight the new target (YourAppLite) and right click to “Get Info”, then navigate to the build tab and make sure Configuration is still on “All Configurations”. Now scroll down quite a ways until you find the “Other C Flags” parameter under the GCC Language group; alter this to be “-DLITE_VERSION” (the leading -D is necessary). What this does is allow you to do things like:

#ifdef LITE_VERSION

// Code unique to your lite version

#else

// Code unique to your full version

#endif

in your code, just like a normal if statement.

Step 7: For any project resources that only need to be in the lite version OR the full version, you can “Get Info” on the file and then navigate to the “Targets” tab, deselecting the unnecessary target. This will help to keep each respective bundle size down, and is especially useful for large packages like adMob (which you’d likely only want in your lite version). Keep in mind that removing assets from a target in this way will likely coincide with the type of branching code described in step 6.

Step 8 (Optional): If you want a separate Default.png file for your lite version, first create the file and name it DefaultLite.png and add it to your project along side the regular Default.png. Now, right click on your YourAppLite target, select Add –> New Build Phase –> New Run Script Build Phase and paste the following in the script field:

mv ${TARGET_BUILD_DIR}/${CONTENTS_FOLDER_PATH}/DefaultLite.png ${TARGET_BUILD_DIR}/${CONTENTS_FOLDER_PATH}/Default.png

Now close the window and expand the arrow next to YourAppLite target. You should see the run script you created, now grab it and move it just below “Copy Bundle Resources”. This will overwrite your Default.png with the DefaultLite.png at run time when you are building your lite version, just as you want.

Warning-Potential Weird Gotcha: Whenever I duplicate a target I notice that any Library Search Paths get slightly altered, so if you are using Library Search Paths and are getting strange errors, make sure you have the same values there in your Lite and Full version targets.

———————————————-Victory!——————————————–

And there you have it; multiple targets at your fingertips. Please feel free to leave some comments or point out any mistakes or gotchas. I hope you found this useful; good luck!

——————————————————————————————————-

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末商膊,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子宠进,更是在濱河造成了極大的恐慌晕拆,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,376評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件材蹬,死亡現(xiàn)場(chǎng)離奇詭異实幕,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)堤器,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門昆庇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人闸溃,你說我怎么就攤上這事整吆」澳欤” “怎么了?”我有些...
    開封第一講書人閱讀 156,966評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵表蝙,是天一觀的道長(zhǎng)拴测。 經(jīng)常有香客問我,道長(zhǎng)府蛇,這世上最難降的妖魔是什么集索? 我笑而不...
    開封第一講書人閱讀 56,432評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮欲诺,結(jié)果婚禮上抄谐,老公的妹妹穿的比我還像新娘渺鹦。我一直安慰自己扰法,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,519評(píng)論 6 385
  • 文/花漫 我一把揭開白布毅厚。 她就那樣靜靜地躺著塞颁,像睡著了一般。 火紅的嫁衣襯著肌膚如雪吸耿。 梳的紋絲不亂的頭發(fā)上祠锣,一...
    開封第一講書人閱讀 49,792評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音咽安,去河邊找鬼伴网。 笑死,一個(gè)胖子當(dāng)著我的面吹牛妆棒,可吹牛的內(nèi)容都是我干的澡腾。 我是一名探鬼主播,決...
    沈念sama閱讀 38,933評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼糕珊,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼动分!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起红选,我...
    開封第一講書人閱讀 37,701評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤澜公,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后喇肋,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體坟乾,經(jīng)...
    沈念sama閱讀 44,143評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,488評(píng)論 2 327
  • 正文 我和宋清朗相戀三年蝶防,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了甚侣。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,626評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡慧脱,死狀恐怖渺绒,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤宗兼,帶...
    沈念sama閱讀 34,292評(píng)論 4 329
  • 正文 年R本政府宣布躏鱼,位于F島的核電站,受9級(jí)特大地震影響殷绍,放射性物質(zhì)發(fā)生泄漏染苛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,896評(píng)論 3 313
  • 文/蒙蒙 一主到、第九天 我趴在偏房一處隱蔽的房頂上張望茶行。 院中可真熱鬧,春花似錦登钥、人聲如沸畔师。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)看锉。三九已至,卻和暖如春塔鳍,著一層夾襖步出監(jiān)牢的瞬間伯铣,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來泰國(guó)打工轮纫, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留腔寡,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,324評(píng)論 2 360
  • 正文 我出身青樓掌唾,卻偏偏與公主長(zhǎng)得像放前,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子郑兴,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,494評(píng)論 2 348

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