iOS app structure and startup

Let’s examine the structure of a typical iOS app, and how it starts up.

.

Now that you have been introduced to Objective-C, Cocoa, and the development tools, let’s examine the structure of a typical iOS app. We briefly covered this topic at times during our introductory sessions, so let’s summarize the coverage now.

.

iOS app structure

All apps have a main.m, which contains the app’s entry point, a function called main().

The main() function initializes an “application” object (you can think of this as the runtime environment for the app).

Then, in a typical configuration, the app’s main nib file is loaded. (The name of the nib file is seen in <appname>-Info.plist.)

The nib file loads, and by configuration, causes the application delegate to load. The application delegate implements methods that handle application-level events. Then, typically, the app’s initial view and view controller are initialized, and control is passed to the view controller.

After that, user interaction and other events in the environment will determine what happens next.

A typical app, therefore, has the following components (that make up its structure):

main() function, in main.m

configuration info located in <appname>-Info.plist

application delegate (<appname>AppDelegate.h and .m)

initial view (nib file) and view controller (.h and .m)

other views and view controllers

resources like images and so on

app icon, as a 57px square PNG-format image

.

iOS app startup

What is the typical startup sequence? What happens, and in what order? The following details the steps:

The app is launched by the user

The main() function is the entry point, and runs

The UIApplicationMain() function is called

The application (singleton) object is created (i.e. the runtime environment for the app)

<appname>-Info.plist is examined, and the nib file named in “Main nib file base name” is loaded (which means that all of its objects are initialized, and connections are established)

As part of the nib loading, the awakeFromNib: method is called on all the objects that it initialized

The runtime sends the applicationWillFinishLaunching: message to the application delegate

The nib’s File’s Owner object has an instance variable named “delegate”; the object that it points to is the application delegate, which is then initialized

The runtime sends the application:didFinishLaunchingWithOptions: message to the application delegate

A subsequent view is initialized by loading a nib. Its view controller is then initialized, because the view controller is configured as the nib’s File’s Owner.

You can see the results in the “Events 1” example app. When you run it, the app will use NSLog() function calls to write information to the debugger console as specific methods are called by the Cocoa runtime.