Skip to content

Commit 6fc738c

Browse files
authored
Prevent incomplete iOS export after build target switch. (juicycleff#838)
* Avoid code being skipped after changing buildtarget to iOS. * Prevent unreachable code warnings from early return.
1 parent 4caddd4 commit 6fc738c

File tree

1 file changed

+45
-8
lines changed
  • example/unity/DemoApp/Assets/FlutterUnityIntegration/Editor

1 file changed

+45
-8
lines changed

example/unity/DemoApp/Assets/FlutterUnityIntegration/Editor/Build.cs

+45-8
Original file line numberDiff line numberDiff line change
@@ -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.\nClick '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

Comments
 (0)