"Turn" Xcode creates multiple taget of a project for easy testing and release of multiple versions

Source: Internet
Author: User

Background: Many times, we need to create multiple target in a project, that is to say we want the same code can be created two applications, put on the simulator or the real machine, or, we usually have more than N cooperative development, when the test, a here to put a write the piece, When we need to test the code written in B, we need to go to B to install it again, if there is only one target, then a will be overwritten

There are times when we need to determine whether the question of a or B code is out of the question, this time need to build a project to compile multiple versions, the following describes how to compile multiple versions in a project    well, not much gossip, the following official start: We set up a default application to demonstrate the practice: After the application is established, the following:  can be seen, there is only one target: Testtarget, in order to facilitate the demonstration, we add a multi-language, Let it in the simulator the name is Chinese we add a sentence in the infoplist.strings of the project: cfbundledisplayname= "test the official version", so that after installing in the simulator is   good, the pre-order work has been able to  1. Add a target, because here we are creating a basically consistent target, so we can choose to copy to create a new target when you choose to copy, there will be two additional files, There will also be a scheme  2 associated with this target in scheme. The above name is too ugly, we changed the name, after changing the name, you will find the re-select our changed name of the info plist file Testtarget2-info.plist file (the file name preferably contains the Info.plist, this is a convention, easy to find this file later) we'll change that scheme, too.   and the name of the generated product should be changed, Switch to TestTargetTest2 's build settings, search for product name, change product name to the name we need (note that this name is just the name of the app we generated, not necessarily the last name that was displayed, The last name to display depends on whether the user has set the cfbundledisplayname= "Test 1";)    3 in the infoplist.strings file. Switch to TestTargetTarget2 this scheme, run and see the simulator, you will find that there are two Tests in the      simulator 1,  Some classmates here did not generate two icons, This should be that he set the two target bundle identifier to the same, if the same, the generated target will overwrite the previous  4. Two target has been generated, and it has been successfully run on the simulator. How do we know which is which, two names are the same, don't takeHeart, this problem is very good to solve the   because a common infoplist.stirngs file, so, only the two target will appear in the simulator as Test 1, know the problem, then we will solve it, it is clear that We should set up a infoplist.stirngs for TestTargetTest2, because we just need the name display is different, the other does not need to change, so we can directly copy, will be the project directory of all the language Infoplist.strings a copy of the file, create a new folder (note that you need to copy all the languages infoplist.strings, because the language of the folder also has to have) because we here, there is only one language, So we have only one En.lproj folder in the TargetTest2, the TargetTest2 folder under the infoplist.strings of the      will add the files just added to the project to add, Remove the targetTest2 from the target of the original infoplist.stirngs as:  modify the contents of Infop.stirngs in the directory TargetTest2 to test 2 5. First choose Testtarget This scheme, run, in the simulator will appear test 1, switch scheme for TestTarget2, run, on the simulator will generate test 2 switching scheme All right after the simulator will be displayed     6. As an appendix, in fact, it is also more important that we have a lot of target common code, resources, etc., sometimes we need to distinguish in the code exactly which target, for example, we generated a second target is a restricted version, we need to prompt the user ( For example, the free version of limited functionality), how to differentiate in the code? There are three ways to use the Cfbundleidentifier to determine the first way
    1. Nsstring*bundleidentifier =[[[nsbundlemainbundle]infodictionary]objectforkey:@ "CFBundleIdentifier"];//do any Additional setup after loading the view, typically from a nib.
      if ([bundleidentifierisequaltostring:@] yohunl. TestTarget2 "]) {
      Working with Code
      NSLog (@ "testtarget2-info.plist");
      }
      else {
      Working with Code
      NSLog (@ "testtarget-info.plist");
      }
      The second way, define a compiler macro to differentiate between opening TestTarget2,

Note that the-D above is required, and we generally capitalize on this defined macro in code that can

#ifdef TARGET2
Target2 's handling code
NSLog (@ "TARGET2");
#else
NSLog (@ "TARGET1");
#endif

The third way, this does not need to increase the-D to increase the precompiled macro in the code

#ifdef TESTTARGET2
Target2 's handling code
NSLog (@ "TESTTARGET2");
#else

NSLog (@ "TARGET1");

#endif above three kinds of ways can be Appendix II: Reference http://finbarrbrady.com/2010/03/building-for-multiple-targets-in-xcode/English/HTTP www.reigndesign.com/blog/ building-flockwork-creating-targets-for-free-and-full-versions-in-a-single-xcode-project/English/HTTP www.omegasoftweb.com/omega/blog.cfm?p=43 English http://blog.csdn.net/ysysbaobei/article/details/10951991 Chinese:/http blog.csdn.net/yohunl/article/details/17403029

"Turn" Xcode creates multiple taget of a project for easy testing and release of multiple versions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.