How to put iOS entitlements file in a Unity project

I will show you how to include iOS’s entitlements file in a Unity project and how to hand it over to Xcode project.
I did this research out of my own need.
I’m using this to automatically add “Associated Domains” to the generated Xcode project.

Like you and many other Unity users, I was somewhat confident in Unity’s ability to handle complex things.
So I tried putting a file which has .entitlements as it’s extension in a project
just to find out disappointing result that It has no effect.

Then I searched and scouted around for an easy aid for hours but with no success.

Only thing I could do was to create a script I need by myself.

Usage:

  1. Import EntitlementsPostProcess.unitypackage.
  2. Inside Xcode trim Capabilities and let Xcode create an entitlements file.
  3. Copy the entitlements file created into Unity project.
  4. Hook up your entitlements file within the inspector view of EntitlementsPostProcess.cs

Links to documents:

Tested with Unity 5.3.5f1 and Xcode 8.1
It should work with Unity 5.4 too.

EntitlementsPostProcess.cs

using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.Collections;
using System.IO;


public class EntitlementsPostProcess : ScriptableObject
{
    public DefaultAsset m_entitlementsFile;

    [PostProcessBuild]
    public static void OnPostProcess(BuildTarget buildTarget, string buildPath)
    {
        if (buildTarget != BuildTarget.iOS)
        {
            return;
        }

        var dummy = ScriptableObject.CreateInstance<EntitlementsPostProcess>();
        var file = dummy.m_entitlementsFile;
        ScriptableObject.DestroyImmediate(dummy);
        if (file == null)
        {
            return;
        }

        var proj_path = PBXProject.GetPBXProjectPath(buildPath);
        var proj = new PBXProject();
        proj.ReadFromFile(proj_path);

        // target_name = "Unity-iPhone"
        var target_name = PBXProject.GetUnityTargetName();
        var target_guid = proj.TargetGuidByName(target_name);
        var src = AssetDatabase.GetAssetPath(file);
        var file_name = Path.GetFileName(src);
        var dst = buildPath + "/" + target_name + "/" + file_name;
        FileUtil.CopyFileOrDirectory(src, dst);
        proj.AddFile(target_name + "/" + file_name, file_name);
        proj.AddBuildProperty(target_guid, "CODE_SIGN_ENTITLEMENTS", target_name + "/" + file_name);

        proj.WriteToFile(proj_path);
    }
}

2860052–209249–EntitlementsPostProcess.unitypackage (1.38 KB)

16 Likes

Yay thank you so much! I was worried I’d never stop scratching my head getting push notifications to work in Unity Cloud Build but this saved me!

1 Like

This worked brilliantly as per my post:

Thanks so much @a_p_u_r_o :smile:

1 Like

Many thanks @a_p_u_r_o .

1 Like

This is great, thanks @a_p_u_r_o

1 Like

Pinning this post since so many people find it useful!

1 Like

Perfect! Slove my problem to turn on push notifications automaticly!
Thanks so much @@a_p_u_r_o

1 Like

Great answer and solved my problem on enabling iCloud automactically!
Now I am wondering how can I enable IAP and GameCenter by default too? Since they doesn’t need entitlement file? (Now I still need to manually turn these 2 on)

2 Likes

As I consider this thread to consist of notions which are already a bit hard to understand,
I feel like mixing up no further complexity.
Therefore, @SweatyChair , I suggest you to create a new thread.
I am certain that your problem here will be treated in a totally different way.

I tried the instructions shared here by a_p_u_r_o; however when i upload the IPA file generated by cloud build via application loader i get several errors due to ERROR ITMS-90045 Invalid Code Signing Entitlements" on several keys e.g. UILuanchStoryBoardName, UIApplicationExitOnSuspend etc. Any suggestions? Thanks

Go to this thread https://forum.unity3d.com/threads/automatic-turn-on-in-app-purchase-capabilities-in-ios.474283/
I am also finding a solution.

1 Like

igot this error …please any idea … will pareciate so much
//
.

Thanks, that was just what the Doctor ordered :slight_smile:

One thing though: on my local setup, I found that the script would fail when appending to an existing project since the entitlements file was already there. Easy fix was to just delete it first:

Insert

FileUtil.DeleteFileOrDirectory(dst);

Before the FileUtil.Copy(…).

1 Like

Thank you. your work is great!!!

1 Like

There is also this method:

PBXProject.AddCapability(targetGuid,PBXCapabilityType,entitlementFileName,addOptionalFramework,framework)

Calling it with an empty PListDocument entitlements file didn’t add the capability though :?

Thats GREAT!
But, how can I get this .entitlements file? I opened the XCode and configure the capabilities, but the .entitlements file do not changed and just have this content.

<?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>aps-environment</key>
    <string>development</string>
</dict>
</plist>

How i change that file and make all the process?

how about unity officially integrate this in the player settings?

1 Like

i want add my audio files

FileUtil.CopyFileOrDirectory(Application.dataPath + “/chching.wav”, buildPath+“/”+target_name+“/chching.wav”);
proj.AddFile(target_name + “/chching.wav”,“/chching.wav”);

file is added but not working , when i drag and drop file in xcode its working.

can you help me with this

i want to add my custom sound for notification

FileUtil.CopyFileOrDirectory(Application.dataPath + “/chching.wav”, buildPath+“/”+target_name+“/chching.wav”);
proj.AddFile(target_name + “/chching.wav”,“/chching.wav”);

file is added but not working , when i drag and drop file in xcode its working.

can you help me with this

I don’t get the solution in that thread, it was closed and it seems likely people need to manually set up IAP and Game center flag inside xcode project rather writing postbuild ios.

I also tried this https://bitbucket.org/snippets/Eyeoness/eaong . However, it just doesn’t work. Unity shows log successfully writing the SystemCapabilities, however, when open file pbxproj, nothing changes. It seems likely somewhere overwrite my method.

Any advise to enable IAP and Game Center via post build process Unity ios.

1 Like