Search Unity

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:
    17
    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:
    249
    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,477
    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

    Unity Technologies

    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:
    3
    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:
    123
    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:
    17
    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:
    21
    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:
    3
    SweatyChair likes this.
  12. fla-rar

    fla-rar

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

    new error.jpg
     
  13. BoaNeo

    BoaNeo

    Joined:
    Feb 21, 2013
    Posts:
    36
    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:
    177
    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:
    2,411
    how about unity officially integrate this in the player settings?
     
  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:
    79
    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:
    822
    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:
    822
  23. bevangelisti

    bevangelisti

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

    Thaina

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

    MrMatthias

    Joined:
    Sep 18, 2012
    Posts:
    177
    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.
unityunity