Skip to content

Instantly share code, notes, and snippets.

@camnewnham
Last active October 17, 2023 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camnewnham/3266c547ed733ceb99df3f379e289509 to your computer and use it in GitHub Desktop.
Save camnewnham/3266c547ed733ceb99df3f379e289509 to your computer and use it in GitHub Desktop.
Run Unity iOS app behind control center
// Belongs in an Assets/**/Editor folder
public class iOSPostProcessor
{
/// <summary>
/// Post processor to automate skipping the checkbox on testflight for encryption compliance.
/// </summary>
[PostProcessBuild(-999)]
public static void OnPostProcessBuildRunInBackground(BuildTarget buildTarget, string path)
{
if (buildTarget != BuildTarget.iOS)
{
return;
}
string filePath = Path.Combine(path, "Classes", "UnityAppController.mm");
string content = File.ReadAllText(filePath);
// Check if this mod has already been applied -> if we have "append" the build.
Regex regex = new Regex("applicationDidEnterBackground\\(\\).+?}", RegexOptions.Singleline);
Match match = regex.Match(content);
if (!match.Success)
{
Debug.LogError("Failed to parse UnityAppController.mm");
return;
}
int numberOfLinesInMethod = match.Value.Split("\n").Length;
if (numberOfLinesInMethod > 6)
{
Debug.Log("Skipped modification to UnityAppController.mm as applicationDidEnterBackground() had more content than expected.");
return;
}
content = content
.Replace("applicationWillResignActive", "###temp###")
.Replace("applicationDidEnterBackground", "applicationWillResignActive")
.Replace("###temp###", "applicationDidEnterBackground");
File.WriteAllText(filePath, content);
Debug.Log("Moved iOS pause functionality from applicationWillResignActive to applicationDidEnterBackground.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment