iOS app structure and startup

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

.

This page was most recently updated in September 2012.

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.

You can follow along by creating a new iOS app, with the “Single View Application” template.

.

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 storyboard user interface file is loaded. (The name of the storyboard file is seen in <appname>-Info.plist.)

The storyboard 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 (AppDelegate.h and .m)
  • initial view (in the storyboard 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:

  1. The app is launched by the user
  2. The main() function is the entry point, and runs
  3. The UIApplicationMain() function is called
  4. The application (singleton) object is created (i.e. the runtime environment for the app)
  5. <appname>-Info.plist is examined, and the storyboard file named in “Main storyboard file base name” is loaded, and the initial view is loaded (which means that all of its objects are initialized, and connections are established)
  6. The runtime sends the applicationWillFinishLaunching: message to the application delegate
  7. The runtime sends the application:didFinishLaunchingWithOptions: message to the application delegate

.

A subsequent view is initialized by loading it from the storyboard. Its view controller is then initialized, because the view controller is configured as its 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.

.


.

.

.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: