前言

	NS_CLASS_AVAILABLE_IOS(2_0) @interface UIApplication : UIResponder
@available(iOS 2.0, *) public class UIApplication : UIResponder

1、Application 相关方法

  • Objective-C

    	// 运行程序时,必须执行的方法(程序入口)
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch.
    return YES;
    } // 当前应用程序将要进入非活动状态(进入后台)(Will, Should 将要的意思)
    - (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.
    */
    } // 当前程序已经进入后台(Did 已经的意思)
    - (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.
    */
    } // 当前程序将要进入前台
    - (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.
    */
    } // 当前程序已经变成活动状态(进入前台)
    - (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.
    */
    } // 当前程序运行结束
    - (void)applicationWillTerminate:(UIApplication *)application {
    /*
    Called when the application is about to terminate. Save data if appropriate. See also
    applicationDidEnterBackground:.
    */
    } // 内存紧张
    - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { // try to clean up as much memory as possible. next step is to terminate app
    }
  • Swift

    	// 运行程序时,必须执行的方法(程序入口)
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch.
    return true
    } // 当前应用程序将要进入非活动状态(进入后台)(Will, Should 将要的意思)
    func applicationWillResignActive(application: UIApplication) {
    /*
    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.
    */
    } // 当前程序已经进入后台(Did 已经的意思)
    func applicationDidEnterBackground(application: UIApplication) {
    /*
    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.
    */
    } // 当前程序将要进入前台
    func applicationWillEnterForeground(application: UIApplication) {
    /*
    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.
    */
    } // 当前程序已经变成活动状态(进入前台)
    func applicationDidBecomeActive(application: UIApplication) {
    /*
    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.
    */
    } // 当前程序运行结束
    func applicationWillTerminate(application: UIApplication) {
    /*
    Called when the application is about to terminate. Save data if appropriate. See also
    applicationDidEnterBackground:.
    */
    } // 内存紧张
    func applicationDidReceiveMemoryWarning(application: UIApplication) { // try to clean up as much memory as possible. next step is to terminate app
    }

2、main 方法

  • Objective-C

    	int main(int argc, char * argv[]) {
    
    		@autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
    }
    • 作用:main 函数中只调用了一个 UIApplicationMain 的函数,整个程序的入口可以认为从 main 交给了 UIApplicationMain 函数。

    • 函数原型:

      	UIKIT_EXTERN int UIApplicationMain(int argc,
      char *argv[],
      NSString * __nullable principalClassName,
      NSString * __nullable delegateClassName);
      • argc, argv[]:与 main 的两个参数相同,一个是整形,一个是指针数组。

      • principalClassName:委托方类名,这个类实时检测当前程序的运行状态,这个参数一定要是 UIApplication 类或其子类,如果参数为空 nil,默认为 UIApplication 。

      • delegateClassName:代理方类名,遵守 UIApplicationDelegate 协议,实现协议中的方法,当第三个参数中的委托方检测到当前程序状态改变时会委托第四个参数在状态改变时执行相应的操作。

3、状态栏的设置

  • Objective-C

        // 获取状态栏高度
    /*
    returns CGRectZero if the status bar is hidden,默认高度为 20.0
    */
    CGFloat height = [UIApplication sharedApplication].statusBarFrame.size.height; // 显示/隐藏状态栏
    /*
    需在 Info.plist 添加 key:View controller-based status bar appearance,value:NO statusBarHidden:YES 隐藏,NO 显示(默认)
    */
    [UIApplication sharedApplication].statusBarHidden = NO; // 设置状态栏颜色
    /*
    需在 info.plist 添加 key:View controller-based status bar appearance,value:NO
    在 iOS7 版本以前直接设置就可以 UIStatusBarStyleDefault = 0, Dark content, for use on light backgrounds 黑色内容,默认
    UIStatusBarStyleLightContent = 1, Light content, for use on dark backgrounds 白色内容
    */
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  • Swift

        // 获取状态栏高度
    /*
    returns CGRectZero if the status bar is hidden,默认高度为 20.0
    */
    let height:CGFloat = UIApplication.sharedApplication().statusBarFrame.size.height // 显示/隐藏状态栏
    /*
    需在 Info.plist 添加 key:View controller-based status bar appearance,value:NO statusBarHidden:true 隐藏,false 显示(默认)
    */
    UIApplication.sharedApplication().statusBarHidden = false // 设置状态栏颜色
    /*
    需在 info.plist 添加 key:View controller-based status bar appearance,value:NO
    在 iOS7 版本以前直接设置就可以 case Default Dark content, for use on light backgrounds 黑色内容,默认
    case LightContent Light content, for use on dark backgrounds 白色内容
    */
    UIApplication.sharedApplication().statusBarStyle = .LightContent

4、状态栏上网络状态风火轮的设置

  • Objective-C

        // 风火轮旋转状态设置
    /*
    YES 开始旋转,NO 停止旋转(默认),停止时自动隐藏
    */
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  • Swift

    	// 风火轮旋转状态设置
    /*
    true 开始旋转,false 停止旋转(默认),停止时自动隐藏
    */
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true

iOS - UIApplication的更多相关文章

  1. 【iOS [[UIApplication sharedApplication] delegate]】运用

    之前想要拿到app的窗口,我们通常的写法是: [UIApplication sharedApplication].keyWindow 这种写法之前一直也觉得是正确的,没什么问题,而且网上大多数的博客或 ...

  2. iOS UIApplication sharedapplication用法

    应用中打开其他应用 我们来讨论一下,在iOS开发中,如何实现从app1打开app2. 基本的思路就是,可以为app2定义一个URL,在app1中通过打开这个URL来打开app2,在此过程中,可以传送一 ...

  3. IOS UIApplication使用

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  4. 【转】iOS UIApplication详解

    1.状态栏UIStateBar的设置是在UIApplication里面设置的,它包含4中风格 2. - (void)beginIgnoringInteractionEvents; (void)endI ...

  5. ios UIApplication简单使用

    每个app有且只有一个UIApplication对象,当程序启动的时候通过调用UIApplicationMain方法得到的.可以通过sharedApplication方法得到. UIApplicati ...

  6. iOS UIApplication的代理方法总结

    1.简单介绍 1> 整个应用程序的象征,一个应用程序就一个UIApplication对象.使用了单例设计模式 2> 通过[UIApplication sharedApplication]訪 ...

  7. IOS UIApplication和AppDelegate 关系

    UIApplication.AppDelegate.委托等的关系?  什么是委托?为什么要有委托?委托在Iphone中的实现机制是怎样的? 一般来说,我们创建了一个Iphone项目,默认会有这个mai ...

  8. iOS:UIApplication和它对象的代理

    <1>UIApplication的对象是单例对象   类方法:UIApplication *app = [UIApplication sharedAppplication] <2&g ...

  9. iOS UIApplication 里面各const实际用意

    //后台通知:屏幕操作通知等等 UIKIT_EXTERN NSString *const UIApplicationDidEnterBackgroundNotification       NS_AV ...

随机推荐

  1. Eclipse如何设置字体

    Eclipse 是一个开放源代码的.基于Java的可扩展开发平台,是学习java和开发java最常用的IDE之一.有时候会遇到这种情况,刚刚下载了新的Eclipse,字体显示英文没问题,但是显示中文就 ...

  2. PHP笔记随笔

    1.CSS控制页面文字不能复制: body{-webkit-user-select:none;}   2.[php过滤汉字和非汉字] $sc="aaad....##--__i汉字过滤&quo ...

  3. Codeforces 745C:Hongcow Builds A Nation(并查集)

    http://codeforces.com/problemset/problem/744/A 题意:在一个图里面有n个点m条边,还有k个点是受限制的,即不能从一个受限制的点走到另外一个受限制的点(有路 ...

  4. postgresql 热备与恢复

    一. PostgreSQL热备份的过程一般为: 数据库中执行:pg_start_backup() ; 然后使用操作系统的tar或 cp命令拷贝 PostgreSQL数据文件. 数据库中执行:pg_st ...

  5. Android关机闹钟实现

    Android关机闹钟实现 时间转换网站:http://tool.chinaz.com/Tools/unixtime.aspx 1.apk层 这个还是比较简单的,百度一下就可以看到apk的代码,我之前 ...

  6. jqeury之轮播图

    $(document).ready(function(){ var sWidth = $('#pic1').width(); var len = $('#pic1 .sildebar li').len ...

  7. 过滤器Filter(拦截jsp页面的跳转)案例:

    创建一个 Filter , class类: 其继承于 接口 Filte(接口导包:import javax.servlet.Filter;) 在 web.xml 文件中配置并映射该 Filter. 其 ...

  8. 银行账户管理系统(oracle数据库连接池,数据库的链接,)

    /* * 银行账户管理系统: * 属性:账户id,姓名,金额salary,利息类型: *管理员模块实现的功能: * 1.给用户开户 * 2.查询所有账户信息 * 用户模块实现的功能: * 1.显示用户 ...

  9. Unity-Animator深入系列---StateMachineBehaviour状态机脚本学习

    回到 Animator深入系列总目录 首先这个脚本必须继承自StateMachineBehaviour public class MySMB : StateMachineBehaviour { pub ...

  10. Linux如何查看当前占用CPU或内存最多的K个进程

    一.可以使用以下命令查使用内存最多的K个进程 方法1: ps -aux | sort -k4nr | head -K 如果是10个进程,K=10,如果是最高的三个,K=3 说明:ps -aux中(a指 ...