Uiapplication sharedapplication-Ios

Source: Internet
Author: User

IPhone applicationsProgramIs started by the main function, which is responsible for calling the uiapplicationmain functionFunctionThe format is as follows:
Int uiapplicationmain (
Int argc,
Char * argv [],
Nsstring * principalclassname,
Nsstring * delegateclassname
);
So what did the uiapplicationmain function do? This function is mainly responsible for three things:

1) initialize from the given class nameApplicationThe program object, that is, an instance that initializes the uiapplication or subclass object. If nil is specified here
SystemBy default, the uiapplication class is used to control and coordinate 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 a subclass) object. This object mainly performs the following tasks:

1) handles incomingUserEvent, and distribute the event message 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, and it can also be. Because all the operations can be done by entrusting them to help us, it only needs to maintain some unmodifiable things in the future, for example, an event message is distributed and transmitted, and an event processing request is sent to the delegate. For example, after an application is loaded and processed, a message is sent to the delegate, then, the delegate can implement the actions the developer wants in the applicationdidfinishlanching delegate 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 changes (mainly the time attribute, not the specific time value)
Open URL
-(Bool) Application :( uiapplication *) Application handleopenurl :( nsurl *) URL
Open the specified URL
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

1. Set the number icon on the icon

// Set the number icon on the icon of the main interface, which is introduced in 2.0. The default value is 0.
[Uiapplicationsharedapplication]. applicationiconbadgenumber = 4;

2. Set whether redo and undo operations are supported during shaking gestures.

// Shake the gesture to determine whether redo undo operations are supported.
// Introduced after 3.0, default Yes
[Uiapplicationsharedapplication]. applicationsupportsshaketoedit = yes;

3. Determine the program running status

// Determine the program running status, which will be introduced after 2.0

If ([uiapplicationsharedapplication]. applicationstate = uiapplicationstateinactive ){
Nslog (@ "the program is running ");
}

4. Prevent the screen from being dimmed to sleep

// Prevent the screen from being dimmed. Use it with caution. The default value is no 2.0.
[Uiapplicationsharedapplication]. idletimerdisabled = yes;

Use this function with caution because it consumes a lot of power.

5. display the networking status

// Display the network tag 2.0
[Uiapplicationsharedapplication]. networkactivityindicatorvisible = yes;

6. display an address on Map

Nsstring * addresstext = @ "1 infinite loop, Cupertino, CA 95014 ";
// URL encode the Spaces
Addresstext = [addresstextstringbyaddingpercentescapesusingencoding: nsasciistringencoding];
Nsstring * urltext = [nsstringstringwithformat: @ "http://maps.google.com/maps? Q = % @ ", addresstext];

[[Uiapplicationsharedapplication] Openurl: [nsurlurlwithstring: urltext];

7. send an email

Nsstring * recipients
= @ "Mailto: first@example.com? Cc = second@example.com & subject = hello from California! ";
Nsstring * Body = @ "& Body = It is raining in sunny California! ";

Nsstring * Email = [nsstringstringwithformat: @ "% @", recipients, body];
Email = [emailstringbyaddingpercentescapesusingencoding: nsutf8stringencoding];

[[Uiapplicationsharedapplication] Openurl: [nsurlurlwithstring: email];

8. Call a number.

// Call the Google 411
[[Uiapplicationsharedapplication] Openurl: [nsurlurlwithstring: @ "Tel: // 8004664411"];

9. send SMS
// Text to Google SMS
[[Uiapplicationsharedapplication] Openurl: [nsurlurlwithstring: @ "SMS: // 466453"];

10. Open a website

// Lanuch any iPhone developers fav site
[[Uiapplicationsharedapplication] Openurl: [nsurlurlwithstring: @ "http://itunesconnect.apple.com"];

The uiapplication header is displayed.FileImplementation
@ Interface uiapplication: uiresponder <uiactionsheetdelegate> {
@ Package
ID <uiapplicationdelegate> _ 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, system warnings with insufficient memory are commonly used. In this case, the applicationdidreceivememorywarning () method of the application delegate class is called,
Then we can try to release some memory.

The preceding figure shows how the application processes the uiapplication sharedapplication in the application life cycle (including startup, stop, recovery, and exit ).

Contributed by zhudameng213
The PPT document may have poor browsing experience on the WAP side. We recommend that you first select txt or download the source file to your local machine for viewing.
20120221
IOS application Lifecycle
Uiviewcontroller Lifecycle
Program Lifecycle
The life cycle of a program refers to the whole process from application startup to application end. Every IOS application contains a uiapplication object, in iOS, this uiapplication object is used to monitor the entire life cycle of an application. Every IOS application must specify a proxy object for its uiapplication object, the proxy object processes the application lifecycle events monitored by the uiapplication object.
5 iOS app statuses
Not running: the application has not been started, or the application is running but is tied
Stop
Inactive: the current application is running on the foreground, but does not receive events (currently
Executing other code ). Generally, when an application switches from one status to another, the transition will temporarily stay in this status. The only thing that remains in this status for a long time is when the user locks the screen or the system prompts the user to respond to some (such as phone calls or unread text messages) events.
Active: the current application is running on the foreground and receives events. This is before the application is running
The normal status of the running station.
Background: The application is in the background and the code is still being executed. Most
Applications in the sushortded State enter this state for a short time. However, applications that require additional execution time will remain in this state for a longer period of time. In addition, if an application needs to directly run in the background when it is started, such an application will directly enter the background state from the not running state without going through the inactive state in the middle. For example, applications without interfaces. Note: This is not a specific interface-free application. It can also be an interface application. If you want to directly enter the background status, the application interface will not be displayed.
Suincluded: the application is in the background and the code execution has stopped. Automatically
Move the application to this status, and no notification will be sent to the application before this operation. When the application is in this status, the application still resident in the memory but does not execute any program code. When the system generates a low-memory alarm, the system will clear the applications in the susponded State to provide sufficient memory for the applications running on the foreground.
Create a uiapplication object and specify its proxy
Use the uiapplicationmain function to create a uiapplication object and specify its proxy object appdelegate. The third parameter is the subclass of the specified uiapplication to generate a uiapplication object. When it is nil, the default object is initialized by the uiapplication class; the fourth parameter is the specified proxy object.
Proxy object of uiapplication
As the proxy class of uiapplication, you must first implement the uiapplicationdelegate Protocol, which defines what to do or can do as a proxy. The uiapplication object is responsible for listening to application lifecycle events and handing over the lifecycle events to the uiapplication proxy object for processing.
Detailed description of the uiapplication proxy object lifecycle Function
-(Void) applicationwillresignactive :( uiapplication *) Application
Note: When an application is about to be executed in an inactive state, during this period, the application

Do not receive messages or events in sequence, such as calls
-(Void) applicationdidbecomeactive :( uiapplication *) Application
Note: When the application enters the activity status for execution, this is exactly the opposite of the above method.
-(Void) applicationdidenterbackground :( uiapplication *) Application
Note: This API is called when a program is pushed to the background. So to set the background to continue running, you can set it in this function.
Detailed description of the uiapplication proxy object lifecycle Function
-(Void) applicationwillenterforeground :( uiapplication *) Application
Note: This method is called when the program is to return to the foreground from the background.
-(Void) applicationwillterminate :( uiapplication *) Application
Note: When a program is about to exit, it is called to save data and clean up before exiting. You need to set the key value of uiapplicationexitsonsuspend.
(Void) applicationdidreceivememorywarning :( uiapplic ation *) Application
Note: iOS devices only have limited memory. If too much memory is allocated to the application, the operating system will terminate the operation of the application. This method will be executed before the termination, you can usually clean up the memory here to prevent the program from being terminated.
Detailed description of the uiapplication proxy object lifecycle Function
(Void) applicationdidfinishlaunching :( uiapplication *) a pplication
Note: The program is executed after being loaded.
-(Bool) Application :( uiapplication *) Application handleopenurl :( nsurl *) URL
Note: It is executed when the URL is opened.
Uiviewcontroller
Uiviewcontroller is the carrier and Controller of the IOS top-level view. The interaction between users and the program interface is controlled by uiviewcontroller. Uiviewcontroller manages the lifecycle of uiview and resource loading and release.
Uiview and uiwindow demonstrate the application user interface.
Uiviewcontroller lifecycle events
-(Void) loadview
Load view resources and initialize views
-(Void) viewdidload-(void) viewdidunload
Release view resources
-(Void) viewwillappear :( bool) animated
View to be loaded
-(Void) viewdidappear :( bool) animated
View appears
-(Void) viewwilldisappear :( bool) animated
View is about to disappear
(Void) viewdiddisappear :( bool) animated
View disappears
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.