@@ -356,8 +356,35 @@ private static void ModifyAndroidGradle(bool isPlugin)
356356
357357 private static void BuildIOS ( String path , bool isReleaseBuild )
358358 {
359- // Switch to ios standalone build.
360- EditorUserBuildSettings . SwitchActiveBuildTarget ( BuildTargetGroup . iOS , BuildTarget . iOS ) ;
359+ bool abortBuild = false ;
360+
361+ // abort iOS export if #UNITY_IOS is false.
362+ // Even after SwitchActiveBuildTarget() it will still be false as the code isn't recompiled yet.
363+ // As a workaround, make the user trigger an export again after the switch.
364+
365+ #if ! UNITY_IOS
366+ abortBuild = true ;
367+ if ( Application . isBatchMode )
368+ {
369+ Debug . LogError ( "Incorrect iOS buildtarget, use the -buildTarget argument to set iOS" ) ;
370+ }
371+ else
372+ {
373+ bool dialogResult = EditorUtility . DisplayDialog (
374+ "Switch build target to iOS?" ,
375+ "Exporting to iOS first requires a build target switch.\n Click 'Export iOS' again after all importing has finished." ,
376+ "Switch to iOS" ,
377+ "Cancel"
378+ ) ;
379+ if ( dialogResult )
380+ {
381+ EditorUserBuildSettings . SwitchActiveBuildTarget ( BuildTargetGroup . iOS , BuildTarget . iOS ) ;
382+ }
383+ }
384+ #endif
385+ //don't return within #if !UNITY_IOS as that results in unreachable code warnings.
386+ if ( abortBuild )
387+ return ;
361388
362389 if ( Directory . Exists ( path ) )
363390 Directory . Delete ( path , true ) ;
@@ -396,15 +423,25 @@ private static void BuildIOS(String path, bool isReleaseBuild)
396423 if ( report . summary . result != BuildResult . Succeeded )
397424 throw new Exception ( "Build failed" ) ;
398425
399- //trigger postbuild script manually
426+ // log an error if this code is skipped. (might happen when buildtarget is switched from code)
427+ bool postBuildExecuted = false ;
400428#if UNITY_IOS
401429 XcodePostBuild . PostBuild ( BuildTarget . iOS , report . summary . outputPath ) ;
430+ postBuildExecuted = true ;
402431#endif
403-
404- if ( isReleaseBuild ) {
405- Debug . Log ( "-- iOS Release Build: SUCCESSFUL --" ) ;
406- } else {
407- Debug . Log ( "-- iOS Debug Build: SUCCESSFUL --" ) ;
432+ if ( postBuildExecuted )
433+ {
434+ if ( isReleaseBuild )
435+ {
436+ Debug . Log ( "-- iOS Release Build: SUCCESSFUL --" ) ;
437+ }
438+ else
439+ {
440+ Debug . Log ( "-- iOS Debug Build: SUCCESSFUL --" ) ;
441+ }
442+ } else
443+ {
444+ Debug . LogError ( "iOS export failed. Failed to modify Unity's Xcode project." ) ;
408445 }
409446 }
410447
0 commit comments