"Thermal Notifications in iOS App"


To all Mac users, you might have heard that annoying noise of fan from your laptop. From a developer perspective, this noise is pretty much a bad sign. More CPU usage, more memory consumption leads to this. Worst part, your laptop battery get drained immediately. No body likes to have a noisy laptop, Right? OK, as a developer I can make excuses like, all Macbooks got good battery backup and my app can survive with that. But as a user, will you agree to it? No! And what if the same situation arise in iOS apps? You might have felt heat spicing up in your iPhone/iPad back cover while using some VOIP applications or some chat apps. Well, we have Thermal Notifications to help in this case for both OSX and iOS. Here we discuss about iOS apps. It is the same in OSX. Let's get started! If you think, that your app might have some serious thermal related issues, for high CPU usage or high memory usage as we discussed above, then you should start monitoring the same. We will first add the observers to find the change in state for Thermal notifications by making use of ProcessInfo class.

//Registering for Thermal Change notifications
private func registerForThermalNotifications() {
                              selector: #selector(responseToHeat(*:)),
                  name: ProcessInfo.thermalStateDidChangeNotification,
                                               object: nil)
The states of thermal conditions are: 
  1. **Nominal*- : This is normal state and requires no attention.
  2. **Fair*- : This state your iPhone/iPad gets heated up slowly.
  3. **Serious*- : In this state, your device might start acting weird. Device unresponsiveness and touch cancellations might happen. This is the time when you need to control your app by reducing use of CPU and high memory. Can stop any download or upload progess right away till the system cools down.
  4. **Critical*- : Reduce everything now. If camera being used or VOIP application continuously running for a long time, it is time to cool down the entire system. 
If you want to know which state device currently on, you can add this snippet to your code. 
@objc private func responseToHeat(* notification: Notification ) {
        let state = ProcessInfo.processInfo.thermalState
        switch state {
            case .nominal: 
            // No action required as such
            case .fair: 
           // Starts getting heated up. Try reducing CPU expensive operations.
            case .serious: 
           // Time to reduce the CPU usage and make sure you are not burning more
            case .critical: 
          // Reduce every operations and make initiate device cool down.
Tips to reduce thermal scenarios: 
  1. Always check your CPU usage in Xcode. 
  2. Check Memory allocations, leaks in your code.
  3. Handle your didReceiveMemoryWarning wisely. 
  4. Make sure you do Time Profiling for your app and see which functions are taking more time. More time relates to more expensive operations. Break it down.
Take care of your app. Battery drain or heating up is something which users dislike the most. 
Good luck devs.

-"Thermal Notifications in iOS App"