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

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

I just fixed it :slight_smile:

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

public class PostXcodeBuild
{
    [PostProcessBuild]
    public static void SetXcodePlist(BuildTarget buildTarget, string pathToBuiltProject)
    {
        if (buildTarget != BuildTarget.iOS) return;

        var plistPath = pathToBuiltProject + "/Info.plist";
        var plist = new PlistDocument();
        plist.ReadFromString(File.ReadAllText(plistPath));

        var rootDict = plist.root;
        rootDict.SetString("NSCameraUsageDescription", "Requiring camera to scan your document");
        rootDict.SetString("NFCReaderUsageDescription", "Requiring nfc to scan your document");

        PlistElementArray nfcIdentifiers = rootDict.CreateArray("com.apple.developer.nfc.readersession.iso7816.select-identifiers");
        nfcIdentifiers.AddString("A00000045645444C2D3031");
        nfcIdentifiers.AddString("A0000002471001");

        File.WriteAllText(plistPath, plist.WriteToString());

        PostProcessEntitlements(pathToBuiltProject);
    }


    private static void PostProcessEntitlements(string pathToBuiltProject)
    {


#if UNITY_IOS
        // load project
        string projectPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
        var project = new PBXProject();
        project.ReadFromFile(projectPath);
        string targetGuid = project.GetUnityMainTargetGuid();

        // create or modify the entitlements plist
        PlistDocument plist = new PlistDocument();
        string plistFilePath = project.GetEntitlementFilePathForTarget(targetGuid);
        string plistFileName;
        bool addEntitlementFile = false;

        // if we don't have an entitlements file already...
        if (string.IsNullOrEmpty(plistFilePath))
        {
            // ...get a path for a to create a new one.
            plistFileName = $"{Application.productName}.entitlements";
            plistFilePath = Path.Combine(pathToBuiltProject, plistFileName);
            addEntitlementFile = true;
            plist.Create();
        }
        else
        {
            // ...just snag the basename from the path.
            plistFileName = Path.GetFileName(plistFilePath);
            plist.ReadFromFile(plistFilePath);
        }

        // modify the plist
        PlistElementDict root = plist.root;
        PlistElementArray nfcFormats = root.CreateArray("com.apple.developer.nfc.readersession.formats");
        nfcFormats.AddString("TAG");

        // save the modified plist
        plist.WriteToFile(plistFilePath);
        Debug.Log($"Wrote Entitlements plist to {plistFilePath}");

        if (addEntitlementFile)
        {
            // add entitlements plist to project
            project.AddFile(plistFilePath, plistFileName);
            project.AddBuildProperty(targetGuid, "CODE_SIGN_ENTITLEMENTS", plistFilePath);
            project.WriteToFile(projectPath);
            Debug.Log($"Added Entitlements plist to project (target: {targetGuid})");
        }
#endif // UNITY_IOS
    }
}
1 Like