5252#define ADD_APP_GROUP
5353
5454using System . IO ;
55- using System ;
5655using UnityEditor ;
5756using UnityEditor . iOS . Xcode ;
58- using System . Collections . Generic ;
59- using System . Diagnostics ;
60- using System . Linq ;
6157using System . Text . RegularExpressions ;
6258using UnityEditor . Build ;
6359using UnityEditor . Build . Reporting ;
@@ -79,17 +75,8 @@ public class BuildPostProcessor : IPostprocessBuildWithReport {
7975 private static readonly string PluginLibrariesPath = Path . Combine ( PackageName , "Runtime" , "Plugins" , "iOS" ) ;
8076 private static readonly string PluginFilesPath = Path . Combine ( "Packages" , PluginLibrariesPath ) ;
8177
82- [ Flags ] private enum Entitlement {
83- None = 0 ,
84- ApsEnv = 1 << 0 ,
85- AppGroups = 1 << 1
86- }
87-
88- private static readonly Dictionary < Entitlement , string > EntitlementKeys = new Dictionary < Entitlement , string > {
89- [ Entitlement . ApsEnv ] = "aps-environment" ,
90- [ Entitlement . AppGroups ] = "com.apple.security.application-groups"
91- } ;
92-
78+ private readonly string _appGroupName = $ "group.{ PlayerSettings . applicationIdentifier } .onesignal";
79+
9380 private string _outputPath ;
9481 private string _projectPath ;
9582
@@ -113,29 +100,19 @@ public void OnPostprocessBuild(BuildReport report) {
113100 _outputPath = report . summary . outputPath ;
114101 _projectPath = PBXProject . GetPBXProjectPath ( _outputPath ) ;
115102 _project . ReadFromString ( File . ReadAllText ( _projectPath ) ) ;
116-
117- // Add required entitlements
118- UpdateEntitlements ( Entitlement . ApsEnv | Entitlement . AppGroups ) ;
103+
104+ // Turn on capabilities required by OneSignal
105+ AddProjectCapabilities ( ) ;
119106
120107 // Add the service extension
121108 AddNotificationServiceExtension ( ) ;
122109
123- // Reload file after changes from AddNotificationServiceExtension
124- _project . WriteToFile ( _projectPath ) ;
125- _project . ReadFromString ( File . ReadAllText ( _projectPath ) ) ;
126-
127- // Turn on push capabilities
128- AddPushCapability ( ) ;
129-
130- // Ensure the Pods target has been added to the extension
131- ExtensionAddPodsToTarget ( ) ;
132-
133110 // Save the project back out
134111 File . WriteAllText ( _projectPath , _project . WriteToString ( ) ) ;
135112 }
136113
137114 /// <summary>
138- /// Checks for existing entitlements file or creates a new one and returns the path
115+ /// Get existing entitlements file if exists or creates a new file, adds it to the project, and returns the path
139116 /// </summary>
140117 private string GetEntitlementsPath ( string targetGuid , string targetName ) {
141118 var relativePath = _project . GetBuildPropertyForAnyConfig ( targetGuid , "CODE_SIGN_ENTITLEMENTS" ) ;
@@ -146,38 +123,12 @@ private string GetEntitlementsPath(string targetGuid, string targetName) {
146123 if ( File . Exists ( fullPath ) )
147124 return fullPath ;
148125 }
149-
150- return Path . Combine ( _outputPath , targetName , $ "{ targetName } .entitlements") ;
151- }
152-
153- /// <summary>
154- /// Add or update the values of necessary entitlements
155- /// </summary>
156- private void UpdateEntitlements ( Entitlement entitlements , string targetGuid , string targetName ) {
157- var entitlementsPath = GetEntitlementsPath ( targetGuid , targetName ) ;
126+
127+ var entitlementsPath = Path . Combine ( _outputPath , targetName , $ "{ targetName } .entitlements") ;
128+
129+ // make new file
158130 var entitlementsPlist = new PlistDocument ( ) ;
159-
160- var existingEntitlements = File . Exists ( entitlementsPath ) ;
161-
162- if ( existingEntitlements )
163- entitlementsPlist . ReadFromFile ( entitlementsPath ) ;
164-
165- var groupsKey = EntitlementKeys [ Entitlement . AppGroups ] ;
166-
167- if ( ( entitlements & Entitlement . AppGroups ) != 0 ) {
168- var groups = entitlementsPlist . root [ groupsKey ] == null
169- ? entitlementsPlist . root . CreateArray ( groupsKey )
170- : entitlementsPlist . root [ groupsKey ] . AsArray ( ) ;
171-
172- var group = $ "group.{ PlayerSettings . applicationIdentifier } .onesignal";
173- if ( groups . values . All ( elem => elem . AsString ( ) != group ) )
174- groups . AddString ( group ) ;
175- }
176-
177131 entitlementsPlist . WriteToFile ( entitlementsPath ) ;
178-
179- if ( existingEntitlements )
180- return ;
181132
182133 // Copy the entitlement file to the xcode project
183134 var entitlementFileName = Path . GetFileName ( entitlementsPath ) ;
@@ -186,25 +137,26 @@ private void UpdateEntitlements(Entitlement entitlements, string targetGuid, str
186137 // Add the pbx configs to include the entitlements files on the project
187138 _project . AddFile ( relativeDestination , entitlementFileName ) ;
188139 _project . SetBuildProperty ( targetGuid , "CODE_SIGN_ENTITLEMENTS" , relativeDestination ) ;
140+
141+ return entitlementsPath ;
189142 }
190143
191- private void UpdateEntitlements ( Entitlement entitlements )
192- => UpdateEntitlements ( entitlements , _project . GetMainTargetGuid ( ) , _project . GetMainTargetName ( ) ) ;
193-
194- private void AddPushCapability ( string targetGuid , string targetName ) {
195- _project . AddCapability ( targetGuid , PBXCapabilityType . PushNotifications ) ;
196- _project . AddCapability ( targetGuid , PBXCapabilityType . BackgroundModes ) ;
144+ /// <summary>
145+ /// Add the required capabilities and entitlements for OneSignal
146+ /// </summary>
147+ private void AddProjectCapabilities ( ) {
148+ var targetGuid = _project . GetMainTargetGuid ( ) ;
149+ var targetName = _project . GetMainTargetName ( ) ;
197150
198151 var entitlementsPath = GetEntitlementsPath ( targetGuid , targetName ) ;
199-
200- // NOTE: ProjectCapabilityManager's 4th constructor param requires Unity 2019.3+
201152 var projCapability = new ProjectCapabilityManager ( _projectPath , entitlementsPath , targetName ) ;
153+
202154 projCapability . AddBackgroundModes ( BackgroundModesOptions . RemoteNotifications ) ;
155+ projCapability . AddPushNotifications ( false ) ;
156+ projCapability . AddAppGroups ( new [ ] { _appGroupName } ) ;
157+
203158 projCapability . WriteToFile ( ) ;
204159 }
205-
206- private void AddPushCapability ( ) =>
207- AddPushCapability ( _project . GetMainTargetGuid ( ) , _project . GetMainTargetName ( ) ) ;
208160
209161 /// <summary>
210162 /// Create and add the notification extension to the project
@@ -237,7 +189,15 @@ private void AddNotificationServiceExtension() {
237189
238190 _project . WriteToFile ( _projectPath ) ;
239191
240- UpdateEntitlements ( Entitlement . AppGroups , extensionGuid , ServiceExtensionTargetName ) ;
192+ // add capabilities + entitlements
193+ var entitlementsPath = GetEntitlementsPath ( extensionGuid , ServiceExtensionTargetName ) ;
194+ var projCapability = new ProjectCapabilityManager ( _projectPath , entitlementsPath , ServiceExtensionTargetName ) ;
195+
196+ projCapability . AddAppGroups ( new [ ] { _appGroupName } ) ;
197+
198+ projCapability . WriteToFile ( ) ;
199+
200+ ExtensionAddPodsToTarget ( ) ;
241201#endif
242202 }
243203
0 commit comments