I believe many people have noticed the concept of Target in XCode. this is reflected in many places. For example, after opening a project, there is a Targets item in the list on the left, and in the top menu of the Project interface, there are also multiple projects that involve the Target, so what is the Target?
Apple said: "Targets that define the products to build. A target organizes the files and instructions needed to build a product into a sequence of build actions that can be taken."
In a simple understanding, we can think that a target corresponds to a new product (based on the same Code). But all of them have a piece of code. What should we do with a new product? Is this interesting?
In fact, this is not just a waste. Although the Code is the same, compilation settings (such as compilation conditions) and the resource files contained can be very different. therefore, even if the same code is generated, the product may be very different.
Let's take a few typical applications with multiple Targets, such as the full version and lite version; for example, the 20-off, 30-off, and 50-off versions of the same game; or, for example, changing resources and names for the same game can be sold as new games (hello, what are you teaching ...)
What are the same and what are the differences between Targets!
Since the same code is used to generate different products, what are the differences between different targets?
To solve this problem, let's take a look at the content specified by a Target.
From the list on the left side of XCode, we can see that a Target contains Copy Bundle Resources, Compile Sources, Link Binary With Libraries.
- Copy bundle resources refers to the resource files included in the. app of the generated product.
- Compile sources indicates which source code is compiled
- Link binary with libraries indicates which library files will be referenced during compilation
By using different settings in copy bundle resources, we can make different products contain different resources, including the main icons of the program, etc, instead of including the resources listed in the xcode project.
This is not all the content specified by a target. Each target can use an independent and different info. plist file.
We all know that this info. plist file defines a lot of key content for an iPhone project,
For example, the program name and the globally unique ID of the product are generated.
In addition, different targets can define complete and differentiated compilation settings, from simple adjustment of Optimization Options to adding the compilation conditions used for Conditional compilation, so that the base SDK can be differentiated.
Create the second target!
Why is it the second one? Because the first one is the default target after the project is created! (So much nonsense, drag it away ...)
There are multiple methods to create a target. We can copy a copy from the existing target, and then add a slight change to it. We can also create a new target. However, the two methods are similar.
First, let's take a look at how to create a target using the replication method.
Create a Target Using Replication
In the list on the left side of xcode, expand the targets item. On the existing target, right-click and select "DUPLICATE", or select an existing target, in the top menu, select "DUPLICATE.
Now we get a new target, and a XXXX copy will be obtained in the resource. plist. this new target is exactly the same as the original target, and the rest is some differentiated modifications. We will discuss this later.
Create a new target
Similar to the copy method, we can right-click the menu in many places in the list on the left, and we can see that "new target... "item, and in the project on the top menu of the project, you can also see this" new target....
After clicking this button, we will first let you choose the target type. Since I have always been referring to the program itself, we will naturally choose application. (for other items, please study them as you are interested, for example, we can extract parts of the program into a static library ).
Next, you will be asked to enter a new target name, instead of generating a target name such as XXXXX copy by default in the copy method.
However, the generated target is almost empty. Copy bundle resources, compile sources, and link binary with libraries does not contain any content. The compilation settings are completely original.
You can drag the content to the target settings and adjust the compilation options to complete the target configuration.
How to modify some content in target!
In fact, this part of content may also be used in non-multi-targets projects.
Since the modification is basically completed in the project/compilation settings, no declaration is made in special cases. You can right-click the target to open the corresponding project/compilation settings, select get info.
Modify the generated product name: the product name in the packing segment
Info. plist file name: info. plist file in the packing segment. For example, if the copied target finds that XXXXX copy. plist is too silly, you can change it here.
Conditional compilation: Add a user-defined setting (you can see this content in the lower left corner of the build page of target "XXXX" info), and enter it in other C flag, for example, to define a define value called lite_version, we can write "-dlite_version" or "-dlite_version = 1 ". then it can be used in the program
If defined (lite_version)
Else
Endif
Such Conditional compilation is used to partially differentiate the code.
Some may remember the cracked version of the code I posted in the code area. One of the methods is to check info. plist is text or binary, so can we recommend a simulated cracking target to directly generate text info. plist for testing?
Of course you can. In the packing section, there is an item called "info. plist output encoding ". The default value is binary. We only need to select XML to generate the product. info. plist is a text style.
In addition, to add/delete files in copy bundle resources, compile sources, and link binary with libraries, you can select get info on the file to be modified and switch to the target page, you can specify the target of this file as the parameter. For example, icon.png can be specified to the default target, while icon_lite.png can be specified to the target of lite verion.