原文:http://www.codza.com/free-iphone-app-version-from-the-same-xcode-project

There are more than 15,000 apps in the app store and hundreds are added daily. You need a way to show off your awesome creation to the millions of potential buyers.

One of the most effective marketing tools for small fish is releasing a free version of the app. To do that, you could copy the whole project, but then you would be duplicating code, maintaining it in two places and what if there’s an update?

In this post I’ll show you a better way: building two versions of your app from the same xcode project using targets and some other magic. Source code for the sample project is available here:twoForOne.

I’ve built a very simple app, it displays an image in UIImageView and has two buttons. Looks like this:

This is the pay-for version. The free version will have a different image (blue) and the expensive button will be hidden. This will demonstrate how to use a different set of resources (red image vs blue image) and how to modify code (hide button) at compile time based on which target is active.

First we’ll add a new build phase to the target. This will allow us to modify the background and icon resources later. It’s easier to add it now because we only need to do it for one target (it will be automatically duplicated with the target.) Right click on the target and select Add / New Build Phase /New Run Script Build Phase (you can click on the screen shot to see it better).

Type ./targetSetup in the script window. targetSetup is the script that does the work; we’ll create it later.

Take the Run Script build phase and move it to the top of the stack. We want this to run before any other build phase.

Now duplicate the target and name the new target twoForOneFree (right click on the target and select duplicate). (If you don’t have a right button on your mouse, go and get one. Those stupid soap bar mice are so not cool anymore… were they ever?):

Notice that xcode made a duplicate of the Info.plist file:

Info copy.plist is not a very nice name, so rename it to InfoFree.plist. You also have to tell the target to use the renamed plist, so open the info window for thetwoForOneFree target (right click on the target and choose get info- you see how much nicer it is that you have a right mouse button?), make sure that you are looking atAll Configurations andAll Settings and typeinfo into the search box. Change theInfo.plist file setting to point atInfoFree.plist:

In the same info window change the product name to twoForOneFree (this will be the name of the .app and by default this is what the iphone will display under the icon.) Typeproduct name in the search box to find the setting quickly:

The compiler doesn’t know which target is active, so we’ll define a directive that will be easy to use in code to create version specific modifications (this is what we’ll use to hide the expensive button). Still in the same window, search forother c. If you find a key called Other C Flags, you are in luck: set it to-DTWOFORONEFREEand skip the next paragraph.

If not, you need to create one. At the bottom of the window click the settings (gears) button and selectAdd User-Defined Setting and typeOTHER_CFLAGS for the key and-DTWOFORONEFREE for the value:

This instructs the compiler to define the TWOFORONEFREE macro for this target.

Look at lines 18-20 intwoForOneAppDelegate.m. We use an #ifdef directive to hide the expensive button if this macro is defined:

#ifdef TWOFORONEFREE
[expensiveButton setHidden:YES];
#endif

Obviously you;ll need to do more than hiding a button. You can use this same approach to do other modifications to the free version. You can also define aTWOFORONEmacro on thetwoForOne target for more flexibility (you can use that macro to add features that the free version doesn’t have.)

Now we’ll look at modifying resources. We want the free version to have a different bacground and also a different icon. I created two versions of the background, you can see them in theImages folder:

The UIImageView uses bg.png. The other two files, bg_red.png andbg_blue.png are not added to the xcode project (you can add them, but it’s not necessary). I want to copybg_red.png tobg.png when building the expensive version andbg_blue.png tobg.png for the free build.

Similarly, there is a Icon_red.png and Icon_blue.png at the project level, one of those will be copied toIcon.png to get the correct icon.

We already added the run script build phase that calls the targetSetup script. Now let’s create s shell script. Right click onResources(so much milage out of that new right mouse button!) and selectAdd / New File. From the dialog select theOther category (all the way at the bottom) and then theShell Script type:

In the next dialog enter targetSetup for the name and uncheck both targets (we don’t want this script to be copied into any of the targets at build time):

The script itself is very simple. It copies the correct bg file and Icon file based on the selected target (the build process conveniently sets an environment variable with the target name).

#!/bin/tcsh -f

echo .targetSetup: $TARGET_NAME

if ($TARGET_NAME == “twoForOne”) then
cp Icon_red.png Icon.png
cp Images/bg_red.png Images/bg.png
endif

if ($TARGET_NAME == “twoForOneFree”) then
cp Icon_blue.png Icon.png
cp Images/bg_blue.png Images/bg.png
endif

#

That’s it! Just set the desired target and you can build the expensive or free version from the same project:

Here are some screenshots. You can see that even the icons are different (as expected):

You can download the twoForOne xcode project here

xcode多target相关推荐

  1. Xcode多Target设置

    有时候一个项目会分为多个版本,比如免费版.收费版,或者对于不同的客户定制不同版本.但是大体上功能都是差不多,只是部分页面稍有区别.如果每个版本都建一个工程又显得麻烦了,都放在一个 Target 又得写 ...

  2. Making Your Own iPhone Frameworks. In Xcode

    Back in April Oliver wrote an excellent article entitled "Making Your Own iPhone Frameworks&quo ...

  3. Xcode - VALID_ARCHS

    背景 使用模拟器编译时报如下错误: ld: in /Users/xxx/Documents/xx/svn/xxx/xxx/External/Captuvo/libCaptuvo SDK.a(Captu ...

  4. xcode APP 打包以及提交apple审核详细流程(新版本更新提交审核)

    版权声明:转载自:http://blog.csdn.net/mad1989/article/details/8167529 目录(?)[+] 上传时出错Application failed codes ...

  5. ios5 ARC机制介绍和使用

    参考http://www.yifeiyang.net/development-of-the-iphone-simply-1/ http://blog.csdn.net/diyagoanyhacker/ ...

  6. 【新手教程】如何向App Store提交应用

    作者:Bart Jacobs 当你克服重重困难终于开发出了自己的App,下一步就是向App Store提交应用了,这时应该如何操作呢?我的App真的准备好提交了?我敢肯定这些问题将会浮现在你的脑海.基 ...

  7. 【HIMI转载推荐之三】新手教程之如何向APP STORE提交应用

    当你克服重重困难终于开发出了自己的App,下一步就是向App Store提交应用了,这时应该如何操作呢?我的App真的准备好提交了?我敢肯定这些问题将会浮现在你的脑海.基于这篇教程,我将告诉你一个完整 ...

  8. iOS自动化探索(九)使用Jenkins自动化打包并发布iOS App

    继前一篇: Mac环境下安装Jenkins Jenkins安装好后, 我们试着创建一个iOS自动打包并发布的任务 iOS App构建必须在MAC上面使用xcode进行,所以我们要安装下xcode集成插 ...

  9. Prepare for Mac App Store Submission--为提交到Mac 应用商店做准备

    返回 Mac App Store Prepare for Mac App Store Submission 提交到Mac 应用商店之前的准备 Most of your time is spent on ...

最新文章

  1. javafx 调用java_Java“地铁”表(JavaFX)
  2. 14. 最长公共前缀
  3. hibernate中对象的3种状态----瞬时态、持久态、脱管态
  4. HDU4472_Count
  5. 服务器添加管理员隐藏账号,绝招:隐藏管理员账号 三分钟搞定
  6. 程序员吐槽:组里新来一个“加班狂”,可把大家害惨了
  7. 上楼梯算法的java实现(转)
  8. require-ensure
  9. Javaweb鼠标事件案例分析—鼠标移入移出表格颜色变化,kafka面试题
  10. android 自动安装 解析包错误,安卓android手机安装包频繁提示解析错误解决方法...
  11. Tencent Kona JDK11无暂停内存管理ZGC生产实践
  12. Java排序之归并排序 1
  13. 肝完这篇 TCP/IP ,我就去面试去。
  14. 【嵌入式Linux驱动开发】二十一、Linux内核自带的KEY驱动探索
  15. 中国大学MOOC-陈越、何钦铭-数据结构-2019夏期末考试(含答案)
  16. abb机器人旋转六轴角度指令_ABB-120型号机器人的6个轴运动的角度范围各是多少?...
  17. Educational Codeforces Round 141 (Rated for Div. 2) 赛时口胡思路(青大acmer训练日常)小上一波分
  18. 高考英语68分,大一通过英语四六级,考研英语80分!做到这些,你也可以!
  19. 华为设备的AP与AC的配置
  20. 在matlab上实现遗传算法解决TSP旅行者问题

热门文章

  1. Spring Boot特性
  2. Google DeepMind 声称在合成语音领域取得突破
  3. MYSQL服务的极简免配置快绿色速安装法[适合新手和懒人]
  4. Mac 装Sequel pro 连接 Mysql 8.0 失败、登录不了、loading问题
  5. 危机四伏的千亿级金融放贷市场,我们能做什么?
  6. Java 基础 之 常量
  7. ReDim Preserve 的用途
  8. Python的GIL是什么鬼,多线程性能究竟如何
  9. 【并行编程】系统体系结构和组件具体说明
  10. 英特尔媒体年会场景(15P)——实拍与小结