Search Unity

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

Discussion in 'iOS and tvOS' started by Jonathan-FourFats, Sep 15, 2017.

  1. Jonathan-FourFats

    Jonathan-FourFats

    Joined:
    Apr 21, 2016
    Posts:
    22
    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: https://useyourloaf.com/blog/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?
     
  2. Jonathan-FourFats

    Jonathan-FourFats

    Joined:
    Apr 21, 2016
    Posts:
    22
    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;

    // 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) {

    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
    }
    }
     
  3. Mischawake

    Mischawake

    Joined:
    Dec 13, 2012
    Posts:
    3
    The above code has some typos, the two lines with:

    allString += lines+"\n";

    should be:

    allString += lines+"\n";
     
  4. keijoh

    keijoh

    Joined:
    Jan 25, 2017
    Posts:
    1
    Correction for typo should be:
    allString += lines [ i ]+"\n";
     
  5. jvlppm_pf

    jvlppm_pf

    Joined:
    Apr 24, 2016
    Posts:
    6
    I've updated this method:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEditor.Callbacks;
    4. using System.Collections;
    5. #if UNITY_IOS
    6. using UnityEditor.iOS.Xcode;
    7. #endif
    8. using System.IO;
    9.  
    10. // https://useyourloaf.com/blog/avoiding-conflicts-with-system-gestures-at-screen-edges/
    11.  
    12.  
    13. public class DeferringSystemGestures
    14. {
    15. #if UNITY_CLOUD_BUILD
    16.     // This method is added in the Advanced Features Settings on UCB
    17.     // PostBuildProcessor.OnPostprocessBuildiOS
    18.     public static void OnPostprocessBuildiOS (string exportPath)
    19.     {
    20.         Debug.Log("[UCB] OnPostprocessBuildiOS");
    21.         ProcessPostBuild(BuildTarget.iPhone,exportPath);
    22.     }
    23. #endif
    24.  
    25.     [PostProcessBuild]
    26.     public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
    27.     {
    28. #if !UNITY_CLOUD_BUILD
    29.         Debug.Log("[iOS] OnPostprocessBuild");
    30.         ProcessPostBuild(buildTarget, path);
    31. #endif
    32.     }
    33.  
    34.     public static void ProcessPostBuild(BuildTarget buildTarget, string path)
    35.     {
    36. #if UNITY_IOS
    37.         if (buildTarget == BuildTarget.iOS)
    38.         {
    39.             var placement = "- (BOOL)prefersStatusBarHidden";
    40.             #if UNITY_2017_2_OR_NEWER
    41.             var fileBase = "/Classes/UI/UnityViewControllerBase+iOS";
    42.             #else
    43.             var fileBase = "/Classes/UI/UnityViewControllerBaseiOS";
    44.             #endif
    45.  
    46.             AddToFile(
    47.                 path: path + fileBase + ".h",
    48.                 location: placement,
    49.                 text: "- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures;");
    50.  
    51.             AddToFile(
    52.                 path: path + fileBase + ".mm",
    53.                 location: placement,
    54.                 text: "- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures\n{\n return UIRectEdgeAll;\n}\n");
    55.         }
    56. #endif
    57.     }
    58.  
    59.     static void AddToFile(string path, string text, string location)
    60.     {
    61.         string content = System.IO.File.ReadAllText(path);
    62.  
    63.         if (!content.Contains(text)) {
    64.             content = content.Replace(location, text + "\n" + location);
    65.         }
    66.         System.IO.File.WriteAllText(path, content);
    67.     }
    68. }
    69.  
     
    Last edited: Oct 30, 2017
  6. BrainAndBrain

    BrainAndBrain

    Joined:
    Nov 27, 2014
    Posts:
    115