Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BlazorWebView on iOS/MacCatalyst 18 #23906

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 38 additions & 1 deletion src/BlazorWebView/src/Maui/BlazorWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,44 @@ namespace Microsoft.AspNetCore.Components.WebView.Maui
/// </summary>
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();

Expand Down
4 changes: 2 additions & 2 deletions src/BlazorWebView/src/Maui/iOS/BlazorWebViewHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public partial class BlazorWebViewHandler : ViewHandler<IBlazorWebView, WKWebVie
{
private IOSWebViewManager? _webviewManager;

internal const string AppOrigin = "app://" + BlazorWebView.AppHostAddress + "/";
internal static readonly Uri AppOriginUri = new(AppOrigin);
internal static string AppOrigin { get; } = "app://" + BlazorWebView.AppHostAddress + "/";
internal static Uri AppOriginUri { get; } = new(AppOrigin);
private const string BlazorInitScript = @"
window.__receiveMessageCallbacks = [];
window.__dispatchMessageCallback = function(message) {
Expand Down
Loading