对我应该观察哪些应用程序状态通知感到困惑

Tom*_*you 3 ios swift

我的应用程序是一个使用语音和视频的直播应用程序。我想要:

  1. 检测用户何时通过应用程序打开通知中心/控制中心
  2. 检测用户何时收到某种全屏通知(例如电池电量低)
  3. 检测用户何时接到电话
  4. 检测用户何时按下主页按钮以使应用程序后台运行
  5. 检测应用程序何时终止。

我有点困惑我应该观察哪些通知来检测这些事件。

我的猜测是:

  1. .willResignActiveNotification
  2. .willResignActiveNotification
  3. .willResignActiveNotification或者.didEnterBackgroundNotification
  4. .didEnterBackgroundNotification
  5. .willTerminateNotification

并检测应用程序何时返回其活动状态 1 到 4 我需要.didBecomeActiveNotification

这是正确的吗?哪一个是 3 号?

Bha*_*ara 6

是的,您应该观察.willResignActiveNotification,因为您的应用程序仍然存在于 iOS 的 Phone Application 下方,当有来电时,iOS 会显示该应用程序。.didEnterBackgroundNotification来电时不会触发,按下主页按钮时会触发。

现在,一旦您通过拒绝通话或结束通话结束通话,Phone ApplicationiOS 将从顶部删除并使您的应用程序处于活动状态。所以你可以在那里观察.didBecomeActiveNotification所有情况。

当您创建新项目时,您还可以检查 Xcode 提供的方法中的注释行。结帐AppDelegate.swift以了解差异

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 invalidate graphics rendering callbacks. 
        // Games should use this method to pause the game.
}

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.
}
Run Code Online (Sandbox Code Playgroud)

结合你的案例总结一下:

  1. 检测用户何时接到电话

    只会.willResignActiveNotification被解雇。

  2. 检测用户何时按下主页按钮以使应用程序后台运行

    两者.willResignActiveNotification.didEnterBackgroundNotification将被解雇。

希望能帮助到你。