iOS应用程序的状态

作者: Andy_Ron | 来源:发表于2017-07-07 21:00 被阅读135次
    • iOS应用程序一共有五种状态:
    • Not Running 程序还没运行
    • Inactive 程序运行在foreground但没有接收事件
    • Active 程序运行在foreground接收事件
    • Background 程序运行在background正在执行代码
    • Suspended 程序运行在background没有执行代码
    • iOS应用程序状态变化会回调APPDelegate中的方法,但不是每一种状态变化都会有对应的方法(上图的红框的两个变化就没有对应的方法)

      • application:didFinishLaunchingWithOptions: Not Running -> Inactive
      • applicationDidBecomeActive: Inactive -> Active
      • applicationWillResignActive: Active -> Inactive
      • applicationDidEnterBackground: Background -> Suspended
      • applicationWillEnterForeground: Background -> Inactive
      • applicationWillTerminate: Suspended -> Not Running
    • 常见的应用状态变化场景

      • 程序第一次启动(或者被杀掉以后启动):
        Not Running -> Inactive -> Active
      • 点击Home键(没有在Inof.plist中设置Application does not run in background):
        Active -> Inactive -> Background -> Suspended
      • 点击Home键(在Inof.plist中设置Application does not run in backgroundYES,应用不能运行在后台,进入后台后会立即进入Not Running):
        Active -> Inactive -> Background -> Suspended -> Not Running
      • 挂起重新运行
        Suspended -> Background -> Inactive -> Active
      • 内存清除(杀掉应用或删除应用)
        Suspended -> Not Running
      • 应用之间的切换
        Active -> Inactive
        Inactive -> Active
      • 点击Home键(在Inof.plist中设置Application does not run in backgroundYES,应用不能运行在后台,进入后台后会立即进入Not Running):
        Active -> Inactive -> Background -> Suspended -> Not Running
    • 可通过在APPDelegate的回调方法中打印数据,来查看应用状态变化

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
            print("`application:didFinishLaunchingWithOptions:`  Not Running -> Inactive")
            return true
        }
    
        func applicationWillResignActive(_ application: UIApplication) {
    
            print("`applicationWillResignActive:`  Active -> Inactive")
        }
    
        func applicationDidEnterBackground(_ application: UIApplication) {
            
            print("`applicationDidEnterBackground:`  Background -> Suspended")
        }
    
        func applicationWillEnterForeground(_ application: UIApplication) {
            
            print("`applicationWillEnterForeground:` Background -> Inactive")
        }
    
        func applicationDidBecomeActive(_ application: UIApplication) {
    
            print("`applicationDidBecomeActive:`  Inactive -> Active")
        }
    
        func applicationWillTerminate(_ application: UIApplication) {
    
            print("`applicationWillTerminate:`  Suspended -> Not Running")
        }
    
    
    欢迎您访问我的的微信公众号:欣欣向戎(微信号: andy_ron1587 )!

    相关文章

      网友评论

        本文标题:iOS应用程序的状态

        本文链接:https://www.haomeiwen.com/subject/zuxrhxtx.html