Phedg1
Phedg1

Reputation: 1968

How can I get Unity to automatically add the Sign In With Apple capability when compiling for iOS?

I have been Googling for hours and trying every variation of this code that I can think of but I haven't been able to get the Sign In With Apple capability to be added automatically.

I have tried examples from this Github project: https://github.com/lupidan/apple-signin-unity/blob/master/AppleAuth/Editor/ProjectCapabilityManagerExtension.cs

I have been following these posts: https://forum.unity.com/threads/how-to-put-ios-entitlements-file-in-a-unity-project.442277/

https://answers.unity.com/questions/1224123/enable-push-notification-in-xcode-project-by-defau.html

An entitlements file without this capability looks 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>
</dict>
</plist>

And one with the capability looks 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.applesignin</key>
    <array>
        <string>Default</string>
    </array>
</dict>
</plist>

I have tried to guestimate with this code:

proj.AddBuildProperty(target, "SystemCapabilities", "{com.apple.developer.applesignin = [Default];}");
proj.AddCapability(target, PBXCapabilityType.SignInWithApple, relativeDestination, true);

But none of these modify the entitlements file or add the capability.

I treid using the ProjectCapabilityManager with this:

ProjectCapabilityManager capabilityManager = new ProjectCapabilityManager(buildPath, filename, targetName);
capabilityManager.AddSignInWithApple();
capabilityManager.WriteToFile();

But I get an error message in the console saying that access was denied to the buildPath (which was provided by OnPostProcessBuild())

I could really use some help.

Upvotes: 2

Views: 4088

Answers (2)

劉思源
劉思源

Reputation: 1

I have the same problem, this is my solution and it works. PS. Unity 2019.4.23, XCode 13.3

        var pbxPath = pathToXCode + "/Unity-iPhone.xcodeproj/project.pbxproj";

        PBXProject pbxProject = new PBXProject();
        pbxProject.ReadFromFile(pbxPath);

        string targetGuid = pbxProject.GetUnityMainTargetGuid();
        pbxProject.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");

        string unityFrameworkGuid = pbxProject.TargetGuidByName("UnityFramework");
        pbxProject.AddFrameworkToProject(unityFrameworkGuid, "AuthenticationServices.framework", true);
        pbxProject.SetBuildProperty(unityFrameworkGuid, "ENABLE_BITCODE", "NO");

        pbxProject.RemoveFrameworkFromProject(targetGuid, AuthenticationServicesFramework);
        File.WriteAllText(pbxPath, pbxProject.WriteToString());

        string fn = "ios.entitlements";
        ProjectCapabilityManager mgr = new ProjectCapabilityManager(pbxPath, fn, "Unity-iPhone");
        mgr.AddSignInWithApple();
        mgr.WriteToFile();

Upvotes: 0

FernandoChen
FernandoChen

Reputation: 11

I'm on Unity3D 5.6.7f and this is what I did to make it work:

  • Download XcodeAPI from Unity's official bitbucket repositry

  • Open CapabilityManager.cs and add the following code

    /// <summary>
    /// Add Sign In With Apple capability to the project.
    /// </summary>
    public void AddSignInWithApple()
    {
        var arr = (GetOrCreateEntitlementDoc().root[SignInWithAppleEntitlements.Key] = new PlistElementArray()) as PlistElementArray;
        arr.values.Add(new PlistElementString("Default"));
    }
    

    and this

    internal class SignInWithAppleEntitlements
    {
        internal static readonly string Key = "com.apple.developer.applesignin";
    }
    

    Now you can add Sign In With Apple capability with a post-process like this

    ProjectCapabilityManager projCapManager = new ProjectCapabilityManager(projectPath,"YouAppName.entitlements","Unity-iPhone");
    projCapManager.AddSignInWithApple();
    

Upvotes: 1

Related Questions