Skip to content

[Android][iOS] WebView Navigated event not triggered when using HtmlWebViewSource #28354

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

Merged
merged 2 commits into from
Apr 18, 2025

Conversation

Tamilarasan-Paranthaman
Copy link
Contributor

@Tamilarasan-Paranthaman Tamilarasan-Paranthaman commented Mar 12, 2025

Root Cause of the issue

  • On Android and iOS, the Navigated event is not triggered when using HtmlWebViewSource without a BaseUrl. This occurs because when BaseUrl is null, a default value is assigned (AssetBaseUrl for Android and NSBundle.MainBundle.BundlePath for iOS). In the Navigated event, execution exits early when these default values are used, preventing the event from being triggered.

Description of Change

  • I have removed these conditions on both Android and iOS to fix the issue. This check seems unnecessary because the default value is explicitly set while loading the HTML when BaseUrl is null, so there is no need to restrict it in the Navigated event. Additionally, on iOS, the Navigating event was called without any such condition restriction.

Issues Fixed

Fixes #23502
Fixed #21800

Tested the behaviour in the following platforms

  • iOS
  • Android
  • Windows
  • Mac

Screenshot

Platform Before Issue Fix After Issue Fix
Android
Before-Fix-Android.mov
After-Fix-Android.mov
iOS
Before-Fix-iOS.mov
After-Fix-iOS.mov

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Mar 12, 2025
Copy link
Contributor

Hey there @Tamilarasan-Paranthaman! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@Tamilarasan-Paranthaman Tamilarasan-Paranthaman marked this pull request as ready for review March 14, 2025 03:33
@Copilot Copilot AI review requested due to automatic review settings March 14, 2025 03:33
@Tamilarasan-Paranthaman Tamilarasan-Paranthaman requested a review from a team as a code owner March 14, 2025 03:33
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an issue where the WebView Navigated event was not being triggered when using HtmlWebViewSource without a BaseUrl on Android and iOS. The changes remove conditions that prevented the event from firing when default asset URLs were used.

  • Removed conditional checks for default asset URLs in the Navigated event on Android and iOS.
  • Added new UI test cases in TestCases.HostApp and automated tests in TestCases.Shared.Tests to verify the fix.
  • Updated platform-specific code in MauiWebViewClient.cs, WebViewHandler.Android.cs, and MauiWebViewNavigationDelegate.cs to align with the intended behavior.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Controls/tests/TestCases.HostApp/Issues/Issue23502.cs Added UI test page demonstrating WebView events.
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue23502.cs Added automated test to verify WebView events.
src/Core/src/Platform/Android/MauiWebViewClient.cs Removed check for AssetBaseUrl in OnPageStarted and OnPageFinished to ensure proper event triggering.
src/Core/src/Handlers/WebView/WebViewHandler.Android.cs Removed condition checking AssetBaseUrl in NavigatingCanceled.
src/Core/src/Platform/iOS/MauiWebViewNavigationDelegate.cs Removed early return when URL equals the default asset URL to allow the Navigated event to trigger.
Comments suppressed due to low confidence (2)

src/Core/src/Handlers/WebView/WebViewHandler.Android.cs:146

  • Ensure that removing the check for AssetBaseUrl in NavigatingCanceled is fully validated so that asset-based URLs are handled appropriately without unintended side effects.
if (VirtualView == null || string.IsNullOrWhiteSpace(url))

src/Core/src/Platform/iOS/MauiWebViewNavigationDelegate.cs:43

  • The removal of the early return for the default asset URL in iOS is appropriate for resolving the issue; please ensure that this change does not adversely affect valid file-based navigations.
if (url == "file://{NSBundle.MainBundle.BundlePath}/")

@@ -22,7 +22,7 @@ public override bool ShouldOverrideUrlLoading(WebView? view, IWebResourceRequest

public override void OnPageStarted(WebView? view, string? url, Bitmap? favicon)
{
if (!_handler.TryGetTarget(out var handler) || handler.VirtualView == null || url == WebViewHandler.AssetBaseUrl)
if (!_handler.TryGetTarget(out var handler) || handler.VirtualView == null)
Copy link
Preview

Copilot AI Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removal of the AssetBaseUrl check in OnPageStarted appears intentional for fixing the bug; please ensure that this change does not introduce regressions when handling local asset URLs.

Copilot uses AI. Check for mistakes.

@@ -51,7 +51,7 @@ public override void OnPageStarted(WebView? view, string? url, Bitmap? favicon)

public override void OnPageFinished(WebView? view, string? url)
{
if (!_handler.TryGetTarget(out var handler) || handler.VirtualView == null || string.IsNullOrWhiteSpace(url) || url == WebViewHandler.AssetBaseUrl)
if (!_handler.TryGetTarget(out var handler) || handler.VirtualView == null || string.IsNullOrWhiteSpace(url))
Copy link
Preview

Copilot AI Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirm that the removal of the AssetBaseUrl condition in OnPageFinished does not affect scenarios where asset-based URLs might be used, ensuring consistency with the Android behavior.

Copilot uses AI. Check for mistakes.

@PureWeen PureWeen changed the base branch from main to inflight/current April 18, 2025 09:35
@PureWeen PureWeen merged commit 3a41053 into dotnet:inflight/current Apr 18, 2025
128 of 130 checks passed
PureWeen pushed a commit that referenced this pull request Apr 23, 2025
…ebViewSource (#28354)

* WebView navigated event fix

* Test sample changes.
prakashKannanSf3972 pushed a commit to prakashKannanSf3972/maui that referenced this pull request Apr 24, 2025
…ebViewSource (dotnet#28354)

* WebView navigated event fix

* Test sample changes.
NanthiniMahalingam pushed a commit to NanthiniMahalingam/maui that referenced this pull request Apr 24, 2025
…ebViewSource (dotnet#28354)

* WebView navigated event fix

* Test sample changes.
prakashKannanSf3972 pushed a commit to prakashKannanSf3972/maui that referenced this pull request Apr 25, 2025
…ebViewSource (dotnet#28354)

* WebView navigated event fix

* Test sample changes.
prakashKannanSf3972 pushed a commit to prakashKannanSf3972/maui that referenced this pull request Apr 28, 2025
…ebViewSource (dotnet#28354)

* WebView navigated event fix

* Test sample changes.
github-actions bot pushed a commit that referenced this pull request Apr 28, 2025
…ebViewSource (#28354)

* WebView navigated event fix

* Test sample changes.
anandhan-rajagopal pushed a commit to anandhan-rajagopal/maui that referenced this pull request May 2, 2025
…ebViewSource (dotnet#28354)

* WebView navigated event fix

* Test sample changes.
anandhan-rajagopal pushed a commit to anandhan-rajagopal/maui that referenced this pull request May 2, 2025
…ebViewSource (dotnet#28354)

* WebView navigated event fix

* Test sample changes.
PureWeen pushed a commit that referenced this pull request May 2, 2025
…ebViewSource (#28354)

* WebView navigated event fix

* Test sample changes.
SuthiYuvaraj pushed a commit to SuthiYuvaraj/maui that referenced this pull request May 9, 2025
…ebViewSource (dotnet#28354)

* WebView navigated event fix

* Test sample changes.
@github-actions github-actions bot locked and limited conversation to collaborators May 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-webview WebView community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/android platform/ios
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maui webview .Navigated event is not triggered Webview events fire different on iOS and Android
3 participants