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 .