Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to put iOS entitlements file in a Unity project

Discussion in 'iOS and tvOS' started by a_p_u_r_o, Nov 22, 2016.

  1. a_p_u_r_o

    a_p_u_r_o

    Joined:
    May 27, 2015
    Posts:
    23
    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:
    https://docs.unity3d.com/ScriptReference/Callbacks.PostProcessBuildAttribute.html
    https://docs.unity3d.com/ScriptReference/iOS.Xcode.PBXProject.html

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

    EntitlementsPostProcess.png


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEditor.Callbacks;
    4. using UnityEditor.iOS.Xcode;
    5. using System.Collections;
    6. using System.IO;
    7.  
    8.  
    9. public class EntitlementsPostProcess : ScriptableObject
    10. {
    11.     public DefaultAsset m_entitlementsFile;
    12.  
    13.     [PostProcessBuild]
    14.     public static void OnPostProcess(BuildTarget buildTarget, string buildPath)
    15.     {
    16.         if (buildTarget != BuildTarget.iOS)
    17.         {
    18.             return;
    19.         }
    20.  
    21.         var dummy = ScriptableObject.CreateInstance<EntitlementsPostProcess>();
    22.         var file = dummy.m_entitlementsFile;
    23.         ScriptableObject.DestroyImmediate(dummy);
    24.         if (file == null)
    25.         {
    26.             return;
    27.         }
    28.  
    29.         var proj_path = PBXProject.GetPBXProjectPath(buildPath);
    30.         var proj = new PBXProject();
    31.         proj.ReadFromFile(proj_path);
    32.  
    33.         // target_name = "Unity-iPhone"
    34.         var target_name = PBXProject.GetUnityTargetName();
    35.         var target_guid = proj.TargetGuidByName(target_name);
    36.         var src = AssetDatabase.GetAssetPath(file);
    37.         var file_name = Path.GetFileName(src);
    38.         var dst = buildPath + "/" + target_name + "/" + file_name;
    39.         FileUtil.CopyFileOrDirectory(src, dst);
    40.         proj.AddFile(target_name + "/" + file_name, file_name);
    41.         proj.AddBuildProperty(target_guid, "CODE_SIGN_ENTITLEMENTS", target_name + "/" + file_name);
    42.  
    43.         proj.WriteToFile(proj_path);
    44.     }
    45. }
    46.  
     

    Attached Files:

    Last edited: Nov 22, 2016
  2. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    262
    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!
     
    a_p_u_r_o likes this.
  3. DrakenDev

    DrakenDev

    Joined:
    Jul 20, 2012
    Posts:
    11
  4. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    a_p_u_r_o likes this.
  5. SimteractiveDev

    SimteractiveDev

    Joined:
    Jul 9, 2014
    Posts:
    97
    a_p_u_r_o likes this.
  6. SophiaC

    SophiaC

    Joined:
    Sep 6, 2016
    Posts:
    238
    Pinning this post since so many people find it useful!
     
    a_p_u_r_o likes this.
  7. YorkYoung

    YorkYoung

    Joined:
    Nov 16, 2016
    Posts:
    4
    Perfect! Slove my problem to turn on push notifications automaticly!
    Thanks so much @@a_p_u_r_o
     
    a_p_u_r_o likes this.
  8. SweatyChair

    SweatyChair

    Joined:
    Feb 15, 2016
    Posts:
    140
    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)
     
    Last edited: May 29, 2017
    dttngan91 and YorkYoung like this.
  9. a_p_u_r_o

    a_p_u_r_o

    Joined:
    May 27, 2015
    Posts:
    23
    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.
     
  10. topXight

    topXight

    Joined:
    Feb 16, 2016
    Posts:
    38
    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
     
  11. YorkYoung

    YorkYoung

    Joined:
    Nov 16, 2016
    Posts:
    4
    SweatyChair likes this.
  12. fla-rar

    fla-rar

    Joined:
    May 1, 2014
    Posts:
    246
    igot this error ...please any idea .... will pareciate so much
    //
    .

    new error.jpg
     
  13. BoaNeo

    BoaNeo

    Joined:
    Feb 21, 2013
    Posts:
    56
    Thanks, that was just what the Doctor ordered :)

    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(...).
     
    a_p_u_r_o likes this.
  14. TigerFish89

    TigerFish89

    Joined:
    Jan 6, 2016
    Posts:
    3
    Thank you. your work is great!!!
     
    Last edited: Sep 13, 2017
    a_p_u_r_o likes this.
  15. MrMatthias

    MrMatthias

    Joined:
    Sep 18, 2012
    Posts:
    191
    There is also this method:
    Code (CSharp):
    1. PBXProject.AddCapability(targetGuid,PBXCapabilityType,entitlementFileName,addOptionalFramework,framework)
    Calling it with an empty PListDocument entitlements file didn't add the capability though :?
     
  16. ribeiroferreiralucas

    ribeiroferreiralucas

    Joined:
    Aug 29, 2017
    Posts:
    1
    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.

    Code (CSharp):
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    3. <plist version="1.0">
    4. <dict>
    5.     <key>aps-environment</key>
    6.     <string>development</string>
    7. </dict>
    8. </plist>
    9.  
    How i change that file and make all the process?
     
  17. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,521
    how about unity officially integrate this in the player settings?
     
    vmessina likes this.
  18. WasimBashir

    WasimBashir

    Joined:
    Nov 24, 2017
    Posts:
    9
    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
     
  19. WasimBashir

    WasimBashir

    Joined:
    Nov 24, 2017
    Posts:
    9
    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
     
  20. dttngan91

    dttngan91

    Joined:
    Nov 21, 2013
    Posts:
    80
    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.
     
    SweatyChair likes this.
  21. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    1,064
    We are using Unity cloud build for our ios app. Is there any way to enable to add basic capabilities like IAP and GameCenter in Unity 5.6? Or we are forced to switch to Unity 2017 / 2018 in order to make this work?
     
  22. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    1,064
  23. bevangelisti

    bevangelisti

    Joined:
    Feb 16, 2018
    Posts:
    3
  24. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,158
    Are there anyway I could use this API in unity 5.6.6?
     
  25. MrMatthias

    MrMatthias

    Joined:
    Sep 18, 2012
    Posts:
    191
    the module should be included since Unity 5.0, or was the method added later? If so you can load the latest version of the xcode api https://bitbucket.org/Unity-Technologies/xcodeapi/overview (it uses the namespace UnityEditor.iOS.Xcode.Custom, so it doesn't conflict with the bundled version)
     
    Thaina likes this.
  26. squarelover

    squarelover

    Joined:
    Nov 14, 2012
    Posts:
    31
    it do not add this capability which is bad because actually it looks like it should
     
  27. squarelover

    squarelover

    Joined:
    Nov 14, 2012
    Posts:
    31
    var projectcapabilitymanager = new ProjectCapabilityManager(projPath,dst,"Unity-iPhone");
    projectcapabilitymanager.AddInAppPurchase();
    doesnt works also
     
  28. squarelover

    squarelover

    Joined:
    Nov 14, 2012
    Posts:
    31
    alright adding StoreKit.framewokr automatically adds the Capability to an xcode (or with a conjunction with those lines above dont wont to test that separately really)
    proj.AddFrameworkToProject(target_guid, "StoreKit.framework", false);
     
  29. rahulsm56

    rahulsm56

    Joined:
    May 14, 2019
    Posts:
    2
    One of the most helpful threads here , thanks @a_p_u_r_o !
     
  30. modernator24

    modernator24

    Joined:
    Apr 7, 2017
    Posts:
    186
    It throws exception on build:

    Code (CSharp):
    1. IOException: Failed to Copy File / Directory from 'Assets/Sample.entitlements' to '/Users/rico345100/Desktop/development/unity/Stick-Warfare-Blood-Strike-iOS/Build/SWBS/9D25AB9C213FB47800354C27/Sample.entitlements'.
    2. UnityEditor.FileUtil.CopyFileOrDirectory (System.String source, System.String dest) (at /Users/builduser/buildslave/unity/build/Editor/Mono/FileUtil.bindings.cs:35)
    3. EntitlementsPostProcess.OnPostProcess (UnityEditor.BuildTarget buildTarget, System.String buildPath) (at Assets/Editor/EntitlementsPostProcess.cs:39)
    4. System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <fb001e01371b4adca20013e0ac763896>:0)
    5. Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    6. System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <fb001e01371b4adca20013e0ac763896>:0)
    7. System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <fb001e01371b4adca20013e0ac763896>:0)
    8. UnityEditor.Build.BuildPipelineInterfaces+AttributeCallbackWrapper.OnPostprocessBuild (UnityEditor.Build.Reporting.BuildReport report) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/BuildPipelineInterfaces.cs:177)
    9. UnityEditor.Build.BuildPipelineInterfaces+<>c__DisplayClass17_0.<OnBuildPostProcess>b__1 (UnityEditor.Build.IPostprocessBuildWithReport bpp) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/BuildPipelineInterfaces.cs:433)
    10. UnityEditor.Build.BuildPipelineInterfaces.InvokeCallbackInterfacesPair[T1,T2] (System.Collections.Generic.List`1[T] oneInterfaces, System.Action`1[T] invocationOne, System.Collections.Generic.List`1[T] twoInterfaces, System.Action`1[T] invocationTwo, System.Boolean exitOnFailure) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/BuildPipelineInterfaces.cs:391)
    11. UnityEditor.EditorApplication:Internal_CallGlobalEventHandler() (at /Users/builduser/buildslave/unity/build/Editor/Mono/EditorApplication.cs:442)
    12.