iOS - UIApplication

前言

    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
优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/312261

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
Mario,低调大师唯一一个Java游戏作品

Mario,低调大师唯一一个Java游戏作品

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Apache Tomcat7、8、9(Java Web服务器)

Apache Tomcat7、8、9(Java Web服务器)

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

Eclipse(集成开发环境)

Eclipse(集成开发环境)

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。

Sublime Text 一个代码编辑器

Sublime Text 一个代码编辑器

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。