Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Keeping iOS application running during system alert overlays and temporary interruptions

Discussion in 'iOS and tvOS' started by KevinCodes4Food, Jun 29, 2015.

  1. KevinCodes4Food

    KevinCodes4Food

    Joined:
    Dec 6, 2013
    Posts:
    61
    I have a Unity iOS application that needs to continue running at all times when in the foreground, even when behind a system alert overlay or during other temporary interruptions.

    By default, Unity pauses an app when a system alert happens. For example, Unity will pause behind the low battery indicator or the trust this computer overlay that pops up when you plug into a new computer. This is the correct behavior for most games. However, my application needs to alert users very reliably at specific times and, ideally, show a running clock. If paused, it cannot generate an alert nor update the clock.

    Unfortunately, local notifications are no help here for an alert, either. Local notifications work great when my app is in the background, but will not show when in this paused state. To iOS, the paused app is still in the foreground, and Apple does not show local notifications for apps in the foreground. And the running clock is paused while awaiting the notification, anyway.

    I see that code in UnityAppController.mm pauses all Unity operations when it receives an applicationWillResignActive signal from the device. From Apple's state transition document, applicationWillResignActive is called not just when going to the background, but also during system alert overlays and other temporary interruptions.

    Instead of pausing on applicationWillResignActive, I would prefer to pause only when actually entering background state. Per the Apple docs, this would be the on a call to applicationDidEnterBackground.

    https://developer.apple.com/library...StrategiesforHandlingAppStateTransitions.html

    My solution has been to move all of the pause code in Unity's UnityAppController.mm from applicationWillResignActive to applicationDidEnterBackground. This appears to be working. However, I wanted to see if this might cause problems or if there might be a better way.

    So, for the Unity iOS community:
    1. Is the best way to keep a Unity iOS app running during system alerts and temporary interruptions to migrate the pause code from applicationWillResignActive to applicationDidEnterBackground in UnityAppController.mm? Or is there a better, easier way?
    2. Are there potential issues or risks to moving Unity's pause code to applicationDidEnterBackground?
     
  2. KevinCodes4Food

    KevinCodes4Food

    Joined:
    Dec 6, 2013
    Posts:
    61
    I have been testing a build with all pause code moved to applicationsDidEnterBackground and it appears to be working. I would be curious if anyone else has tried this or if there are risks, per my original questions.
     
  3. pep_dj

    pep_dj

    Joined:
    Nov 7, 2014
    Posts:
    178
    Hi @KevinCodes4Food I know it's an old thread. But, please, can you tell us if your approach worked well?
     
  4. juliens137

    juliens137

    Joined:
    Oct 2, 2017
    Posts:
    26
    Hi Kevin, Would love to know how this works and how you implemented it. I'm having trouble with my Unity Window disappearing when an alert pops up in a native ios app i'm working on