UIApplication and iosuiapplication for iOS development

Source: Internet
Author: User

UIApplication and iosuiapplication for iOS development

The core function of UIApplication is to provide control and collaborative work during iOS program running.

The iPhone application is started by the main function. It is responsible for calling the UIApplicationMain function. The function is as follows:
Int UIApplicationMain (
Int argc,
Char * argv [],
NSString * principalClassName,
NSString * delegateClassName
);

Argc and argv: Pass them directly to UIApplicationMain for relevant processing.

PrincipalClassName: Specifies the application Class Name (symbol of the app), which must be a UIApplication (or subclass ). If it is nil, The UIApplication class is used as the default value.

DelegateClassName: Specifies the proxy class of the application, which must comply with the UIApplicationDelegate protocol.

The UIApplicationMain function creates a UIApplication object based on principalClassName, creates a delegate object based on delegateClassName, and assigns the delegate object to the delegate attribute in the UIApplication object.


So what did the UIApplicationMain function do? This function is mainly responsible for three things:


1) initialize the application object from the given class name, that is, initializing an instance of the UIApplication or subclass object. If you specify nil here, the system will default to the UIApplication class, this class controls and coordinates the running of applications. In subsequent work, you can use the static sharedApplication method to obtain the application handle.


2) Initialize an application delegate from the given application delegate class. Set the delegate as the delegate of the application. If the input parameter is nil, the system calls the function to access the Info. plist file to find the main nib file and obtain the application delegate.


3) Start the main event loop and start to receive events.

The above is the work of the UIApplicationMain function. The next question is how to display and control messages in the application view? The following is the role of a UIApplication (or subclass) object. This object mainly performs the following tasks:


1) handles incoming user events, and distributes event messages to the target object (sender, action) that should process the message ).
2) manage and control views, including presentation, behavior control, and current display view.
3) this object has an application delegate object. When important events (including system events or lifecycle control events) occur in the lifecycle, the application notifies the object. For example, when an application starts, the memory is insufficient, or the application ends, the application delegates the response when these events occur.

Through the above analysis, we can know that UIApplication is a black box for developers, because all operations can be done by entrusting them to help us, it only needs to maintain some unmodifiable things, such as event message distribution and delivery, and sending event processing requests to the delegate. For example, after the application is loaded and processed, it sends a message to the delegate, and then the delegate can implement the action the developer wants in the applicationDidFinishLanching delegate letter number. When you create an application using XCode, an application delegate class is implemented by default. For a loaded view, there is a view-related delegate class to process the life events in the view loading process. The following describes the main tasks that can be entrusted:
Control Application Behavior


-(Void) applicationDidFinishLaunching :( UIApplication *) application
The application has been started.
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
When other methods open an application (such as specifying a URL or connecting to the application), the delegate is notified to be started.
-(Void) applicationWillTerminate :( UIApplication *) application
Notification delegate. The application will be closed and exited. Please do some cleanup.
-(Void) applicationDidReceiveMemoryWarning :( UIApplication *) application
Notification delegate, the application receives a warning of insufficient memory from the system.

-(Void) applicationSignificantTimeChange :( UIApplication *) application

The time of the notification delegate system has changed (mainly the time attribute, not the specific time)

Open URL

-(BOOL) application :( UIApplication *) application handleOpenURL :( NSURL *) url
Open the specified URL 

[[UIApplicationsharedApplication] openURL: [NSURLURLWithString: @ "mailto: // admin@hzlzh.com"];

// Call the phone number

[[UIApplication sharedApplication] openURL: [NSURLURLWithString: @ "tel: // 8008808888"];

// Call SMS

[[UIApplicationsharedApplication] openURL: [NSURL URLWithString: @ "sms: // 800888"];

// Call the self-contained browser safari

[[UIApplicationsharedApplication] openURL: [NSURLURLWithString: @ "http://www.hzlzh.com"];

// Call Remote

[[UIApplicationsharedApplication] openURL: [NSURL URLWithString: @ "remote: // fff"];

You can call phone to pass the number. You can call SMS to set only the number and Cannot initialize the SMS content.


Control status bar orientation changes
-Application: willChangeStatusBarOrientation: duration:
Device direction will change
-Application: didChangeStatusBarOrientation:
Activity status changes
-(Void) applicationWillResignActive :( UIApplication *) application
The notification delegate application enters the inactive state, during which the application does not receive messages or events. -(Void) applicationDidBecomeActive :( UIApplication *) application
Notifies the delegated application to enter the active state. Please restore Data

The UI application header file implementation is displayed.
@ Interface UIApplication: UIResponder {
@ Package
Id _ delegate; // This is the application delegate.
NSTimer .......
}
Therefore, when processing system events in UIApplication, you only need to go to the _ delegate class for processing. This class object is the application delegate object. We can obtain the objects entrusted by the application from the single-instance Class Object of the application.
UIApplicationDelegate * myDelegate = [[UIApplication sharedApplication] delegate];

When the UIApplication receives all system events and lifecycle events, it will pass the events to the UIApplicationDelegate for processing, and the user input events will be passed to the corresponding target object for processing. For example, after an application is called and other messages, you can call the applicationWillResignActive () method of the application delegate class. This method is also called when the user locks the screen, it is applicable to the delegate method when the application is re-opened by the user. In addition, the system warning is commonly used for insufficient memory. At this time, the applicationDidReceiveMemoryWarning () method of the application delegate class will be called, and then we can try to release some memory.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.