Search Unity

Continue running & rendering game behind iOS Notification/Control Center

Discussion in 'iOS and tvOS' started by rober87, Sep 16, 2020.

  1. rober87

    rober87

    Joined:
    May 24, 2018
    Posts:
    2
    Hello, I am trying to make my iPad app continue to run (Update, rendering, networking, everything besides input) while the iOS Notification Center, Control Center, and the multitasking/app switcher (if it was just in full screen before opening the app switcher)

    ______________________________________

    the solution was to change UnityAppController.mm and move the code from
    applicationWillResignActive to applicationDidEnterBackground
     
    Last edited: Sep 16, 2020
  2. camogram

    camogram

    Joined:
    Jul 11, 2018
    Posts:
    29
    Thank you for posting your solution! :) This works in our case as well.

    Our use case was for a networked AR App, where frequently users would pull down the control center to record their screen, only to return to the app to have everything disconnected. Once we realised that Apple's own QuickLook was still running behind the control center we started looking for a solution.

    For anyone stumbling across this, here's a post process script that does this for you by swapping applicationWillResignActive and applicationDidEnterBackground


    Code (CSharp):
    1. // Belongs in an Assets/**/Editor folder
    2.  
    3. public class iOSPostProcessor
    4.     {
    5.         /// <summary>
    6.         /// Post processor to automate skipping the checkbox on testflight for encryption compliance.
    7.         /// </summary>
    8.         [PostProcessBuild(-999)]
    9.         public static void OnPostProcessBuildRunInBackground(BuildTarget buildTarget, string path)
    10.         {
    11.             if (buildTarget != BuildTarget.iOS)
    12.             {
    13.                 return;
    14.             }
    15.  
    16.             string filePath = Path.Combine(path, "Classes", "UnityAppController.mm");
    17.             string content = File.ReadAllText(filePath);
    18.  
    19.             // Check if this mod has already been applied -> if we have "append" the build.
    20.             Regex regex = new Regex("applicationDidEnterBackground\\(\\).+?}", RegexOptions.Singleline);
    21.             Match match = regex.Match(content);
    22.  
    23.             if (!match.Success)
    24.             {
    25.                 Debug.LogError("Failed to parse UnityAppController.mm");
    26.                 return;
    27.             }
    28.  
    29.             int numberOfLinesInMethod = match.Value.Split("\n").Length;
    30.  
    31.             if (numberOfLinesInMethod > 6)
    32.             {
    33.                 Debug.Log("Skipped modification to UnityAppController.mm as applicationDidEnterBackground() had more content than expected.");
    34.                 return;
    35.             }
    36.  
    37.             content = content
    38.                 .Replace("applicationWillResignActive", "###temp###")
    39.                 .Replace("applicationDidEnterBackground", "applicationWillResignActive")
    40.                 .Replace("###temp###", "applicationDidEnterBackground");
    41.  
    42.             File.WriteAllText(filePath, content);
    43.             Debug.Log("Moved iOS pause functionality from applicationWillResignActive to applicationDidEnterBackground.");
    44.         }
    45.     }