diff --git a/src/BlazorWebView/src/Maui/Android/AndroidWebKitWebViewManager.cs b/src/BlazorWebView/src/Maui/Android/AndroidWebKitWebViewManager.cs index 5963abf04bfc..90fc13c88326 100644 --- a/src/BlazorWebView/src/Maui/Android/AndroidWebKitWebViewManager.cs +++ b/src/BlazorWebView/src/Maui/Android/AndroidWebKitWebViewManager.cs @@ -22,9 +22,9 @@ internal class AndroidWebKitWebViewManager : WebViewManager // Using an IP address means that WebView doesn't wait for any DNS resolution, // making it substantially faster. Note that this isn't real HTTP traffic, since // we intercept all the requests within this origin. - private static readonly string AppOrigin = $"https://{BlazorWebView.AppHostAddress}/"; - private static readonly Uri AppOriginUri = new(AppOrigin); - private static readonly AUri AndroidAppOriginUri = AUri.Parse(AppOrigin)!; + private static string AppOrigin { get; } = $"https://{BlazorWebView.AppHostAddress}/"; + private static Uri AppOriginUri { get; } = new(AppOrigin); + private static AUri AndroidAppOriginUri { get; } = AUri.Parse(AppOrigin)!; private readonly ILogger _logger; private readonly AWebView _webview; private readonly string _contentRootRelativeToAppRoot; diff --git a/src/BlazorWebView/src/Maui/BlazorWebView.cs b/src/BlazorWebView/src/Maui/BlazorWebView.cs index a2284fb4cdea..330ead19c2d7 100644 --- a/src/BlazorWebView/src/Maui/BlazorWebView.cs +++ b/src/BlazorWebView/src/Maui/BlazorWebView.cs @@ -11,7 +11,44 @@ namespace Microsoft.AspNetCore.Components.WebView.Maui /// public partial class BlazorWebView : View, IBlazorWebView { - internal const string AppHostAddress = "0.0.0.0"; + internal static string AppHostAddress { get; } = GetAppHostAddress(); + + private const string AppHostAddressAlways0000Switch = "BlazorWebView.AppHostAddressAlways0000"; + + private static bool IsAppHostAddressAlways0000Enabled => + AppContext.TryGetSwitch(AppHostAddressAlways0000Switch, out var enabled) && enabled; + + private static string GetAppHostAddress() + { + if (IsAppHostAddressAlways0000Enabled) + { + return "0.0.0.0"; + } + else + { +#if IOS || MACCATALYST + // On iOS/MacCatalyst 18 and higher the 0.0.0.0 address does not work, so we use localhost instead. + // This preserves behavior on older versions of those systems, while defaulting to new behavior on + // the new system. + + // Note that pre-release versions of iOS/MacCatalyst have the expected Major/Minor values, + // but the Build, MajorRevision, MinorRevision, and Revision values are all -1, so we need + // to pass in int.MinValue for those values. + + if (System.OperatingSystem.IsIOSVersionAtLeast(major: 18, minor: int.MinValue, build: int.MinValue) || + System.OperatingSystem.IsMacCatalystVersionAtLeast(major: 18, minor: int.MinValue, build: int.MinValue)) + { + return "localhost"; + } + else + { + return "0.0.0.0"; + } +#else + return "0.0.0.0"; +#endif + } + } private readonly JSComponentConfigurationStore _jSComponents = new(); diff --git a/src/BlazorWebView/src/Maui/iOS/BlazorWebViewHandler.iOS.cs b/src/BlazorWebView/src/Maui/iOS/BlazorWebViewHandler.iOS.cs index 1645d8a8e145..5a5698d697e4 100644 --- a/src/BlazorWebView/src/Maui/iOS/BlazorWebViewHandler.iOS.cs +++ b/src/BlazorWebView/src/Maui/iOS/BlazorWebViewHandler.iOS.cs @@ -23,8 +23,8 @@ public partial class BlazorWebViewHandler : ViewHandler