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 .