iPhoneネイティブアプリケーション開発概念1ーmainアプリケーション

  • 投稿日:
  • by
  • カテゴリ:

まとめ


  • メインルーチンはmail.mファイルのmain()関数に記述する
  • main()関数内のUIApplicationMain()関数の実行により、Info.plistファイルをロードし、NSMainNibFile キーに記述されるnibファイルをロードする。アプリケーションのメインオブジェクト(defaultはUIApplicationクラス)及びアプリケーションデリゲート( NSObject )を生成する
  • UIApplicationMain()関数もメインイベントのループも起動し、イベント処理を開始させる

UIApplicationクラスについて

The UIApplication class provides a centralized point of control and coordination for applications running on iPhone OS.

Every application must have exactly one instance of UIApplication (or a subclass of UIApplication ). When an application is launched, the UIApplicationMain function is called; among its other tasks, this function create a singleton UIApplication object. Thereafter you can access this object by invoking the sharedApplication class method.

A major role of a UIApplication object is to handle the initial routing of incoming user events. It also dispatches action messages forwarded to it by control objects (UIControl) to the appropriate target objects. In addition, the UIApplication object maintains a list of all the windows (UIWindow objects) currently open in the application, so through those it can retrieve any of the application’s UIView objects. The application object is typically assigned a delegate, an object that the application informs of significant runtime events—for example, application launch, low-memory warnings, and application termination—giving it an opportunity to respond appropriately.

Applications can cooperatively handle a resource such as an email or an image file through the openURL: method. For example, an application opening an email URL with this method may cause the mail client to launch and display the message.

UIApplication defines a delegate that must adopt the UIApplicationDelegate protocol implement one or more of the methods.

The programmatic interfaces of UIApplication and UIApplicationDelegate also allow you to manage behavior that is specific to the device. You can control application response to changes in interface orientation, temporarily suspend incoming user events, and turn proximity sensing (of the user’s face) off and on again.


サンプル

As is true for every C-based application, the initial entry point for every iPhone application is a function called main. The good news is that, when you create a new project using the iPhone templates in Xcode, you do not have to write this function yourself. The project templates include a version of this function with all the code needed to start your application.

Listing 1 shows the main function for the MoveMe application. The main function is located in that project's main.m file. Every application you create will have a main function that is almost identical to this one. This function performs two key tasks. First, it creates the application's top-level autorelease pool, whose job is to reclaim the memory for Objective-C objects that are freed using their autorelease method. Second, it calls theUIApplicationMain function to create the MoveMe application's key objects, initialize those objects, and start the event-processing loop. The application does not return from this function until it quits.

Listing 1  Using the provided main function

int main(int argc, char *argv[])

{

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int retVal = UIApplicationMain(argc, argv, nil, nil);

[pool release];

return retVal;

}

Help


UIApplicationMain

This function is called in the main entry point to create the application object and the application delegate and set up the event cycle.

int UIApplicationMain (
int argc,
char *argv[],
NSString *principalClassName,
NSString *delegateClassName
);

Parameters
argc

The count of arguments in argv; this usually is the corresponding parameter to main.

argv

A variable list of arguments; this usually is the corresponding parameter to main.

principalClassName

The name of the UIApplication class or subclass. If you specify nilUIApplication is assumed.

delegateClassName

The name of the class from which the application delegate is instantiated. If principalClassNamedesignates a subclass of UIApplication, you may designate the subclass as the delegate; the subclass instance receives the application-delegate messages.

Return Value

The value 0. This value is always returned when the function exits successfully. If there are internal problems, the application code calls the exit system function with an appropriate error code, thus killing the application immediately without returning from this function.

Discussion

This function instantiates the application object from the principal class and and instantiates the delegate (if any) from the given class and sets the delegate for the application. It also sets up the main event loop, including the application’s run loop, and begins processing events. This function returns only on termination.