iOS uiapplication detailed

Source: Internet
Author: User

UIApplication Object Features: Feature 1:
  • The UIApplication object is a symbol of the application, and a UIApplication object represents an application, and is a singleton. (an object used to encapsulate an entire application, such as what to do when the application executes to a certain period, the life cycle, etc.) )
  • Get UIApplication object: [UIApplication sharedapplication]
  • Once an iOS program is started, the first object created is the UIApplication object, and there is only one.
Example: Get two UIApplication objects by code, print address can see the address is the same.
1-(void) Viewdidload {2   [Super Viewdidload]; 3  4   //Get App object proves that there is only one UIApplication object in an application 5   UIApplication *APP1 = [UIApplication sharedapplication];  6  7   uiapplication *app2 = [UIApplication sharedapplication]; 8   NSLog (@ "app1=%p----APP2 =%p", App1, APP2); 9 10}
Printing results: 2015-12-02 13:34:58.847 uiapplication[4563:852645] app1=0x7fe2b3e0eaa0----APP2 = 0x7fe2b3e0eaa0
Feature 2:
  • Each application has its own UIApplication object, and it is a singleton.
  • If you try to create a new UIApplication object in your program, you will be prompted with an error.
Example:
1-(void) Viewdidload {2   [super viewdidload];3  4   //Create a UIApplication object from alloc+ init, report exception 5   UIApplication *app = [[UIApplication alloc] init];6  7   NSLog (@ "%p", app); 8}
Printing results: 2015-12-02 13:39:00.270 uiapplication[4675:867735] * * * terminating app due to uncaught exception ' Nsinternalincon Sistencyexception ', Reason: ' There can only be one uiapplication instance. '
Feature 3:
  • Use the UIApplication object to perform some application-level operations
(1) Apply icon to display numbers (see 1.1 Common properties)
(2) Wait icon indicator on the status bar (see 1.1 Common properties)
(3) using UIApplication to open a resource (OpenURL: Method)? The system automatically opens with an app based on protocol recognition
1//Open a webpage: 2 [app Openurl:[nsurl urlwithstring:@ "http://ios.icast.cn"]; 3  4//Call 5 [app Openurl:[nsurl urlwithstring:@ "tel://10086"]; 6  7//SMS 8 [app Openurl:[nsurl urlwithstring:@ "s ms://10086 "]; 9 10//Email [app Openurl:[nsurl urlwithstring:@ "Mailto://[email protected]"]];12  
? Using the OpenURL method, you can also open other apps and call each other between different apps. U.S. 美图秀秀, click to share to "Sina Weibo", open "Sina Weibo" select account, jump back to "Mei 美图秀秀", began to share Himalaya, use Weibo, QQ account login. Need to jump between applications.
(4) Manage status bar through UIApplication (see 1.2 Management status bar)
1.1-Common PropertiesCommon Properties of UIApplication
1//The Red reminder number in the upper right corner of the application icon (default is 0) 2 @property (nonatomic) Nsinteger applicationiconbadgenumber; 3  4 Example: 5  6-(void) Viewdidload {7   [super Viewdidload]; 8  9  //Get a Singleton object   uiapplication *app = [UIA Pplication sharedapplication];11   //create user notification settings (requires user permission to set up notifications after iOS8) if ([[[[   Uidevice Currentdevice] Systemversion] Floatvalue] >= 8.0) {uiusernotificationsettings *settings =15         [ Uiusernotificationsettings settingsfortypes:uiusernotificationtypebadge16                                           categories:nil];17      //Registration Rights 18     [app Registerusernotificationsettings:settings];19   }   //Set digit   App.applicationiconbadgenumber = 10;23 24}

3 Example: 4 5-(void) Viewdidload {6 [super Viewdidload]; 7 8 //Get a Singleton object 9 uiapplication *app = [Uiapplicat Ion sharedapplication];10 //Set network indicator app.networkactivityindicatorvisible = yes;13 14}

1.2-Manage status barThe system provides 2 ways to manage the status bar 1. Through Uiviewcontroller management (after IOS 7) (each uiviewcontroller can have its own different status bar) (recommended)
1//style of the status bar 2-(uistatusbarstyle) Preferredstatusbarstyle; 3  4//visibility of the status bar 5-(bool) Prefersstatusbarhidden; 6//Status bar visibility (default is NO)---uiviewcontroller management (recommended) 7-(BOOL) Prefersstat Usbarhidden {8   return NO; 9}10 11//status bar style---uiviewcontroller management (recommended)-(Uistatusbarstyle) preferredstatusbarsty Le {+/   * status bar style statusBarStyle15    uistatusbarstyledefault            status bar is black    uistatusbarstylelightcontent       status bar is white    UIStatusBarStyleBlackTranslucent18    UIStatusBarStyleBlackOpaque19    */20   return UISTATUSBARSTYLEDEFAULT;22}

2. Through UIApplication management (before IOS 6)
(1) After iOS 7, if you want to manage through uiapplication, first add a configuration item to the Info.plist file
  • Key:view controller-based status bar appearance
  • Value:no
1//status bar style---uiapplication management 2-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event {3
      
       4   //Get Singleton object 5   uiapplication *app = [UIApplication sharedapplication]; 6  7   //animate the effect to set the style of the status bar-black to white (expired) 8   [app Setstatusbarstyle:uistatusbarstylelightcontent Animated:yes]; 9   //Hide status bar   one by one App.statusbarhidden = yes;12   //animation to hide the status bar (expired iOS 3.2 deprecated)   [app Setstatusbarhidden:yes animated:yes];15   */* Withanimation: Method  (expired)    Uistatusbaranimationnone   No special effects    Uistatusbaranimationfade   Fade Effect    Uistatusbaranimationslide  exit Hide    */21   [app Setstatusbarhidden:yes withanimation: UISTATUSBARANIMATIONSLIDE];22}
      
Use occasions
UIApplication: The style of the status bar is set only once, using the animation effect
Uiviewcontroller: Status bar is hidden, style is different
1.3-uiapplicationdelegateIntroduced
  • All mobile operating systems have a fatal disadvantage: apps are vulnerable to interruptions.
  • For example, a call or lock screen will cause the app to go backstage or even be terminated, and many other similar situations will cause the app to be disturbed.
  • When the app is disturbed, some system events occur, and UIApplication notifies its delegate object to handle these system events.
Delegate can handle events such as:
  • Application lifecycle events (such as program startup and shutdown)
  • System events (such as incoming calls)
  • Memory warning
  • ... ...
Brief introduction:
  • The Appdelegate file after the project is created is the proxy object for uiapplication.
  • The proxy object is already set up in the main function, so we don't have to set it manually.
Settings in the main function:
File location: Supporting files-> MAIN.M
1 #import <UIKit/UIKit.h> 2 #import "AppDelegate.h" 3  4 int main (int argc, char *argv[]) {5   @autoreleasepoo L {6       7     //Set Start UIApplication object, and corresponding proxy object Appdelegate 8     return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class])); 9   }10}
Attention:
The main function of appdelegate is to process (listen) The various events of the application itself
To become a proxy object for uiapplication, you must comply with the following: Uiapplicationdelegate protocol
1.3.1-Proxy Method Appdelegate file By default has complied with the Uiapplicationdelegate protocol, is already the agent of Uiapplicationdelegate
1//  APPDELEGATE.H2  3 #import <uikit/uikit.h>4 5 @interface Appdelegate:uiresponder < Uiapplicationdelegate>6 7 @property (Strong, nonatomic) UIWindow *window;8  9 @end
Processing (listening) of various events of the application itself:
 1//APPDELEGATE.M 2 3 #import "AppDelegate.h" 4 5 @interface Appdelegate () 6 7 @end 8 9 @implementation Appdelegat E10//app starts after the call to the method starts, this method will no longer be called! 12//If the application is killed by the operating system due to memory and other reasons, click the icon again, this method will be called!   -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {14 NSLog (@ "%s", __func__), yes;16}17//app is about to exit active (will lose focus: cannot interact with user) (this method is called when calling or texting) 19//The game should stop the tour in this method The play Process! This method is especially important in game development! (void) Applicationwillresignactive: (uiapplication *) application {NSLog (@ "%s", __func__),}23//app has entered the background call The method (save the application's data and status in this method) 25//The application exits to the background, frees the shared resource, saves the user data, stops the clock, and saves enough application state information ... -(void) Applicationdidenterbackground: (uiapplication *) application {NSLog (@ "%s", __func__),}29//app will enter the front desk When the method is called (the data and state of the application are restored in this method)-(void) Applicationwillenterforeground: (uiapplication *) application {NSLog (@ "%s", __ FUNC__);}34//app has entered the active state, restarting the original paused state (regain focus: can interact with the user)-(void) Applicationdidbecomeactive: (UIApplication *) Application {PNS NSLog (@ "%s", __func__), and the app receives a memory warning call this method-(void)}40 Emorywarning: (uiapplication *) application {NSLog (@ "%s", __func__);}45//app will be killed call this method 47//NOTE: 1. The user actively shuts down 2. System because Memory Not enough shutdown program-(void) Applicationwillterminate: (uiapplication *) application {NSLog (@ "%s", __func__); Wuyi}52 @end

iOS uiapplication detailed

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.