iOS Development uiapplication

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size: 15px;">The core role of UIApplication is to provide control and collaboration during the operation of the iOS Program.</span></p></p><p><p><span style="font-size: 15px;">The iphone application is initiated by the main function main, which is responsible for invoking the Uiapplicationmain function, as shown in the following form:</span><br><span style="font-size: 15px;">int Uiapplicationmain (</span><br><span style="font-size: 15px;">int argc,</span><br><span style="font-size: 15px;">Char *argv[],</span><br><span style="font-size: 15px;">NSString *principalclassname,</span><br><span style="font-size: 15px;">NSString *delegateclassname</span><br><span style="font-size: 15px;">);</span></p></p><p><p><span style="font-size: 15px;">argc, argv: Direct transfer to Uiapplicationmain for related processing can be</span></p></p><p><p><span style="font-size: 15px;">principalclassname: Specifies the application class name (symbol of the app), which must be uiapplication (or subclass). If nil, use the UIApplication class as the default value</span></p></p><p><p><span style="font-size: 15px;">delegateclassname: Specifies the Application's proxy class, which must comply with the Uiapplicationdelegate protocol</span></p></p><p><p><span style="font-size: 15px;">The Uiapplicationmain function creates a UIApplication object based on Principalclassname and creates a delegate object from Delegateclassname. and assigns the delegate object to the delegate property in the UIApplication object</span></p></p><p><p><br><span style="font-size: 15px;">So what exactly does the Uiapplicationmain function do? This function is mainly responsible for three things:</span></p></p><p><p><br><span style="font-size: 15px;">1) initializes the <span style="text-decoration: underline;">application</span> object from the given class name, which is an instance of initializing the UIApplication or subclass object, and if you are given nil here, the system defaults to the UIApplication class, It is primarily this class that controls and coordinates the operation of the Application. In subsequent work, you can use the static method sharedapplication to get the handle of the Application.</span></p></p><p><p><br><span style="font-size: 15px;">2) Initializes an application delegate from the given application delegate class. and set the delegate to the Application's delegate, here is if the passed parameter is nil, the function is called to access the Info.plist file to find the main nib file, get the application Delegate.</span></p></p><p><p><br><span style="font-size: 15px;">3) start the main event loop and start receiving Events.</span></p></p><p><p><span style="font-size: 15px;">The above is the work of the Uiapplicationmain function, the next question is the application view display, the control of the message? Here is the responsibility of the UIApplication (or Subclass) object, which mainly does the following:</span></p></p><p><p><br><span style="font-size: 15px;">1) is responsible for handling incoming user events and distributing event messages to the target object (sender, Action) that should process the Message.</span><br><span style="font-size: 15px;">2) manage and control views, including rendering, control behavior, current display view, and More.</span><br><span style="font-size: 15px;">3) the object has an application delegate object that the application notifies when important events (which can include system events or life cycle control events) occur during some life Cycles. For example, application startup, insufficient memory, or application completion, and so on, when these events occur, the application delegates to Respond.</span></p></p><p><p><span style="font-size: 15px;">From the above analysis, we can know that uiapplication is a black box for the developer, because all the operations can be done by its delegate, it only need to maintain some immutable things, such as event message distribution and delivery, send the delegate event processing request, etc. After the application is loaded, it sends a message to the delegate, and the delegate can implement the action the developer wants in the Applicationdidfinishlanching delegate Function. When you create an application with xcode, an application delegate class is implemented by Default. For loaded views, There is a view-related delegate class to handle the life events of the view loading process. Here are the main things that a delegate can do:</span><br><span style="font-size: 15px;">Controlling the behavior of an application</span></p></p><p><p> <br> <span style="font-size:15px;">-(void) applicationdidfinishlaunching: (uiapplication *) application  </span> <br> <span style="font-size:15px;">           application Started.   </span> <br> <span style="font-size:15px;">-(BOOL) application: (uiapplication *) Application Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions </span> <br> <span style="font-size:15px;">          when the application is opened due to other methods (such as URL designation or connection), the notification delegate starts   </span> <br> <span style=" font-size:15px; ">-(void) applicationwillterminate: (uiapplication *) application  </span> <br> <span style="font-size:15px;"           notification delegate, the application will exit at shutdown, please do some cleanup work.   < span> <br> <span style="font-size:15px; >-(void) applicationdidreceivememorywarning: (uiapplication *) application  </span> <br> <span style= " font-size:15px;">         Notifies the delegate that the application received a low memory warning from the System. </span></span></p></p><p><p><span style="font-size: 15px;">-(void) applicationsignificanttimechange: (uiapplication *) Application</span></p></p><p><p><span style="font-size: 15px;">Notifies the delegate system that the time has changed (mainly the time attribute, not the specific Time)</span></p></p><p><p><span style="font-size: 15px;">Open URL</span></p></p><p><p><span style="font-size: 15px;">-(BOOL) application: (uiapplication *) application handleopenurl: (nsurl *) URL</span><br><span style="font-size: 15px;">Opens the specified URL<em id="__mceDel"> </em></span></p></p><p><p><span style="font-size: 15px;">[[uiapplicationsharedapplication] openurl:[nsurlurlwithstring:@ "mailto://[email protected]"];</span></p></p><p><p><span style="font-size: 15px;">Call Telephone Phone</span></p></p><p><p><span style="font-size: 15px;">[[uiapplication sharedapplication] openurl:[nsurlurlwithstring:@ "tel://8008808888"];</span></p></p><p><p><span style="font-size: 15px;">Call SMS</span></p></p><p><p><span style="font-size: 15px;">[[uiapplicationsharedapplication] Openurl:[nsurl urlwithstring:@ "sms://800888"];</span></p></p><p><p><span style="font-size: 15px;">Call your own browser safari</span></p></p><p><p><span style="font-size: 15px;">[[uiapplicationsharedapplication] openurl:[nsurlurlwithstring:@ "http://www.hzlzh.com"];</span></p></p><p><p><span style="font-size: 15px;">Call Remote</span></p></p><p><p><span style="font-size: 15px;">[[uiapplicationsharedapplication] Openurl:[nsurl urlwithstring:@ "remote://fff"];</span></p></p><p><p><span style="font-size: 15px;">Call phone can pass number, call SMS can only set number, cannot initialize SMS Content.</span></p></p><p><br><span style="font-size: 15px;"><span style="font-size: 15px;">Control status bar orientation change</span></span><br><span style="font-size: 15px;"><span style="font-size: 15px;">–application:willchangestatusbarorientation:duration:</span></span><br><span style="font-size: 15px;"><span style="font-size: 15px;">device orientation will change</span></span><br><span style="font-size: 15px;"><span style="font-size: 15px;">–application:didchangestatusbarorientation:</span></span><br><span style="font-size: 15px;"><span style="font-size: 15px;">Active status Change</span></span><br><span style="font-size: 15px;"><span style="font-size: 15px;">-(void) applicationwillresignactive: (uiapplication *) application</span></span><br><span style="font-size: 15px;"><span style="font-size: 15px;">notifies the delegate that the application will go into an inactive state during which the application does not receive messages or Events. -(void) applicationdidbecomeactive: (uiapplication *) application</span></span><br><span style="font-size: 15px;"><span style="font-size: 15px;">notifies the delegate application to enter the active state, please restore the data</span></span><br><br><span style="font-size: 15px;"><span style="font-size: 15px;">you can see the uiapplication header file implementation</span></span><br><span style="font-size: 15px;"><span style="font-size: 15px;">@interface Uiapplication:uiresponder {</span></span><br><span style="font-size: 15px;"><span style="font-size: 15px;">@package</span></span><br><span style="font-size: 15px;"><span style="font-size: 15px;">ID _delegate; This is the application Delegate. </span></span><br><span style="font-size: 15px;"><span style="font-size: 15px;">Nstimer</span> .....</span><br><span style="font-size: 15px;"><span style="font-size: 15px;">} </span></span><br><span style="font-size: 15px;"><span style="font-size: 15px;">therefore, when dealing with system events in uiapplication, just go to the _delegate class to process, which is the application delegate Object. We can get the application delegate object from the Application's Singleton class object</span></span><br><span style="font-size: 15px;"><span style="font-size: 15px;">uiapplicationdelegate* mydelegate = [[uiapplication sharedapplication] delegate];</span></span><br><br><span style="font-size: 15px;"><span style="font-size: 15px;">UIApplication receives all system events and lifecycle events, it passes the event to Uiapplicationdelegate for processing, and for the user input event, it is passed to the corresponding target object to be Processed. For example, we can invoke the Applicationwillresignactive () method of the application delegate class when the application is called, and this method is also called when the user locks the screen, and is adapted to the delegate method when the application is reopened by the User. Another common use is an out-of-memory system warning that invokes the Applicationdidreceivememorywarning () method of the Application's delegate class, and then we can try to free up some memory. </span></span></p><p><p>iOS Development uiapplication</p></p></span>

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.