UIApplication简单从字面上了解就是应用程序,开发的时候有的时候会根据需要调用其中的方法,看起来不起眼,实际在iOS开发UIApplication提供了iOS程序运行期间的控制和协作工作。每一个应用程序在运行期必须有且仅有一个UIApplication(或则其子类)的一个实例,就是设计模式中常说的单例模式,通过sharedApplication获取单例对象,不需要另外的init一个UIApplication。

UIApllication概念

经常程序可能编译的时候没错,运行的时候报错,通常都是停留在main.m中,这是程序的入口点,也从这里创建了UIApplication单例实例。



int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}



UIIApplication一般主要用于处理用户事件,处理的时候会新建一个队列,将所有用户事件都放入队列,逐一处理,在处理的时候,它会发送当前事件 到一个合适的处理事件的目标控件。同时UIApplication实例还维护一个在本应用中打开的window列表(UIWindow实例),这样UIApplication可以获取应用中的任何一个UIView对象。UIApplication实例会被赋予一个代理对象,以处理应用程序的生命周期事件(比如程序启动和关闭),系统事件(比如电话、短信)等等。具体看下具体应用,

故事板中新建一个按钮,之后的画获取点击事件,加入以下两行代码:



UIApplication *application=[UIApplication sharedApplication];
    //设置图标上的更新数字
    application.applicationIconBadgeNumber=25;



在iOS8.0之前会看到App上面有红色的数字显示,前几个月微信头像整人事件,不少人被坑了,强迫自己几天没看微信,看到小红点就想点,控制台会提示以下信息:Attempting to badge the application icon but haven't received permission from the user to badge the application

具体解决办法很简单,就是获取以下机器的版本号,然后根据需要通知用户选择设置(个人觉得更人性化,手机上要是满屏的小红点,想想就。。);



float versionNumber=[[[UIDevice currentDevice] systemVersion] floatValue];

    if (versionNumber >= 8.0) {
        NSLog(@"%@",[NSString stringWithFormat:@"当前的系统为%f",versionNumber]);
        //通知设置
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
        //接收用户的选择
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }



 UIApplication可以做一些其他的事情,比如其中的openURL就比较实用:



//电话
    [application openURL:[NSURL URLWithString:@"tel://10010"]];
    //短信
    [application openURL:[NSURL URLWithString:@"sms://10010"]];
    //邮件
    [application openURL:[NSURL URLWithString:@"mailto://10000@qq.com"]];
    //URL
    [application openURL:[NSURL URLWithString:@""]];



UIApplicationDelegate

UIApllicationDelegate协议中定义的方法有有一部分是和应用程序的状态是相关的,新建的项目默认的都有AppDelegate.h和AppDelegate.m文件,可以先参考以下默认生成的AppDelegate.m代码:



//
//  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-程序完成加载");
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    NSLog(@"applicationWillResignActive-注销激活");
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    NSLog(@"applicationDidEnterBackground-进入后台");
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
      NSLog(@"applicationWillEnterForeground-进入前台");
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    NSLog(@"applicationDidBecomeActive-成为激活状态");
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
      NSLog(@"applicationWillTerminate-程序终止状态");
}

@end



最开始main函数中其实是将AppDelegate实例化交给UIApplication去代理执行其中的方法,如果你喜欢这个AppDelegate文件,完成有必须要根据需要新建一个自己的类文件,只要声明UIApplicationDelegate即可。关于上面的状态我们可以先看一张苹果官网的图:

iOS 开发allValues 顺序 ios开发ui_iOS

 

其中方法苹果默认有解释,不过大概写下个人的理解:

 application:didFinishLaunchingWithOptions:—应用展示给用户之前完成最终的初始化工作

applicationWillResignActive:-从激活状态进入休眠状态,突然的电话,短信可以使当前程序进入后台,不可交互

applicationDidEnterBackground-应用程序进入后台,可以保存和传输数据

applicationWillEnterForeground-可以在这里恢复数据,即将进入前台,还不是激活状态

applicationDidBecomeActive-进入前台获取获取焦点,可以交互

applicationWillTerminate-应用程序销毁的时候调用,如果挂起不调用

写到这里可以具体看一下App的启动过程:



1.main函数

2.UIApplicationMain 创建UIApplication对象,创建UIApplication的delegate对象(监听应用程序状态)

3.根据Info.plist获得最主要storyboard的文件名,加载最主要的storyboard,创建UIWindow(如果没有故事板,程序启动完毕时调用代理的application:didFinishLaunchingWithOptions:方法,在application:didFinishLaunchingWithOptions:中创建UIWindow ),创建和设置UIWindow的rootViewController, 显示窗口;