1+ #if UNITY_IPHONE && UNITY_EDITOR_OSX
2+
3+ using UnityEngine ;
4+ using UnityEditor ;
5+ using UnityEditor . Callbacks ;
6+ using System . IO ;
7+ using System . Linq ;
8+ using UnityEditor . iOS . Xcode ;
9+
10+ public class iOSPostBuildProcessor
11+ {
12+ [ PostProcessBuild ]
13+ public static void OnPostprocessBuild ( BuildTarget target , string pathToBuiltProject )
14+ {
15+ if ( target == BuildTarget . iOS && IsCommandLineBuild ( ) )
16+ {
17+ Debug . Log ( "Command-line iOS build detected. Modifying Info.plist and Xcode project..." ) ;
18+ ModifyInfoPlist ( pathToBuiltProject ) ;
19+ ModifyXcodeProject ( pathToBuiltProject , GetBundleIdentifierFromArgs ( ) ) ;
20+ }
21+ else
22+ {
23+ Debug . Log ( "Skipping Info.plist modification (not an iOS command-line build)." ) ;
24+ }
25+ }
26+
27+ private static bool IsCommandLineBuild ( )
28+ {
29+ string [ ] args = System . Environment . GetCommandLineArgs ( ) ;
30+ return args . Contains ( "--ciBuild" ) ; // Check for the --ciBuild flag
31+ }
32+
33+ private static void ModifyInfoPlist ( string pathToBuiltProject )
34+ {
35+ string plistPath = Path . Combine ( pathToBuiltProject , "Info.plist" ) ;
36+
37+ if ( ! File . Exists ( plistPath ) )
38+ {
39+ Debug . LogError ( "Info.plist not found!" ) ;
40+ return ;
41+ }
42+
43+ // Load the Info.plist
44+ PlistDocument plist = new PlistDocument ( ) ;
45+ plist . ReadFromFile ( plistPath ) ;
46+
47+ // Get the root dictionary
48+ PlistElementDict rootDict = plist . root ;
49+
50+ // Add App Transport Security Settings
51+ PlistElementDict atsDict = rootDict . CreateDict ( "NSAppTransportSecurity" ) ;
52+ atsDict . SetBoolean ( "NSAllowsArbitraryLoads" , true ) ;
53+
54+ // Save the modified Info.plist
55+ plist . WriteToFile ( plistPath ) ;
56+
57+ Debug . Log ( "Successfully updated Info.plist with NSAllowsArbitraryLoads set to YES." ) ;
58+ }
59+
60+ private static void ModifyXcodeProject ( string pathToBuiltProject , string bundleIdentifier )
61+ {
62+ string pbxprojPath = PBXProject . GetPBXProjectPath ( pathToBuiltProject ) ;
63+ PBXProject pbxProject = new PBXProject ( ) ;
64+ pbxProject . ReadFromFile ( pbxprojPath ) ;
65+
66+ string targetGuid = pbxProject . GetUnityMainTargetGuid ( ) ; // Unity 2019+
67+ pbxProject . SetBuildProperty ( targetGuid , "PRODUCT_BUNDLE_IDENTIFIER" , bundleIdentifier ) ;
68+
69+ pbxProject . WriteToFile ( pbxprojPath ) ;
70+ Debug . Log ( $ "Updated Xcode project with bundle identifier: { bundleIdentifier } ") ;
71+ }
72+
73+ private static string GetBundleIdentifierFromArgs ( )
74+ {
75+ string [ ] args = System . Environment . GetCommandLineArgs ( ) ;
76+ for ( int i = 0 ; i < args . Length ; i ++ )
77+ {
78+ if ( args [ i ] == "--bundleIdentifier" && i + 1 < args . Length )
79+ {
80+ return args [ i + 1 ] ;
81+ }
82+ }
83+ return "com.immutable.Immutable-Sample" ; // Default fallback
84+ }
85+ }
86+
87+ #endif
0 commit comments