iOS 11 how to avoid conflicts with system gestures at screen edges?

Apple has changed the way they handle gestures at the screen edges in iOS 11. Hiding the status bar in iOS 11 no longer causes the system to guess that you want to defer the system gestures. To keep the same behavior we need to override preferredScreenEdgesDeferringSystemGestures in our view controller.
source: Avoiding Conflicts with System Gestures at Screen Edges

I can solve this by modifying UnityViewControllerBaseiOS.mm in the generated xcode project.

  • (UIRectEdge)preferredScreenEdgesDeferringSystemGestures{
    return UIRectEdgeAll;
    }

I want to solve this “inside unity” so other team members also get the fix and when re-export xcode project. How to do this?

solved it myself,

put this file in /Assets/Editor/DeferringSystemGestures.cs

using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.Collections;
#if UNITY_IOS
using UnityEditor.iOS.Xcode;
#endif
using System.IO;

// Avoiding Conflicts with System Gestures at Screen Edges

public class DeferringSystemGestures
{
#if UNITY_CLOUD_BUILD
// This method is added in the Advanced Features Settings on UCB
// PostBuildProcessor.OnPostprocessBuildiOS
public static void OnPostprocessBuildiOS (string exportPath)
{
Debug.Log(“[UCB] OnPostprocessBuildiOS”);
ProcessPostBuild(BuildTarget.iPhone,exportPath);
}
#endif

[PostProcessBuild]
public static void OnPostprocessBuild (BuildTarget buildTarget, string path)
{
#if !UNITY_CLOUD_BUILD
Debug.Log (“[iOS] OnPostprocessBuild”);
ProcessPostBuild (buildTarget, path);
#endif
}

public static void ProcessPostBuild(BuildTarget buildTarget, string path)
{
#if UNITY_IOS

if (buildTarget == BuildTarget.iOS) {

Debug.Log (“[iOS] OnPostprocessBuild - DeferringSystemGestures”);

string filePath = path + “/Classes/UI/UnityViewControllerBaseiOS.h”;
string[ ] lines = System.IO.File.ReadAllLines(filePath);
string allString = “”;
for(int i=0; i<lines.Length; i++) {
if(i == 14){
allString += “- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures;\n”;
}
allString += lines*+“\n”;*
}
System.IO.File.WriteAllText(filePath, allString);
filePath = path + “/Classes/UI/UnityViewControllerBaseiOS.mm”;
lines = System.IO.File.ReadAllLines(filePath);
allString = “”;
for(int i=0; i<lines.Length; i++) {
if(i == 39){
allString += “- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures{\n return UIRectEdgeAll;\n}\n\n”;
}
allString += lines*+“\n”;*
}
System.IO.File.WriteAllText(filePath, allString);
}
#endif
}
}

The above code has some typos, the two lines with:

allString += lines*+“\n”;*

should be:

allString += lines_+“\n”;_

Correction for typo should be:
allString += lines [ i ]+“\n”;

I’ve updated this method:

using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.Collections;
#if UNITY_IOS
using UnityEditor.iOS.Xcode;
#endif
using System.IO;

// https://useyourloaf.com/blog/avoiding-conflicts-with-system-gestures-at-screen-edges/


public class DeferringSystemGestures
{
#if UNITY_CLOUD_BUILD
    // This method is added in the Advanced Features Settings on UCB
    // PostBuildProcessor.OnPostprocessBuildiOS
    public static void OnPostprocessBuildiOS (string exportPath)
    {
        Debug.Log("[UCB] OnPostprocessBuildiOS");
        ProcessPostBuild(BuildTarget.iPhone,exportPath);
    }
#endif

    [PostProcessBuild]
    public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
    {
#if !UNITY_CLOUD_BUILD
        Debug.Log("[iOS] OnPostprocessBuild");
        ProcessPostBuild(buildTarget, path);
#endif
    }

    public static void ProcessPostBuild(BuildTarget buildTarget, string path)
    {
#if UNITY_IOS
        if (buildTarget == BuildTarget.iOS)
        {
            var placement = "- (BOOL)prefersStatusBarHidden";
            #if UNITY_2017_2_OR_NEWER
            var fileBase = "/Classes/UI/UnityViewControllerBase+iOS";
            #else
            var fileBase = "/Classes/UI/UnityViewControllerBaseiOS";
            #endif

            AddToFile(
                path: path + fileBase + ".h",
                location: placement,
                text: "- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures;");

            AddToFile(
                path: path + fileBase + ".mm",
                location: placement,
                text: "- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures\n{\n return UIRectEdgeAll;\n}\n");
        }
#endif
    }

    static void AddToFile(string path, string text, string location)
    {
        string content = System.IO.File.ReadAllText(path);

        if (!content.Contains(text)) {
            content = content.Replace(location, text + "\n" + location);
        }
        System.IO.File.WriteAllText(path, content);
    }
}

Does “setNeedsUpdateOfScreenEdgesDeferringSystemGestures” need to be called, as indicated in the Use Your Loaf article?