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

[PostProcessBuild] UnauthorizedAccessException when trying to add AppGroups capability

Discussion in 'iOS and tvOS' started by JeanPierre_Bailly, Dec 1, 2017.

  1. JeanPierre_Bailly

    JeanPierre_Bailly

    Joined:
    Nov 2, 2015
    Posts:
    10
    I'm using the iOS AppGroups capability on my project, and I'm trying to automatize my Xcode projects settings with a PostProcessBuild method.

    The PBXProject's method "AddCapability" works well, but it is useless because it can't set the group name used by my app.

    I tried the ProjectCapabilityManager class, but I got some trouble with it.
    By executing the following code in my PostProcessBuild method, I get a "UnauthorizedAccessException" on the xcodeproj file/folder

    Code (CSharp):
    1. ProjectCapabilityManager proCapabilitiesManager = new ProjectCapabilityManager(pathToBuiltProject + "/Unity-iPhone.xcodeproj", "iphone.entitlement", "Unity-iPhone");
    2. proCapabilitiesManager.AddAppGroups(new [] {"my.group.id"});
    3. proCapabilitiesManager.WriteToFile();
    Do you have any idea what I can have done wrong ?
     
  2. JeanPierre_Bailly

    JeanPierre_Bailly

    Joined:
    Nov 2, 2015
    Posts:
    10
    By the way, I'm using Unity 2017.2.0f3
     
  3. Matti-Jokipii

    Matti-Jokipii

    Joined:
    Apr 24, 2015
    Posts:
    16
    I have this too. BTW based on the documantation
    https://docs.unity3d.com/2017.1/Doc.../iOS.Xcode.ProjectCapabilityManager-ctor.html
    your not supposed to append the "/Unity-iPhone.xcodeproj/project.pbxproj" part to the path, but it looks like you must.

    The problem seems to be in that the manager can't handle entitlement file name with the "Unity-iPhone" path component. If you remove that, the build succeeds, but then the project file contains incorrect reference to a nonexisting file at Unity-iPhone directory.
     
    Last edited: Mar 23, 2018
  4. RagnarZero

    RagnarZero

    Joined:
    Apr 1, 2013
    Posts:
    1
    I had the same problem and this seems to at least work partially (game center capability doesn't get added, the others do):

    Code (CSharp):
    1. public void OnPostprocessBuild(BuildTarget target, string path)
    2.     {
    3.         if (target != BuildTarget.iOS) return;
    4.         string pbxPath = PBXProject.GetPBXProjectPath(path);
    5.         var capManager = new ProjectCapabilityManager(pbxPath, "ios.entitlements", "Unity-iPhone");
    6.         capManager.AddGameCenter();
    7.         capManager.AddInAppPurchase();
    8.         capManager.AddKeychainSharing(new string[] { "$(AppIdentifierPrefix)com.my.appid" });
    9.         capManager.AddPushNotifications(false);
    10.         capManager.WriteToFile();
    11.     }
     
  5. pistoleta

    pistoleta

    Joined:
    Sep 14, 2017
    Posts:
    536
    Hello there, using Unity 2019.2.20f1 here.

    Im trying to do 2 simple things in the OnPostprocessBuild event:
    1st: - Add SignInWithApple capability to the Xcode project.
    2nd: - Set AuthenticationServices.framework as optional.

    Can't do any of those, I tried with the example in the thread and it doesn't work, doesn't throw any exception either.

    Also tried with this script I found on Github:

    Code (CSharp):
    1. //APPLE SIGN IN
    2.  
    3.         var projectPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
    4.     #if UNITY_2019_3_OR_NEWER
    5.             var project = new PBXProject();
    6.             project.ReadFromString(System.IO.File.ReadAllText(projectPath));
    7.             var manager = new ProjectCapabilityManager(projectPath, "Entitlements.entitlements", targetGuid: project.GetUnityMainTargetGuid());
    8. #else
    9.         var manager = new ProjectCapabilityManager(projectPath, "Entitlements.entitlements", PBXProject.GetUnityTargetName());
    10. #endif
    11.         manager.AddSignInWithApple();
    12.         manager.WriteToFile();
    And doesn't work either.

    Also found this code for the framework modification:

    Code (CSharp):
    1. PBXProject proj = new PBXProject();
    2.         string pbxFilename = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
    3.         proj.ReadFromFile (pbxFilename);
    4.         string targetName = PBXProject.GetUnityTargetName ();
    5.         string guid = proj.TargetGuidByName (targetName);
    6.         proj.AddFrameworkToProject(guid, "AuthenticationServices.framework", true);
    Where true/false is supposed to be if you want it optional or not.

    Tried also this for removing the existing AuthenticationServices framework:
    Code (CSharp):
    1. proj.RemoveFrameworkFromProject("AuthenticationServices.framework",);
    But didnt work either.

    Anybody ? Im starting to get desperate :confused:
    Thanks
     
    Last edited: Feb 6, 2020
  6. Ersin

    Ersin

    Joined:
    Oct 25, 2012
    Posts:
    13
    This is the solution for Unity 2018.4.22f1.

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.Build;
    3. using UnityEditor.Build.Reporting;
    4. using UnityEditor.iOS.Xcode;
    5.  
    6. public class CapabilityAdder : IPostprocessBuildWithReport
    7. {
    8.     public int callbackOrder => 0;
    9.  
    10.     public void OnPostprocessBuild(BuildReport report)
    11.     {
    12.         BuildTarget buildTarget = report.summary.platform;
    13.         string path = report.summary.outputPath;
    14.  
    15.         if (buildTarget != BuildTarget.iOS)
    16.             return;
    17.      
    18.         string projPath = PBXProject.GetPBXProjectPath(path);
    19.  
    20.         PBXProject proj = new PBXProject();
    21.         proj.ReadFromFile(projPath);
    22.  
    23.         ProjectCapabilityManager manager = new ProjectCapabilityManager(
    24.             projPath,
    25.             "Entitlements.entitlements",
    26.             PBXProject.GetUnityTargetName()
    27.         );
    28.  
    29.         manager.AddInAppPurchase();
    30.         manager.AddPushNotifications(false);
    31.  
    32.         manager.WriteToFile();
    33.     }
    34. }
     
  7. pistoleta

    pistoleta

    Joined:
    Sep 14, 2017
    Posts:
    536
    Can't add the AddKeychainSharing no matter which of the approach I try, I'm using Unity 2121.3.23f1 , anybody having the same problem?
     
  8. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,602
    ProjectCapabilityManager is implemented using AddCapability so the api should suffice (again - i might be missing something)
    the implementation is like this
    Code (CSharp):
    1. public void AddAppGroups(string[] groups)
    2.         {
    3.             var arr = (GetOrCreateEntitlementDoc().root[AppGroupsEntitlements.Key] = new PlistElementArray()) as PlistElementArray;
    4.             for (var i = 0; i < groups.Length; i++)
    5.             {
    6.                 arr.values.Add(new PlistElementString(groups[i]));
    7.             }
    8.  
    9.             project.AddCapability(m_TargetGuid, PBXCapabilityType.AppGroups, m_EntitlementFilePath);
    10.         }
    Can you bug report with small repro project?