UnityiOSBridgeEssentials icon indicating copy to clipboard operation
UnityiOSBridgeEssentials copied to clipboard

How to add entitlements ?

Open rahulnaik906 opened this issue 2 years ago • 1 comments

How and where do i add entitlements to the unity project ?

rahulnaik906 avatar Jun 28 '23 12:06 rahulnaik906

I know this response, is late for you, but maybe it helps someone looking for this in future.

Entitlements can be added in your xcode project after unity builds. But this process can be simplified and added as part of the build process and the entitlements will be added automatically.

Create a C# script in Assets/Editor, name it iOSPostProcessBuild and copy the below into it:

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

public class iOSPostProcessBuild
{
#if UNITY_IOS
	[PostProcessBuild(999)]
	public static void OnPostProcessBuild(BuildTarget target, string path)
	{
		if (target == BuildTarget.iOS)
		{
			// Define the path to the Xcode project
			string projPath = PBXProject.GetPBXProjectPath(path);
			PBXProject proj = new PBXProject();
			proj.ReadFromFile(projPath);

			// Get the target GUID
			string targetGUID = proj.GetUnityMainTargetGuid();

			// Add the iCloud capability to the Xcode project
			proj.AddCapability(targetGUID, PBXCapabilityType.iCloud);

			// Add the entitlements file to the project
			string entitlementsFileName = "YourGameName.entitlements";
			string entitlementsFilePath = Path.Combine(path, entitlementsFileName);
			File.WriteAllText(entitlementsFilePath, GetEntitlementsContent());

			// Set the path to the entitlements file in the Xcode project
			proj.AddFile(entitlementsFilePath, entitlementsFileName);
			proj.SetBuildProperty(targetGUID, "CODE_SIGN_ENTITLEMENTS", entitlementsFileName);

			// Write the changes back to the Xcode project
			proj.WriteToFile(projPath);
		}
	}

	private static string GetEntitlementsContent()
	{
		return @"<?xml version=""1.0"" encoding=""UTF-8""?>
		<!DOCTYPE plist PUBLIC ""-//Apple//DTD PLIST 1.0//EN"" ""http://www.apple.com/DTDs/PropertyList-1.0.dtd"">
		<plist version=""1.0"">
		<dict>
			<key>com.apple.developer.icloud-container-identifiers</key>
			<array/>
			<key>com.apple.developer.ubiquity-kvstore-identifier</key>
			<string>$(TeamIdentifierPrefix)$(CFBundleIdentifier)</string>
		</dict>
		</plist>";
	}
	
#endif
}

change YourGameName to the name of your game. This wont break the code if you forget to change it. It's up to you... :)

farazk86 avatar Oct 17 '24 11:10 farazk86