The role of Info.plist and PCH files, uiapplication,ios program startup process, Appdelegate method interpretation, UIWindow, life cycle approach

Source: Internet
Author: User
Tags vars

Transferred from: http://blog.csdn.net/dwt1220/article/details/29373817

Info.plist Common Settings
After building a project, you will see a "project name-info.plist" file under the supporting Files folder, which is very important for the project to do some run-time configuration, so it cannot be deleted.
Note: In the project created by the older version of Xcode, this profile name is called "Info.plist"
Other plist files in the project cannot have the word "Info", or they will be mistaken for the "info.plist" that is very important in the legend.

There is also a infoplist.strings file in the project that is related to localization of info.plist files


Info.plist Common Properties

Localiztion native Development Region (cfbundledevelopmentregion)-Localization related
Bundle Display Name (cfbundledisplayname)-a program that displays the names after installation, limited to 10-12 characters, and if exceeded, will be displayed abbreviated names
Icon file (cfbundleiconfile)-app icons name, typically icon.png
Bundle version (cfbundleversion)-The version number of the application, which needs to be increased each time a new version is posted to the App Store
Main Storyboard file base name (nsmainstoryboardfile)-the name of the primary storyboard

Bundle identifier (cfbundleidentifier)-Unique identification of the project, deployed to the real machine






PCH file

The project's supporting files folder has a "project name-prefix.pch" file, which is also a header file
The contents of the PCH header file can be shared and accessed by all other source files in the project
In general, some global macros are defined in the PCH file
Add the following preprocessing directives in the PCH file, and then use the log (...) in your project. To output the log information, the NSLog statement can be removed at once when the application is published (debug mode is defined)


#ifdef DEBUG
#define LOG (...) NSLog (__va_args__)
#else
#define LOG (...)
#endif


UIApplication

UIApplication objects are symbols of the application
Each application has its own UIApplication object, and it is a singleton
This singleton object can be obtained by [UIApplication Sharedapplication]

[OBJC]View Plaincopy
    1.     uiapplication  *app = [uiapplication SHAREDAPPLICATION];  
    2.     uiapplication *app1  = [uiapplication SHAREDAPPLICATION];  
    3. Li class= "alt" > //    uiapplication *app2 = [[ uiapplication alloc] init];   will go wrong.   
    4.     nslog (@ "%p - %p ",  app, app1);  //  is the same address   


The first object created after an iOS program is started is a UIApplication object
Use the UIApplication object to perform some application-level operations
Such as:

[OBJC]View Plaincopy
  1. UIApplication *app = [uiapplication sharedapplication];
  2. App. applicationiconbadgenumber = 998; //Set the number on the application icon
  3. App. networkactivityindicatorvisible = YES; //Set up Networking animations for the status bar
  4. //Set the style of the status bar
  5. //App.statusbarstyle = uistatusbarstylelightcontent;  The difference from the following method is that animations cannot be set.
  6. [App setstatusbarstyle:uistatusbarstylelightcontent animated:YES];
  7. //Set whether the status bar is hidden
  8. App. Statusbarhidden = YES;  //The difference from the following method is that the animation cannot be set.
  9. [App Setstatusbarhidden:YES Withanimation:uistatusbaranimationfade];
  10. Nsurl *url = [Nsurl urlwithstring:@ "http://ios.itcast.cn"];
  11. [App Openurl:url];


Note: If you want to use uiapplication to manage the status bar in IOS7, you first need to modify the Info.plist settings





In iOS7, the status bar is managed by Uiviewcontroller by default, and Uiviewcontroller can easily manage the visibility and style of the status bar by implementing the following methods
Style of the status bar

[OBJC]View Plaincopy
    1. -(Uistatusbarstyle) Preferredstatusbarstyle;

Visibility of the status bar

[OBJC]View Plaincopy
    1. -(BOOL) Prefersstatusbarhidden;


OpenURL

[OBJC]View Plaincopy
    1. -(BOOL) OpenURL: (nsurl*) URL;


OpenURL: Some of the functions of the method are

Call, send text messages, email, open a web resource

[OBJC]View Plaincopy
  1. Call
  2. UIApplication *app = [uiapplication sharedapplication];
  3. [App Openurl:[nsurl urlwithstring:@ "tel://10086"];
  4. Texting
  5. [App Openurl:[nsurl urlwithstring:@ "sms://10086"];
  6. Send e-mail
  7. [App Openurl:[nsurl urlwithstring:@ "Mailto://[email protected]"];
  8. Open a Web resource
  9. [App Openurl:[nsurl urlwithstring:@ "http://ios.itcast.cn"];



iOS program start-up process




UIApplication and delegate
Delegate events that can be handled include:
Application lifecycle events (such as program startup and shutdown)
System events (such as incoming calls)
Memory warning



A uiapplicationmain function is executed in the main function.

[OBJC]View Plaincopy
    1. int Uiapplicationmain (int argc, charchar *argv[], nsstring *principalclassname, NSString *   Delegateclassname);


ARGC passed in several parameters
argv The value of the passed-in parameter
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
Delegateclassname: Specifies the application's proxy class, which must comply with the Uiapplicationdelegate protocol


Appdelegate Method Explanation

[OBJC]View Plaincopy
  1. Called when the application is finished (automatically called by the system)
  2. -(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) Launchoptions
  3. {
  4. return YES;
  5. }
  6. Called when it is about to lose its active state (loses focus, cannot interact)
  7. -(void) applicationwillresignactive: (uiapplication *) application
  8. {
  9. }
  10. Regain focus (ability to interact with users)
  11. -(void) applicationdidbecomeactive: (uiapplication *) application
  12. {
  13. }
  14. Called when the application enters the background
  15. The application's data is typically stored in this method, as well as the state
  16. -(void) Applicationdidenterbackground: (uiapplication *) application
  17. {
  18. }
  19. Called when the application is about to enter the foreground
  20. Typically restores the application's data in this method, as well as the state
  21. -(void) Applicationwillenterforeground: (uiapplication *) application
  22. {
  23. }
  24. This method is called when the application is about to be destroyed
  25. Note: This method cannot be called if the application is in a suspended state
  26. -(void) Applicationwillterminate: (uiapplication *) application
  27. {
  28. }
  29. When the application receives a memory warning, it calls the
  30. It is common to release unwanted memory in this method
  31. -(void) applicationdidreceivememorywarning: (uiapplication *) application
  32. {
  33. }


UIWindow

UIWindow is a special kind of uiview that usually has only one uiwindow in an app.
After the iOS program starts, the first view control created is UIWindow, then the controller's view is created, the controller's view is added to the UIWindow, and the controller's view is displayed on the screen.


An iOS program can be displayed on the screen entirely because it has UIWindow
It says that without UIWindow, you can't see any UI interface.


There are two common ways of adding UIView to UIWindow:

1. Use (void) Addsubview: (UIView *) view; Method

[OBJC]View Plaincopy
  1. Once the program is started, it is called once, (methods in Appdelegate)
  2. -(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) Launchoptions
  3. {
  4. //Create UIWindow
  5. self. window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds];
  6. //Create Controller
  7. //Njviewcontroller *VC = [[Njviewcontroller alloc] init];
  8. //Add the view of the controller to the UIWindow
  9. [Self. Window ADDSUBVIEW:VC. view];
  10. Note: An unknown error will occur, and it is not recommended to use
  11. //Set the root controller of the UIWindow (recommended using this method) Self.window.rootViewController = VC;
  12. }


Common methods

[OBJC]View Plaincopy
    1. An application can have only one main window
    2. [Self. window makekeyandvisible];
    3. Let UIWindow become the main window
    4. [Self.window Makekeywindow];


UIWindow's acquisition

[OBJC]View Plaincopy
    1. Get the main window of the application
    2. NSLog (@ "%@", [uiapplication sharedapplication]. Keywindow);
    3. Get the UIWindow where a uiview is located
    4. View. Window


Note: The UIWindow used to receive message events for keyboards and non-touch classes, and only one uiwindow per time in a program is Keywindow. If a text box inside a UIWindow cannot enter text, it may be because the UIWindow is not Keywindow (especially before IOS7)


Four object diagrams



Life cycle Approach

The role of Info.plist and PCH files, uiapplication,ios program startup process, Appdelegate method interpretation, UIWindow, life cycle approach

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.