A detailed introduction to UIApplication

Source: Internet
Author: User

UIApplication
    • What is UIApplication?

      • The UIApplication object is a symbol of the program.
        Each application has its own UIApplication object, this object is the system automatically help us create, it is a 单例对象 .
        The first object created after an iOS program is started is a UIApplication object
        We can only obtain this singleton object by [UIApplication sharedapplication] and cannot create it manually.
    • uiapplication role?

      • The UIApplication object can be used in some 应用级别 operations.
        You can set the red reminder number in the upper right corner of the application icon
        Setting the visibility of an inline indicator
        You can set the application's status bar to jump between apps.
Imitate UIApplication single case.
    • Requirement: A program is created to create the object as soon as it starts.
      The object created can only get objects by share.
      Not able to perform alloc operation, the program crashes when Alloc is executed
    • 1. The object is created when the program starts.
      • The Load method is called when the class is loaded into memory, and it takes precedence over the main method to invoke the
static Persion *_instance;+ ( Span class= "Hljs-keyword" >void) load{want to save the created object, use the member property, but now is the class method, there is no way to access the member properties. So I got a self alloc] init];}     
    • 2. Ensure that the call Alloc will produce the exception, rewrite the system is the Alloc method

       +(instancetype)alloc {  当调用alloc方法时,先查看一下_instance有没有值,如果已经有值的话,直接抛出异常.  if(_instance) {      NSException *exception = [NSException exceptionWithName: @"NSInternalInconsistencyException"                                                       reason:      @"There can only be one Persion instance." userInfo:nil]; 抛出异常 [exception raise]; } 保持系统的做法. return [super alloc];}
    • 3. Get objects by share method
      + (instancetype)sharePersion {  return _instance  }
uiapplication function 1. Set App reminder numbers
获取UIApplication对象UIApplication *ap = [UIApplication sharedApplication];在设置之前, 要注册一个通知,从ios8之后,都要先注册一个通知对象.才能够接收到提醒. UIUserNotificationSettings *notice =[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];注册通知对象[ap registerUserNotificationSettings:notice];设置提醒数字ap.applicationIconBadgeNumber = 20;

Set the reminder number to 202. Set the network status
ap.networkActivityIndicatorVisible = YES;

Setting up user-friendly viewing networking status 3. Setting the status bar
    • Default

      Default condition
    • The application's status bar, which is managed by default, is assigned to the controller.
      The controller provides methods that can be overridden directly by this method
      Set the status bar style in the controller
- (UIStatusBarStyle)preferredStatusBarStyle {    return UIStatusBarStyleLightContent;    }

After Setup is complete
    • Hide the status bar by way of the Controller. The same implementation method:
      Return no when not hidden
      Hide when Yes is returned
- (BOOL)prefersStatusBarHidden {    return NO;    }
  • Usually in development are applications to manage the status bar. To do unified management, otherwise, there will be a lot of controllers. It's going to be very troublesome.
    • To have the application manage the status bar, configure it in Info.plist,
      In the last one add a key value:View controller-based status bar appearance(key:UIViewControllerBasedStatusBarAppearance)
      Set to No. is the application to manage. and控制器管理会无效
  • Manage status with UIApplication.
    • 1. Get UIApplication
      UIApplication *ap = [UIApplication sharedApplication];
    • 2. Set the status bar style.
      ap.statusBarStyle = UIStatusBarStyleLightContent;
    • 3. Setting the state of the Hide
      ap.statusBarHidden = YES;
    • 4. Jump page
      UIApplication *ap = [UIApplication sharedApplication]; URL:协议头://域名 应用程序通过协议头的类型,去打开相应的软件.NSURL *url =[NSURL URLWithString:@"http://www.baidu.com"]; [ap openURL:url];打电话[application openURL:[NSURL URLWithString:@"tel://10086"]];发短信[app openURL:[NSURL URLWithString:@"sms://10086"]];
UIApplication Agent
    • All mobile operating systems have a fatal disadvantage: app很容易受到打扰 .
      For example, a call or lock screen will cause the app to go backstage or even be terminated, there are many other similar situations will cause the app to be disturbed, when the app is disturbed, there will be some system events, then uiapplication 通知 its delegate object, Let the delegate agent handle these system events
    • Delegate events that can be handled include:

      • Application lifecycle events (such as program startup and shutdown)
      • System events (such as incoming calls)
      • Memory warning
      • ...
    • UIApplication will create a compliance agent at the start UIApplicationDelegate of the program.
      This is the Appdelegate class when we created the program. Appdelegate is the Uiapplicationdelegate protocol. In this class, you define many ways to listen for system events. It also defines some application lifecycle methods.

    • The main laws are:
      • The life cycle of an application
Called when the application starts to complete- (BOOL)Application:(uiapplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {return YES;} Called when our application is about to lose focus- (voidApplicationwillresignactive:(UIApplication *) Application {NSLog (@"%s", __func__);} Called when our application is completely in the background- (voidApplicationdidenterbackground:(UIApplication *) application{NSLog (@"%s", __func__);} Called when our application is about to enter the foreground- (void) applicationwillenterforeground:(uiapplication *) application {NSLog (@"%s", __func__);} When our application fully acquires focus, it is called only when an application has fully acquired the focus in order to interact with the user. -(void)applicationdidbecomeactive:(uiapplication *) application {- NSLog (@"%s", __func__ );} Called when our application is about to close -(void)applicationwillterminate:(uiapplication *) application {NSLog (@ "%s", __func__);}                
应?程序的启动原理(重要)
  • When the program starts, it executes the main function, which has the following actions in the main function.

    int main(int argc, char * argv[]) {  @autoreleasepool {      return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 第三个参数:UIApplication类名或者子类的名称 nil == @"UIApplication" NSStringFromClass好处:1.有提示功能 2.避免输入错误 }}
  • Program Complete START process

    • 1. Execute Main
    • 2. Execute the Uiapplicationmain function.
    • 3. Create the UIApplication object and set the proxy for the Uiapplicationmain object.
      The third parameter of UIApplication is the name of UIApplication, which defaults to uiapplication if it is specified as nil.
      The fourth parameter of UIApplication is the proxy for uiapplication.
    • 4. Turn on a main run cycle. Ensure that the application does not exit.
    • 5. Load info.plist. Load the configuration file. Determine if there is a info.plist file with the main storyboard filename base name
      The face has not specified the storyboard file, if any, to load the Info.plist file, if not, then the application load is complete.
    • 6. Notify the application, invoke the proxy method

Original link: http://www.jianshu.com/p/a72bcb948371

A detailed introduction to UIApplication

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.