Edit the Empty Window target and click Build Phases at the top of the editor
(Figure 6-8). These are the stages by which your app is built. By default, there are three
of them with content — Compile Sources, Link Binary With Libraries, and Copy Bundle
Resources — and those are the only stages you’ll usually need, though you can add
others. The build phases are both a report to you on how the target will be built and a
set of instructions to Xcode on how to build the target; if you change the build phases,
you change the build process. Click each build phase to see a list of the files in your
target to which that build phase will apply.
The meanings of the three build phases are pretty straightforward:
Compile Sources
Certain files (your code) are compiled, and the resulting compiled code is copied into the app.
This build phase typically applies to all of the target’s .m files; those are the code files that constitute the target. Sure enough, it currently contains View‐ Controller.m, AppDelegate.m, and main.m. If you add a new class to your project, you’ll specify that it should be part of the app target, and the .m file will automatically be added to the Compile Sources build phase.
Link Binary With Libraries
Certain libraries, usually frameworks, are linked to the compiled code (now referred to, following compilation, as the binary), so that it will expect them to be present on the device when the app runs.
This build phase currently lists three frameworks. I’ll talk later in this chapter about the mechanics of linking the binary with additional frameworks.
You can alter these lists manually, and sometimes you may need to do so. For instance,
if something in your project, such as a sound file, was not in Copy Bundle Resources
and you wanted it copied into the app during the build process, you would drag it from the Project navigator into the Copy Bundle Resources list, or (easier) click the Plus
button beneath the Copy Bundle Resources list to get a helpful dialog listing everything
in your project. Conversely, if something in your project was in Copy Bundle Resources
and you didn’t want it copied into the app, you would delete it from the list; this would
not delete it from your project, from the Project navigator, or from the Finder, but only
from the list of things to be copied into your app.
The meanings of the three build phases are pretty straightforward:
Compile Sources
Certain files (your code) are compiled, and the resulting compiled code is copied into the app.
This build phase typically applies to all of the target’s .m files; those are the code files that constitute the target. Sure enough, it currently contains View‐ Controller.m, AppDelegate.m, and main.m. If you add a new class to your project, you’ll specify that it should be part of the app target, and the .m file will automatically be added to the Compile Sources build phase.
Link Binary With Libraries
Certain libraries, usually frameworks, are linked to the compiled code (now referred to, following compilation, as the binary), so that it will expect them to be present on the device when the app runs.
This build phase currently lists three frameworks. I’ll talk later in this chapter about the mechanics of linking the binary with additional frameworks.
Copy Bundle Resources
Certain files are copied into the app, so that your code or the system can find them there when the app runs. For example, if your app had an icon image, it would need to be copied into the app so the device could find and display it.
This build phase currently applies to the asset catalog; any images you add to the asset catalog will be copied into your app as part of the catalog. It also currently lists InfoPlist.strings and your app’s .storyboard file.
Copying doesn’t necessarily mean making an identical copy. Certain types of file are automatically treated in special ways as they are copied into the app bundle. For example, copying the asset catalog means that icons and launch images in the cat‐ alog are written out to the top level of the app bundle; copying the .storyboard file means that it is transformed into a .storyboardc file, which is itself a bundle con‐ taining nib files.
Certain files are copied into the app, so that your code or the system can find them there when the app runs. For example, if your app had an icon image, it would need to be copied into the app so the device could find and display it.
This build phase currently applies to the asset catalog; any images you add to the asset catalog will be copied into your app as part of the catalog. It also currently lists InfoPlist.strings and your app’s .storyboard file.
Copying doesn’t necessarily mean making an identical copy. Certain types of file are automatically treated in special ways as they are copied into the app bundle. For example, copying the asset catalog means that icons and launch images in the cat‐ alog are written out to the top level of the app bundle; copying the .storyboard file means that it is transformed into a .storyboardc file, which is itself a bundle con‐ taining nib files.
A useful trick is to add a Run Script build phase, which runs a custom shell script late
in the build process. To do so, choose Editor → Add Build Phase → Add Run Script
Build Phase. Open the newly added Run Script build phase to edit the custom shell
script. A minimal shell script might read:
echo "Running the Run Script build phase"
The “Show environment variables in build log” checkbox causes the build process’s
environment variables and their values to be listed in the build log during the Run Script
build phase. This alone can be a good reason for adding a Run Script build phase; you
can learn a lot about how the build process works by examining the environment vari‐
ables.