iOS development-uiapplication and app launch status

Source: Internet
Author: User

uiapplication Simple to understand literally is the application, development time will sometimes call the method in the way, it looks inconspicuous, the actual iOS development UIApplication provides control and collaboration during the operation of the iOS program. Each application must have only one instance of UIApplication (or its subclasses) at run time, which is often said in the design pattern, and the Singleton object is obtained by Sharedapplication. No additional init is required for a uiapplication.

uiapllication Concept

The regular program may compile the time is correct, when running the error, usually is stuck in the main.m, this is the program entry point, also created from here the UIApplication Singleton example.

int main (int argc, char * argv[]) {    @autoreleasepool {        return Uiapplicationmain (argc, argv, Nil, Nsstringfromclas S ([Appdelegate class]));}    }

Uiiapplication is typically used primarily for handling user events, creating a new queue when processing, putting all user events into a queue, processing them, and sending the current event to a suitable target control for handling the event at the time of processing. The UIApplication instance also maintains a window list (UIWindow instance) opened in the app so that uiapplication can get any UIView object in the app. The UIApplication instance is given a proxy object to handle the application's lifecycle events (such as program startup and shutdown), system events (such as phone calls, text messages), and so on. Specifically, to see the specific application,

Create a New button in the storyboard, then draw a click event and add the following two lines of code:

   UIApplication *application=[uiapplication sharedapplication];    Set the update number on the icon    application.applicationiconbadgenumber=25;

Before the iOS8.0 will see the app has a red figure, the first few months avatar, many people have been pits, forcing themselves for a few days not to see, see Little Red dot want to point, the console will prompt the following information: Attempting to badge the application icon But Haven ' t received permission from the user to badge the application

The specific solution is very simple, is to obtain the following machine version number, and then notify the user according to the need to choose settings (personally feel more humane, mobile phone if full screen of the Little red dots, think about it.) );

    Float Versionnumber=[[[uidevice Currentdevice] systemversion] floatvalue];    if (versionnumber >= 8.0) {        NSLog (@ "%@", [nsstring stringwithformat:@ "current system is%f", versionnumber]);        Notification settings        uiusernotificationsettings *settings = [uiusernotificationsettings settingsfortypes: Uiusernotificationtypebadge Categories:nil];        Receive user's Choice        [[UIApplication sharedapplication] registerusernotificationsettings:settings];    }

UIApplication can do some other things, such as the OpenURL of which is more practical:

    Phone    [Application Openurl:[nsurl urlwithstring:@ "tel://10010"];    SMS    [Application openurl:[nsurl urlwithstring:@ "sms://10010"];    Mail    [Application openurl:[nsurl urlwithstring:@ "Mailto://[email protected]"];    URL    [Application Openurl:[nsurl urlwithstring:@ "http://www.cnblogs.com/xiaofeixiang/"];
uiapplicationdelegate

The methods defined in the Uiapllicationdelegate protocol are partly related to the state of the application, The new project defaults to the AppDelegate.h and APPDELEGATE.M files, and you can refer to the following default generated APPDELEGATE.M code first:

appdelegate.m//uiapplicationdemo////Created by Keso on 15/2/5.//Copyright (c) 2015 Keso. All rights reserved.//#import "AppDelegate.h" @interface appdelegate () @end @implementation appdelegate-(BOOL) Application: (uiapplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {//Override    Point for customization after application launch.    NSLog (@ "application-program completes loading"); return YES;} -(void) Applicationwillresignactive: (uiapplication *) application {//Sent when the application was about to move from a Ctive to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US    Er quits the application and it begins the transition to the background state. Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates.    Games should use this method to pause the game. NSLog (@ "applicationwillresignactive-logoff activation"); -(void) ApplicationdidenterBackground: (uiapplication *) application {//Use this method to release shared resources, save user data, Invalidate Ti MERS, and store enough application state information-to-restore your application to their current-state-it is termin    Ated later. If your application supports background execution, this method is called instead of Applicationwillterminate:when the    User quits. NSLog (@ "applicationdidenterbackground-into the Background");} -(void) Applicationwillenterforeground: (uiapplication *) application {//called as part of the transition from the back Ground to the inactive state;      Here's can undo many of the changes made on entering the background. NSLog (@ "applicationwillenterforeground-Enter the front desk"); -(void) Applicationdidbecomeactive: (uiapplication *) application {//Restart any tasks this were paused (or not yet STA rted) While the application was inactive.    If the application is previously in the background, optionally refresh the user interface. NSLog (@ "applicationdidbeComeactive-becomes active ");} -(void) Applicationwillterminate: (uiapplication *) application {//called when the application was about to terminate. S Ave data if appropriate.      See also Applicationdidenterbackground:. NSLog (@ "applicationwillterminate-program termination Status");} @end

At the beginning of the main function is actually the appdelegate instantiation to the uiapplication agent to execute the method, if you like this appdelegate file, complete has to be necessary to create a new class file, Just declare uiapplicationdelegate. For the above status we can look at a picture of Apple's official website first:

Apple has an explanation by default, but presumably writes a personal understanding:

Application:didfinishlaunchingwithoptions:-The final initialization work before the application is presented to the user

Applicationwillresignactive:-from the active state into hibernation, sudden phone, SMS can make the current program into the background, non-interactive

applicationdidenterbackground-application into the background, you can save and transfer data

Applicationwillenterforeground-can recover data here, will enter the foreground, is not active status

Applicationdidbecomeactive-enter the foreground to get the focus, you can interact

applicationwillterminate-application is destroyed when called, if suspend does not call

Write here to see the app's startup process in detail:

1.main function

2.UIApplicationMain creating UIApplication objects, creating uiapplication delegate objects (listening for application state)

3. According to info.plist get the file name of the most important storyboard, load the most important storyboard, create UIWindow (if there is no storyboard, call the agent's application when the program starts: Didfinishlaunchingwithoptions: Method, create UIWindow in application:didfinishlaunchingwithoptions:), Create and set UIWindow Rootviewcontroller, display window;

iOS development-uiapplication and app launch status

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.