Search Unity

Question Automatic Turn on Near Field Communication Capability & Entitlements in Unity Package for iOS

Discussion in 'iOS and tvOS' started by vincent_siip, Feb 14, 2024.

  1. vincent_siip

    vincent_siip

    Joined:
    Jan 29, 2024
    Posts:
    2
    Hi,

    I am creating a Unity package that uses NFC. For iOS I need to add the Near Field Communication capability, but unfortunately, the ProjectCapabilityManager doesn't have a method to add the NFC capability. How can I add the capability and having the entitlements look like this?


    <?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.nfc.readersession.formats</key>
    <array>
    <string>TAG</string>
    </array>
    </dict>
    </plist>


    Thank you
     
  2. vincent_siip

    vincent_siip

    Joined:
    Jan 29, 2024
    Posts:
    2
    I just fixed it :)

    Code (CSharp):
    1. using System.IO;
    2. using UnityEditor;
    3. using UnityEditor.Callbacks;
    4. using UnityEditor.iOS.Xcode;
    5. using UnityEngine;
    6.  
    7. public class PostXcodeBuild
    8. {
    9.     [PostProcessBuild]
    10.     public static void SetXcodePlist(BuildTarget buildTarget, string pathToBuiltProject)
    11.     {
    12.         if (buildTarget != BuildTarget.iOS) return;
    13.  
    14.         var plistPath = pathToBuiltProject + "/Info.plist";
    15.         var plist = new PlistDocument();
    16.         plist.ReadFromString(File.ReadAllText(plistPath));
    17.  
    18.         var rootDict = plist.root;
    19.         rootDict.SetString("NSCameraUsageDescription", "Requiring camera to scan your document");
    20.         rootDict.SetString("NFCReaderUsageDescription", "Requiring nfc to scan your document");
    21.  
    22.         PlistElementArray nfcIdentifiers = rootDict.CreateArray("com.apple.developer.nfc.readersession.iso7816.select-identifiers");
    23.         nfcIdentifiers.AddString("A00000045645444C2D3031");
    24.         nfcIdentifiers.AddString("A0000002471001");
    25.  
    26.         File.WriteAllText(plistPath, plist.WriteToString());
    27.  
    28.         PostProcessEntitlements(pathToBuiltProject);
    29.     }
    30.  
    31.  
    32.     private static void PostProcessEntitlements(string pathToBuiltProject)
    33.     {
    34.  
    35.  
    36. #if UNITY_IOS
    37.         // load project
    38.         string projectPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
    39.         var project = new PBXProject();
    40.         project.ReadFromFile(projectPath);
    41.         string targetGuid = project.GetUnityMainTargetGuid();
    42.  
    43.         // create or modify the entitlements plist
    44.         PlistDocument plist = new PlistDocument();
    45.         string plistFilePath = project.GetEntitlementFilePathForTarget(targetGuid);
    46.         string plistFileName;
    47.         bool addEntitlementFile = false;
    48.  
    49.         // if we don't have an entitlements file already...
    50.         if (string.IsNullOrEmpty(plistFilePath))
    51.         {
    52.             // ...get a path for a to create a new one.
    53.             plistFileName = $"{Application.productName}.entitlements";
    54.             plistFilePath = Path.Combine(pathToBuiltProject, plistFileName);
    55.             addEntitlementFile = true;
    56.             plist.Create();
    57.         }
    58.         else
    59.         {
    60.             // ...just snag the basename from the path.
    61.             plistFileName = Path.GetFileName(plistFilePath);
    62.             plist.ReadFromFile(plistFilePath);
    63.         }
    64.  
    65.         // modify the plist
    66.         PlistElementDict root = plist.root;
    67.         PlistElementArray nfcFormats = root.CreateArray("com.apple.developer.nfc.readersession.formats");
    68.         nfcFormats.AddString("TAG");
    69.  
    70.         // save the modified plist
    71.         plist.WriteToFile(plistFilePath);
    72.         Debug.Log($"Wrote Entitlements plist to {plistFilePath}");
    73.  
    74.         if (addEntitlementFile)
    75.         {
    76.             // add entitlements plist to project
    77.             project.AddFile(plistFilePath, plistFileName);
    78.             project.AddBuildProperty(targetGuid, "CODE_SIGN_ENTITLEMENTS", plistFilePath);
    79.             project.WriteToFile(projectPath);
    80.             Debug.Log($"Added Entitlements plist to project (target: {targetGuid})");
    81.         }
    82. #endif // UNITY_IOS
    83.     }
    84. }
    85.