'How to add App Capabilities such as Push Notifications + Background Mode to Xcode automated from command line / for Unity
I wanted to add App Capabilities in an automated fashion without having to use the user interface to click through. Something that I could use for build automation.
There are several ways to modify an Xcode project to include App capabilities such as Push Notification or Background Modes. When you use a development framework, such as Unity, ideally, the framework or a plugin will do the necessary work for you.
If not, which happens in my case in my current Unity project, you will have to do it manually by opening up Xcode, and go to the Project Properties > Signing & Capabilities section.
I was wondering if there were command-line solutions for this.
Update: I found the solution to this. See below.
Solution 1:[1]
Hurray: I've managed to solve the issue.
I post this question with solutions here, after I have not found any good explanation or solution. I hope it saves the day for you out there. I'm using Unity 2020, and Xcode 13.3, but it very likely works on older and newer versions, too.
The Signing & Capabilities section in Xcode is an assistive view, which means that it analyses the project files and then shows the capabilities which are activated. If you add a capability with the +-Plus button, it creates and/or modifies the necessary files in the project.
In an automated approach, you would want to do those modifications from the command line. Unity's PBXProject is a powerful tool to manipulate Xcode projects. Or there's a Python gem Xcodeproj. Sometimes, however, it does not work reliable, due to conflicts between plugins. It's why I outline here quickly how you would add them in the command line.
Adding App Capability "Push Notifications"
Find the file unity.entitlements (or any other .entitlements file which is already added to the project). Then add the following content to the file:
<key>aps-environment</key>
<string>development</string>
For a test deployment, the following is a possible bash script code snippet:
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<plist version=\"1.0\">
<dict>
<key>aps-environment</key>
<string>development</string>
</dict>
</plist>" > myProjectPath/Unity-iPhone/unity.entitlements
For a production deployment to the app store, the string value should be production:
<key>aps-environment</key>
<string>production</string>
As long, as the entitlement file is included in the project, Xcode will figure out that this app has/should now have the App Capability "Push Notifications"
Example Adding App Capability: "Background Modes"
This app capability is controlled by an array value in the project's plist, normally in the Info.plist file. The value is as follows:
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>
You can insert those lines easily with the mac program plistbuddy (a handy guide here):
set +e
/usr/libexec/PlistBuddy -c "Add :UIBackgroundModes array" ./projectPath/Info.plist || true
/usr/libexec/PlistBuddy -c "Add :UIBackgroundModes: string audio" ./projectPath/Info.plist || true
/usr/libexec/PlistBuddy -c "Add :UIBackgroundModes: string remote-notification" ./projectPath/Info.plist || true
set -e
The above line adds the Background Mode capability with "Audio" and "Remote Notification". The other values as listed here in Apple's documentation. You can add more modes to the BackgroundModes array by duplicating a line in the script above.
Hopefully, it was helpful to you and enables you to create fun games. Enjoy!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Matthias |
