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. Dismiss Notice

Discussion How to port workaround for iOS crash on 15.7? (SKStoreProductViewController sceneDisconnected)

Discussion in 'iOS and tvOS' started by roointan, Dec 16, 2022.

  1. roointan

    roointan

    Joined:
    Jan 8, 2018
    Posts:
    78
    We are observing lots of crashes on iOS 15.7 for our iOS game. Something iOS is responsible for, which is fixed in iOS 16.

    More info: https://developer.apple.com/forums/thread/714464

    People have suggested workarounds for this problem, But can anyone help me migrate this to Unity?
    I really prefer this to be done from inside Unity, and not after exporting the Xcode project and modifying manually; Either by doing something in IPostprocessBuildWithReport, or any other possible way.
    (though how to do it manually is also needed, if this can't be automated)

    Available workarounds:
    https://stackoverflow.com/a/73796906/1235063, https://stackoverflow.com/a/74232140/1235063
     
    Tommy-Angelo likes this.
  2. roointan

    roointan

    Joined:
    Jan 8, 2018
    Posts:
    78
    Can anyone help with this?
     
    Tommy-Angelo likes this.
  3. roointan

    roointan

    Joined:
    Jan 8, 2018
    Posts:
    78
    OK, I had to do it myself.

    Call this method from an IPostprocessBuildWithReport in OnPostprocessBuild() and pass it report.summary.outputPath and all will be set:

    Code (CSharp):
    1. //Fix for a bug that Apple introduced in iOS 15.7 that leads to crashes
    2.     //More info: https://developer.apple.com/forums/thread/714464 , workarounds: https://stackoverflow.com/a/73796906/1235063 , https://stackoverflow.com/questions/72907240/skstoreproductviewcontroller-crashes-for-unrecognized-selector-named-scenediscon/74232140#74232140
    3.     //Expections on Xcode project:
    4.     //SWIFT_OBJC_BRIDGING_HEADER must be set (only for Unity-iPhone target)
    5.     //CLANG_ENABLE_MODULES set to true, for Unity-iPhone, Unity-iPhone Tests, and UnityFramework
    6.     //SWIFT_VERSION for the 3 targets must be specified
    7.     private void AddWorkaroundForCrashIniOS15_7(string pathToBuiltProject)
    8.     {
    9.         string projectPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
    10.         PBXProject project = new PBXProject();
    11.         project.ReadFromString(File.ReadAllText(projectPath));
    12.  
    13.         string swiftFileName = "CrashPrevIOS15_7.swift";
    14.         string swiftFileContent = "//\n//  File.swift\n//  Unity-iPhone\n//\n//  Created by Paiman Roointan on 1/18/23.\n//\n\nimport Foundation\nimport StoreKit\n\n@available(iOS, introduced: 15.7, obsoleted: 16.0)\n@objc extension SKStoreProductViewController {\n    func sceneDisconnected(_ arg: AnyObject) {}\n    func appWillTerminate() {}\n}\n";
    15.         string swiftFilePath = Path.Combine(pathToBuiltProject, swiftFileName);
    16.  
    17.         if (!File.Exists(swiftFilePath))
    18.             File.WriteAllText(swiftFilePath, swiftFileContent);
    19.  
    20.         project.AddFileToBuild(
    21.                     project.GetUnityMainTargetGuid(),
    22.                     project.AddFile(swiftFileName, swiftFileName));
    23.  
    24.         string bridgingHeaderFileName = "Unity-iPhone-Bridging-Header.h";
    25.         string bridgingHeaderFileContent = "//\n//  Use this file to import your target's public headers that you would like to expose to Swift.\n//";
    26.         string bridgingHeaderFilePath = Path.Combine(pathToBuiltProject, bridgingHeaderFileName);
    27.  
    28.         if (!File.Exists(bridgingHeaderFilePath))
    29.             File.WriteAllText(bridgingHeaderFilePath, bridgingHeaderFileContent);
    30.  
    31.         if (string.IsNullOrEmpty(project.GetBuildPropertyForConfig(project.GetUnityMainTargetGuid(), "SWIFT_VERSION")))
    32.             project.SetBuildProperty(project.GetUnityMainTargetGuid(), "SWIFT_VERSION", "5.0");
    33.  
    34.         if (string.IsNullOrEmpty(project.GetBuildPropertyForConfig(project.GetUnityFrameworkTargetGuid(), "SWIFT_VERSION")))
    35.             project.SetBuildProperty(project.GetUnityFrameworkTargetGuid(), "SWIFT_VERSION", "5.0");
    36.  
    37.         if (string.IsNullOrEmpty(project.GetBuildPropertyForConfig(project.TargetGuidByName("Unity-iPhone Tests"), "SWIFT_VERSION")))
    38.             project.SetBuildProperty(project.TargetGuidByName("Unity-iPhone Tests"), "SWIFT_VERSION", "5.0");
    39.  
    40.         project.SetBuildProperty(project.GetUnityMainTargetGuid(), "CLANG_ENABLE_MODULES", "YES");
    41.         project.SetBuildProperty(project.GetUnityFrameworkTargetGuid(), "CLANG_ENABLE_MODULES", "YES");
    42.         project.SetBuildProperty(project.TargetGuidByName("Unity-iPhone Tests"), "CLANG_ENABLE_MODULES", "YES");
    43.  
    44.         project.SetBuildProperty(project.GetUnityMainTargetGuid(), "SWIFT_OBJC_BRIDGING_HEADER", bridgingHeaderFileName);
    45.  
    46.         File.WriteAllText(projectPath, project.WriteToString());
     
    ilievant and Tommy-Angelo like this.