Skip to content
Draft
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
16 changes: 15 additions & 1 deletion src/Core/src/Platform/iOS/WebViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,21 @@ public static void UpdateGoForward(this WKWebView platformWebView, IWebView webV

public static void UpdateReload(this WKWebView platformWebView, IWebView webView)
{
platformWebView?.Reload();
if (platformWebView == null)
return;

// For HTML sources, we need to reload the content from the source
// since WKWebView.Reload() doesn't work with LoadHtmlString
if (webView.Source != null && platformWebView is IWebViewDelegate webViewDelegate)
{
// Simply reload the source, this will work for both URL and HTML sources
webView.Source.Load(webViewDelegate);
}
else
{
// Fallback to standard reload
platformWebView.Reload();
}
}

internal static void UpdateCanGoBackForward(this WKWebView platformWebView, IWebView webView)
Expand Down
107 changes: 107 additions & 0 deletions src/Core/tests/DeviceTests/Handlers/WebView/WebViewHandlerTests.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using Microsoft.Maui.Handlers;
using WebKit;
using System.Threading.Tasks;
using System;
using Microsoft.Maui.Controls;
using System.Threading;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
Expand All @@ -10,5 +15,107 @@ WKWebView GetNativeWebView(WebViewHandler webViewHandler) =>

string GetNativeSource(WebViewHandler webViewHandler) =>
GetNativeWebView(webViewHandler).Url.AbsoluteString;

[Fact(DisplayName = "Reload with HtmlWebViewSource should succeed")]
public async Task ReloadWithHtmlWebViewSourceShouldSucceed()
{
var webView = new WebView();
var htmlSource = new HtmlWebViewSource
{
Html = "<html><body><h1>Test Page</h1><p>Initial content</p></body></html>"
};

webView.Source = htmlSource;

var loadTcs = new TaskCompletionSource<WebNavigationResult>();
var reloadTcs = new TaskCompletionSource<WebNavigationResult>();
var navigatedCount = 0;

webView.Navigated += (sender, args) =>
{
navigatedCount++;
if (navigatedCount == 1)
{
// First navigation (initial load)
loadTcs.TrySetResult(args.Result);
}
else if (navigatedCount == 2)
{
// Second navigation (reload)
reloadTcs.TrySetResult(args.Result);
}
};

await InvokeOnMainThreadAsync(async () =>
{
var handler = CreateHandler<WebViewHandler>(webView);

// Wait for initial load to complete
var loadResult = await loadTcs.Task.WaitAsync(TimeSpan.FromSeconds(10));
Assert.Equal(WebNavigationResult.Success, loadResult);

// Now test reload
webView.Reload();

// Wait for reload to complete
var reloadResult = await reloadTcs.Task.WaitAsync(TimeSpan.FromSeconds(10));

// This should succeed, not fail
Assert.Equal(WebNavigationResult.Success, reloadResult);
});
}

[Fact(DisplayName = "Reload with UrlWebViewSource should still work")]
public async Task ReloadWithUrlWebViewSourceShouldWork()
{
// Skip test if no internet
if (await AssertionExtensions.SkipTestIfNoInternetConnection())
return;

var webView = new WebView();
var urlSource = new UrlWebViewSource
{
Url = "https://www.example.com"
};

webView.Source = urlSource;

var loadTcs = new TaskCompletionSource<WebNavigationResult>();
var reloadTcs = new TaskCompletionSource<WebNavigationResult>();
var navigatedCount = 0;

webView.Navigated += (sender, args) =>
{
navigatedCount++;
if (navigatedCount == 1)
{
// First navigation (initial load)
loadTcs.TrySetResult(args.Result);
}
else if (navigatedCount == 2)
{
// Second navigation (reload)
reloadTcs.TrySetResult(args.Result);
}
};

await InvokeOnMainThreadAsync(async () =>
{
var handler = CreateHandler<WebViewHandler>(webView);

// Wait for initial load to complete
var loadResult = await loadTcs.Task.WaitAsync(TimeSpan.FromSeconds(10));
Assert.Equal(WebNavigationResult.Success, loadResult);

// Now test reload
webView.Reload();

// Wait for reload to complete
var reloadResult = await reloadTcs.Task.WaitAsync(TimeSpan.FromSeconds(10));

// This should succeed
Assert.Equal(WebNavigationResult.Success, reloadResult);
});
}
}
}