Creation of the first iPhone application
The first iPhone application discussed in this article has been created in the following article:
Creation of First iPhone application explained
This project is a simple navigation-based application. It uses a table view on the main window. From the code side, a table view controller (UITableViewController) is used to provide the functionality for a table based view.
In the article below, the directory structure and the files for that project are discussed.
The Directory Structure
The grouping of the file you see in the Groups & Files window of Xcode may not match the actual directory structure on the hard disk.
Figure 1. The Grouping of various iPhone project files in the Groups & Files window of Xcode.
The groups in the Project window of Xcode are Classes, Other Sources, Resources, Frameworks, and Products. However, as you can see in the folder snapshot below, these are all not sub-folders underneath the project folder for FirstApp.
Figure 2. The directory structure of first iPhone application. This is a simple Navigation-Based application.
The figure above shows the directory structure of a simple Navigation-Based application. This directory can be divided into three parts:
- Root Directory
- Classes Directory
- Build Directory
There is no programmatic need to have a Classes folder. Depending on the number of files you have as a part of the project, you can put these files into various sub-folders that make sense to you. Some additional folders that you might expect in a decent sized project are:
- Other Custom Folders
You can name these folders whatever you want in an iPhone project, depending on what makes sense for that particular project.
The File Types
Files with various extensions are created in typical iPhone / Mac OS X applications (Cocoa and Cocoa Touch applications). Following are the files that a developer would normally come in contact with:
Source and User Interface Files:
- .h -- Header Files
- .m -- Objective-C Source Files
- .xib -- Interface Builder files
- .xcodeproj -- Xcode Project File
- info.plist -- Property List
- .pch -- Pre-Compiled Header
Framework and Library Files:
- .framework -- The Framework Bundle
- .dylib -- Dynamic Library
- .app -- The Application Bundle
In addition to the above types of files, you could find image files (.png, etc.), sound files (.caf, etc.), other resource files (.pdf, .xml, etc.) in an iPhone project. The build directory would contain files with a whole set of other extensions. These include the intermediate files that would eventually help compile the executable.
Following sections describe the important file types used in an iPhone application project.
Files that are used more often by the Developer
The following three kinds of files (by file extension) are used more often by the developer of iPhone (or Mac OS X) applications:
- Header Files (.h)
- Implementation files (.m)
- User interface files (.xib)
The functionality that an iPhone application provides is achieved through a series of classes and functions written in Objective-C programming language. The declaration of a class is written in header files, which have the extension .h. The actual implementation of these methods is in .m files.
For example, a class named RootViewController is declared in a header file named RootViewController.h. The methods declared in this file (and any overrides for the methods belonging to the base class) are implemented (fleshed out) in another file named RootViewController.m.
An iPhone project will have a main.m file, which includes the main() method, where the execution of the program begins. The main method calls the UIApplicationMain() method, which calls the necessary iPhone classes to start the UI of the given application.
If your application is very small, you can perhaps place the entire code into the main.m file. Meaning, you could add the declaration and definition of that one single (small) class your iPhone application has into this file. But typically you want to leave the main.m file with that small generated main() method and add your classes in other files.
The User Interface Files
The user interface files have .xib extension in the Xcode. If you look in the .app created for the project, the same files will have .nib extension. In this first app, there are two xib files: MainWindow.xib and RootViewController.xib.
So, for the example class we discussed above, the UI file is RootViewController.xib. This file can be edited with Interface Builder. In this case, there is a table view that displays information in tabular/list format.
This section describes the project file, information property list file, and precompiled header file / Precompiled Prefix header file.
- .xcodeproj -- Xcode Project File
- info.plist -- Property List
- .pch -- Pre-Compiled Header
The Project File
The project file of an iPhone application (or, Xcode project, rather) has the extension .xcodeproj. Here the name of the project file for iPhone FirstApp application is FirstApp.xcodeproj.
The entities with the .xcodeproj extension are presented as single units in the Finder; however, these are File Packages. Meaning, FirstApp.xcodeproj is actually a collection of files. If you look in the Terminal with an ls -l UNIX command, you can actually see that FirstApp.xcodeproj is a directory.
Code Listing 1. The .xcodeproj is actually a directory (see the d for directory at the beginning of the entry)
drwxr-xr-x 6 infinitezest staff 204 Oct 4 10:42 Classes
drwxr-xr-x 5 infinitezest staff 170 Oct 7 17:25 FirstApp.xcodeproj
-rw-r--r-- 1 infinitezest staff 185 Oct 4 10:42 FirstApp_Prefix.pch
-rw-r--r-- 1 infinitezest staff 897 Oct 4 10:42 Info.plist
-rw-r--r-- 1 infinitezest staff 10780 Oct 4 10:42 MainWindow.xib
-rw-r--r-- 1 infinitezest staff 7542 Oct 4 10:42 RootViewController.xib
drwxr-xr-x@ 5 infinitezest staff 170 Oct 4 12:00 build
-rw-r--r--@ 1 infinitezest staff 363 Oct 4 12:46 main.m
The file package FirstApp.xcodeproj actually contains three files, with extensions .mode1v3, .pbxuser, .pbxproj. These bundles of files are used all over in Mac OS X.
Code Listing 2. The contents of the .xcodproj file package
-rw-r--r-- 1 infinitezest staff 41717 Oct 7 17:25 infinitezest.mode1v3
-rw-r--r-- 1 infinitezest staff 11671 Oct 7 17:25 infinitezest.pbxuser
-rwxr-xr-x 1 infinitezest staff 10726 Oct 7 17:25 project.pbxproj
You can look at the contents of a File Package from within the Finder as well.
Figure 3. Viewing the package contents from Finder. Right-click on the File Package and if it is a file package, you will see Show Package Contents menu item.
The Information Property List File
This property file is named Info.plist. This is an XML file that gets bundled with the application. You can see this file in the Xcode project window. This same file gets copied in to the .app application bundle. For example, the FirstApp.app file package contains the FirstApp executable, as well as Info.plist.
Code Listing 3. The Info.plist file for FirstApp.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
The Precompiled Header File (Precompiled Prefix Header File)
The projects generated from the templates have a file with an extension _Prefix.pch; for example, the FirstApp project will have a file named FirstApp_Prefix.pch.
Code Listing 4. Contents of the precompiled prefix header file.
// Prefix header for all source files of the FirstApp target in the FirstApp project
As you can see, this precompiled prefix header file contains Foundation.h and UIKit.h header files. This reduces the compile time. By pch (precompiled header), it means that these header files are compiled before; by prefix, it means that these header files are included to each of the source files at compilation.
Framework and Library Files
For most of the iPhone applications, you can expect to find at least Foundation.framework and UIKit.framework included in the project. Framework files are not copied into the project directories.
For example UIKit.framework is located in the System folder:
Figure 4. The information about UIKit.framework.
From the OS perspective, these .framework files not just a single file, but a directory. With each of these frameworks, you can find a Headers directory and an executable file. The Headers directory contains a set of header files (with .h extension) that declare the classes that are part of the framework.
Figure 5. The contents of UIKit.framework
This FirstApp application does not have any .dylib files. But, for example, the functionality of SQLite comes from a dynamic library called libsqlite3.0.dylib.
The purpose of a development tool / compiler is to generate some kind of executable from the source code. For this application, FirstApp.app is generated. Just like .xcodeproj, this is file package, not just a single file.
Figure 6. Contents of an .app file package. You can see that the .nib files (binary versions of .xib files) are also included.
As you can see from the figure above, the FirstApp.app package contains a Unix Executable file called FirstApp. There are two user interface files (MainWindow.nib and RootViewController.nib), which are derived from the .xib files by the same name.
There is the information property list (info.plist). And finally there is a text file called PkgInfo, which contains APPL????. This information is in info.plist as well. These are in two properties: Bundle OS Type Code (APPL) and Bundle Creator OS Type Code (????).
Following sections briefly describe the contents of the directories in an iPhone application.
This is the root directory of the project. In this example, this is the FirstApp directory (since the iPhone application here is named FirstApp). This directory contains the following files:
- The Project File
- The Properties List File
- The Pre-Compiled Header File
- The main Source File
- The User Interface Files
The Classes directory
This directory contains the header and implementation files for various classes that make up the application. Source files that belong to a project can be stored in different directories -- according to the need. In this case, Classes is one of those folders.
The Build Directory
The build folder contains the files and bundles generated by the Xcode compiler based on the build settings.
This folder contains folders for debug and release versions of the applications. These folders would contain app files in the debug and release mode. Perhaps, there might be symbol files. There would also be a series of intermediate files that aid in compiling the final executables.