1, a program has only one UIApplication
//Obtain UIApplication *app = [UIApplication sharedApplication]; // get error UIApplication *app = [[UIApplication alloc] init]; //UIApplication is a singleton, if you try to create a new UIApplication object in the program, it will prompt an error
2. Using the obtained UIApplication, you can perform some application-level operations (reminder box of the app icon, display the networking status, make a call, open a web page, control the status bar )
//The reminder box of the app icon (the little red dot with information on the icon) app.applocationIconBadgeNumber = 10; //10 is the reminder number //To implement the above method, user notification must be registered: //create user notification UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil]; //Notification for registered users [app registerUserNotificationSettings: settings];
//Set the network status app.networkActivityIndicatorVisible = YES;
//open the Web page NSURL *url = [NSURL URLWithString: @"http://www.baidu.com"]; [[UIApplication sharedApplication] openURL: url]; //Protocol header: http, https, file, tel
//control the status bar //After ios7, the status bar is determined by the controller by default, so: // (1) When the View controller-based status bar appearance of the info.plist file is set to NO, the control of the status bar in the controller will be invalid // hide the status bar [app setStatusBarHidden: YES]; //Set the font color mode of the status bar [app setStatusBarStyle:UIStatusBarStyleLightContent]; //Set the animation to hide the status bar [app setStatusBarHidden: YES withAnimation: UIStatusBarAnimationSlide]; // (2) When the View controller-based status bar appearance of the info.plist file is set to YES, the UIApplication modification control bar is invalid, and a method needs to be added in the controller (ViewController) //whether to hide the status bar - (BOOL)prefersStatusBarHidden { return YES; } //Set the status bar font color - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; }
3,UIApplicationDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //Called when the program is started } - (void)applicationWillResignActive:(UIApplication *)application { //Called when the program loses focus means that it cannot interact with the user } - (void)applicationDidEnterBackground:(UIApplication *)application { //Called when the program enters the background } - (void)applicationWillEnterForeground:(UIApplication *)application { //Called when the program is about to enter the foreground } - (void)applicationDidBecomeActive:(UIApplication *)application { //Called when the program gets the focus to interact with the user } - (void)applicationWillTerminate:(UIApplication *)application { //Calling this method when the program is closed cannot be verified } - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { //Called when the program receives a memory warning } /* When the program runs, the calling sequence is: 1 application:didFinishLaunchingWithOptions: 2 applicationDidBecomeActive: Enter the program first, and then get the focus. At this time, the user can perform interactive operations such as clicking */ /* Press the Home button: 1 applicationWillResignActive: 2 applicationDidEnterBackground: First lose focus, can't interact, and then the program enters the background */ /* Enter the program again: 1 applicationWillEnterForeground: 2 applicationDidBecomeActive: First call the method about to enter the foreground, and then enter the program to get the focus */