Skip to content

Fix iOS WebView Navigating event not triggered for target="_blank" links#32199

Merged
jfversluis merged 10 commits intoinflight/currentfrom
copilot/fix-webview-target-blank-ios
Dec 1, 2025
Merged

Fix iOS WebView Navigating event not triggered for target="_blank" links#32199
jfversluis merged 10 commits intoinflight/currentfrom
copilot/fix-webview-target-blank-ios

Conversation

Copy link
Contributor

Copilot AI commented Oct 24, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description of Change

Fixed iOS WebView to properly raise the Navigating event when clicking links with target="_blank" attribute. This aligns iOS behavior with Android and gives developers control over these navigations.

Core Fix (src/Core/src/Platform/iOS/MauiWebViewNavigationDelegate.cs):

  • Removed early TargetFrame check from inside the LinkActivated case that was bypassing the event
  • Always trigger Navigating event first regardless of target attribute
  • Check for target="_blank" links (when TargetFrame == null) AFTER the event is raised
  • Allow developers to cancel via e.Cancel = true
  • If not cancelled, load in the same WebView (preserving existing behavior)
  • Always cancel the original navigation for target="_blank" since we handle it manually

UI Tests Added (runs on all platforms):

  • Created test page with WebView containing target="_blank" link
  • Added button that triggers the link click via JavaScript when tapped
  • Validates that Navigating event is triggered
  • Test uses polling-based waiting mechanism for reliability
  • Uses button-triggered approach for consistent cross-platform behavior
  • Uses x:Name attributes for direct element access in code-behind

Benefits:

  • ✅ Platform consistency: iOS now matches Android behavior
  • ✅ Developer control: Developers can intercept and cancel target="_blank" navigations
  • ✅ Backward compatible: If not cancelled, links still load in same WebView
  • ✅ Follows existing patterns from Blazor WebView iOS implementation

Example Usage:

webView.Navigating += async (s, e) =>
{
    // Now works on iOS for target="_blank" links!
    if (e.Url.Contains("external-site.com"))
    {
        e.Cancel = true;
        await Launcher.OpenAsync(new Uri(e.Url));
    }
};

Issues Fixed

Fixes #30539

Original prompt

This section details on the original issue you should resolve

<issue_title>Clicking on a target="_blank" link in WebView on iOS does not do anything</issue_title>
<issue_description>### Description

Clicking on a relative link with target="_blank" in web view does not raise the Navigating event. There is a workaround for Android, but nothing for iOS. As a developer as far as I can tell there's nothing I can do to handle the links in my app's web view.

https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/webview?view=net-maui-9.0&pivots=devices-android#navigate-to-content-that-opens-a-new-window-on-android

Steps to Reproduce

  1. Create a MAUI app with one WebView on the main page.
  2. Load https://www.tbwnet.ch/services/downloadcenter in the WebView.
  3. Start the app on an iOS device.
  4. Click on any download in the list.

Actual behavior:
Nothing happens.

Expected behavior:
Either the link opens in an external app like a browser or at least the Navigating event is raised to let the developer decide what to do with it.

Link to public reproduction project repository

https://github.com/edgarssults/maui-ios-nonavigation

Version with bug

9.0.81 SR8.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

No response

Affected platforms

iOS

Affected platform versions

iOS 18.5

Did you find any workaround?

No response

Relevant log output

```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@NafeelaNazhir</author><body>
This issue has been verified in Visual Studio Code 1.101.2 with .NET MAUI versions ( 9.0.82 and 9.0.0). This issue has been reproduced on iOS Platform.</body></comment_new>
</comments>

Fixes #30539


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix webview handling of target="_blank" links on iOS Fix iOS WebView Navigating event not triggered for target="_blank" links Oct 24, 2025
Copilot AI requested a review from jfversluis October 24, 2025 11:43
@jfversluis
Copy link
Member

/azp run MAUI-public

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jfversluis jfversluis marked this pull request as ready for review October 24, 2025 14:08
Copilot AI review requested due to automatic review settings October 24, 2025 14:08
Copy link
Contributor

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 a long-standing issue where iOS WebView's Navigating event was not being triggered when users clicked on links with target="_blank" attributes, creating inconsistent behavior between iOS and Android platforms.

Key Changes:

  • Modified the navigation decision handler in MauiWebViewNavigationDelegate.cs to always raise the Navigating event before handling target="_blank" links
  • Developers can now intercept and control target="_blank" navigations through the Navigating event on iOS
  • Added comprehensive UI tests to validate the fix

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/Core/src/Platform/iOS/MauiWebViewNavigationDelegate.cs Restructured navigation decision logic to ensure Navigating event fires for all link types including target="_blank"
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue30539.cs Added NUnit test that validates Navigating event is triggered for target="_blank" links and navigation can be cancelled
src/Controls/tests/TestCases.HostApp/Issues/Issue30539.xaml.cs Created UI test page code-behind that demonstrates event handling for target="_blank" links
src/Controls/tests/TestCases.HostApp/Issues/Issue30539.xaml Created XAML test page with WebView containing target="_blank" links for automated testing

Comment on lines +22 to +23
Thread.Sleep(3000);

Copy link

Copilot AI Oct 24, 2025

Choose a reason for hiding this comment

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

Using Thread.Sleep(3000) is an anti-pattern in UI tests as it makes tests slower and can lead to flaky tests. Consider using App.WaitForElement with a timeout or a more reliable waiting mechanism that polls for the WebView's ready state instead of a fixed 3-second delay.

Suggested change
Thread.Sleep(3000);

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in commit 098a179. Replaced the fixed 3-second sleep with a polling mechanism that checks the NavigatingLabel text every 500ms for up to 15 seconds, breaking early when the expected text is found.

App.Tap("TestWebView");

// Wait a bit for the navigation event to be processed
Thread.Sleep(1000);
Copy link

Copilot AI Oct 24, 2025

Choose a reason for hiding this comment

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

Using Thread.Sleep(1000) creates unnecessary test delays and potential flakiness. Replace with a more reliable waiting mechanism such as App.WaitForElement("NavigatingLabel", timeout: TimeSpan.FromSeconds(2)) or similar polling-based approach.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in commit 098a179. The polling mechanism now handles the wait time more reliably by checking for state changes rather than using fixed delays.

x:Class="Maui.Controls.Sample.Issues.Issue30539">
<VerticalStackLayout Spacing="10" Padding="10">
<Label
Text="Target _blank link should trigger Navigating event"
Copy link

Copilot AI Oct 24, 2025

Choose a reason for hiding this comment

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

The text should display proper formatting for the HTML attribute. Change 'Target _blank' to 'Target="_blank"' to accurately represent the HTML attribute being tested.

Suggested change
Text="Target _blank link should trigger Navigating event"
Text="target=&quot;_blank&quot; link should trigger Navigating event"

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in commit 098a179. Changed the text to use proper XML escaping: target=&quot;_blank&quot;

@jfversluis
Copy link
Member

/azp run MAUI-UITests-public

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jfversluis
Copy link
Member

@copilot can you address the comments? And cross-check if it has to do with the test failure, see below

Assert.That(navigatingLabel.GetText(), Is.EqualTo("Navigating event triggered"))
Expected string length 26 but was 30. Strings differ at index 17.
Expected: "Navigating event triggered"
But was: "Navigating event not triggered"
----------------------------^

Copy link
Contributor Author

Copilot AI commented Oct 25, 2025

@copilot can you address the comments? And cross-check if it has to do with the test failure, see below

Assert.That(navigatingLabel.GetText(), Is.EqualTo("Navigating event triggered"))
...

I've addressed all the review comments in commit 098a179:

Changes made:

  1. Fixed XAML text formatting to properly display target="_blank" with correct escaping
  2. Replaced all Thread.Sleep() calls with a polling-based waiting mechanism (30 attempts × 500ms)
  3. Changed the test approach to use JavaScript to programmatically trigger the link click after page load

Root cause of test failure:
The original test was trying to tap on the WebView element with Appium, but this doesn't actually click the HTML link inside the WebView. The new approach uses EvaluateJavaScriptAsync() in the Navigated event handler to automatically trigger document.getElementById('testLink').click() once the page loads. This simulates a user click and should reliably trigger the Navigating event.

The test now polls the NavigatingLabel text until it changes from "not triggered" to "triggered", which should resolve the assertion failure.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • scanning-api.github.com
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@jfversluis
Copy link
Member

/azp run MAUI-UITests-public

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jfversluis
Copy link
Member

@copilot build error: src\Controls\tests\TestCases.HostApp\Issues\Issue30539.xaml.cs(47,50): Error CA1307: 'string.Contains(string)' has a method overload that takes a 'StringComparison' parameter. Replace this call in 'Maui.Controls.Sample.Issues.Issue30539.WebView_Navigated(object?, Microsoft.Maui.Controls.WebNavigatedEventArgs)' with a call to 'string.Contains(string, System.StringComparison)' for clarity of intent. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1307)

Copy link
Contributor Author

Copilot AI commented Oct 25, 2025

@copilot build error: src\Controls\tests\TestCases.HostApp\Issues\Issue30539.xaml.cs(47,50): Error CA1307: 'string.Contains(string)' has a method overload that takes a 'StringComparison' parameter. Re...

Fixed in commit 2ecec1d. Added StringComparison.Ordinal parameter to both Contains() calls to satisfy the CA1307 code analyzer requirement.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • scanning-api.github.com
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@jsuarezruiz
Copy link
Contributor

jsuarezruiz commented Oct 27, 2025

/azp run MAUI-UITests-public

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

@jsuarezruiz jsuarezruiz left a comment

Choose a reason for hiding this comment

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

/mnt/vss/_work/1/s/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue30539.cs(33,8): error CS8602: Dereference of a possibly null reference. [/mnt/vss/_work/1/s/src/Controls/tests/TestCases.Android.Tests/Controls.TestCases.Android.Tests.csproj]
/mnt/vss/_work/1/s/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue30539.cs(33,8): error CA1307: 'string.Contains(string)' has a method overload that takes a 'StringComparison' parameter. Replace this call in 'Microsoft.Maui.TestCases.Tests.Issues.Issue30539.TargetBlankLinkShouldTriggerNavigatingEvent()' with a call to 'string.Contains(string, System.StringComparison)' for clarity of intent. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1307) [/mnt/vss/_work/1/s/src/Controls/tests/TestCases.Android.Tests/Controls.TestCases.Android.Tests.csproj]
    0 Warning(s)
    2 Error(s)

@copilot Review the compilation errors

Copy link
Contributor Author

Copilot AI commented Oct 31, 2025

/mnt/vss/_work/1/s/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue30539.cs(33,8): error CS8602: Dereference of a possibly null reference. [/mnt/vss/_work/1/s/src/Controls/tests/TestCa...

Fixed in commit bdc3e30:

  1. CS8602 error: Added null-conditional operators (?.) to all GetText() calls and added null check before the Contains() call
  2. CA1307 error: Added StringComparison.Ordinal parameter to the Contains() call on line 33

Both compilation errors should now be resolved.

Copilot AI requested a review from jsuarezruiz October 31, 2025 10:13
@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@jfversluis
Copy link
Member

@copilot lets remove the asserts for the URL and cancel, just that the navigated event was triggered is enough for now

Copy link
Contributor Author

Copilot AI commented Nov 30, 2025

@copilot lets remove the asserts for the URL and cancel, just that the navigated event was triggered is enough for now

Fixed in commit c87f7a1.

Removed the URL and cancel assertions, keeping only the assertion that verifies the Navigating event was triggered.

@jfversluis
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@jfversluis jfversluis changed the base branch from main to inflight/current December 1, 2025 08:54
Copilot AI and others added 10 commits December 1, 2025 08:58
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
- Fixed XAML text to properly display target="_blank" attribute
- Changed test approach to programmatically trigger target="_blank" link click using JavaScript
- Replaced Thread.Sleep with polling-based waiting mechanism
- Test now automatically triggers the click after page load for more reliable testing

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Added StringComparison.Ordinal parameter to both Contains() calls in WebView_Navigated method to satisfy code analyzer requirement CA1307.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
- Added null-conditional operators to GetText() calls to fix CS8602
- Added StringComparison.Ordinal parameter to Contains() call to fix CA1307
- Added null check before Contains() to ensure safe string comparison

Co-authored-by: jsuarezruiz <6755973+jsuarezruiz@users.noreply.github.com>
- Moved link click trigger from C# Navigated event to HTML JavaScript onload
- Added window.onload handler with setTimeout to automatically click the target="_blank" link
- Removed WebView_Navigated event handler as it's no longer needed
- This ensures the link click happens reliably after the page loads, properly triggering the Navigating event

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
The fix for target="_blank" links is specifically for iOS. The test was
failing on Android because the JavaScript-triggered click behavior differs
between platforms. Making the test iOS-only ensures it only runs where the
fix applies and where it can be reliably tested.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
- Remove iOS-only restriction (#if IOS)
- Add a button that triggers the target="_blank" link click via JavaScript
- Remove auto-click JavaScript from HTML that was unreliable across platforms
- Test now: waits for button → taps button → waits for result
- This approach is more reliable and works consistently across platforms

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
- Added x:Name attributes to NavigatingLabel, UrlLabel, and CancelLabel in XAML
- Updated code-behind to use direct references instead of FindByName calls
- Simplified the code by removing null checks since named elements are always available

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Remove URL and cancel assertions as requested, keeping only the core assertion that the Navigating event was triggered.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
@jfversluis jfversluis force-pushed the copilot/fix-webview-target-blank-ios branch from c87f7a1 to 9992752 Compare December 1, 2025 08:58
@jfversluis jfversluis added this to the .NET 10.0 SR2 milestone Dec 1, 2025
@jfversluis jfversluis merged commit e7533e6 into inflight/current Dec 1, 2025
1 check passed
@jfversluis jfversluis deleted the copilot/fix-webview-target-blank-ios branch December 1, 2025 08:58
PureWeen pushed a commit that referenced this pull request Dec 3, 2025
Fix iOS WebView target blank navigation event handling

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Address review feedback: fix test implementation and formatting

- Fixed XAML text to properly display target="_blank" attribute
- Changed test approach to programmatically trigger target="_blank" link click using JavaScript
- Replaced Thread.Sleep with polling-based waiting mechanism
- Test now automatically triggers the click after page load for more reliable testing

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Fix CA1307 build error: add StringComparison parameter to Contains calls

Added StringComparison.Ordinal parameter to both Contains() calls in WebView_Navigated method to satisfy code analyzer requirement CA1307.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Fix CS8602 null reference and CA1307 compilation errors in test

- Added null-conditional operators to GetText() calls to fix CS8602
- Added StringComparison.Ordinal parameter to Contains() call to fix CA1307
- Added null check before Contains() to ensure safe string comparison

Co-authored-by: jsuarezruiz <6755973+jsuarezruiz@users.noreply.github.com>

Fix test by using JavaScript onload to auto-trigger link click

- Moved link click trigger from C# Navigated event to HTML JavaScript onload
- Added window.onload handler with setTimeout to automatically click the target="_blank" link
- Removed WebView_Navigated event handler as it's no longer needed
- This ensures the link click happens reliably after the page loads, properly triggering the Navigating event

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Make test iOS-only since fix is iOS-specific

The fix for target="_blank" links is specifically for iOS. The test was
failing on Android because the JavaScript-triggered click behavior differs
between platforms. Making the test iOS-only ensures it only runs where the
fix applies and where it can be reliably tested.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Redesign test to use button-triggered approach for all platforms

- Remove iOS-only restriction (#if IOS)
- Add a button that triggers the target="_blank" link click via JavaScript
- Remove auto-click JavaScript from HTML that was unreliable across platforms
- Test now: waits for button → taps button → waits for result
- This approach is more reliable and works consistently across platforms

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Add x:Name attributes to Labels and use direct references

- Added x:Name attributes to NavigatingLabel, UrlLabel, and CancelLabel in XAML
- Updated code-behind to use direct references instead of FindByName calls
- Simplified the code by removing null checks since named elements are always available

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Simplify test to only assert Navigating event was triggered

Remove URL and cancel assertions as requested, keeping only the core assertion that the Navigating event was triggered.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
PureWeen pushed a commit that referenced this pull request Dec 3, 2025
Fix iOS WebView target blank navigation event handling

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Address review feedback: fix test implementation and formatting

- Fixed XAML text to properly display target="_blank" attribute
- Changed test approach to programmatically trigger target="_blank" link click using JavaScript
- Replaced Thread.Sleep with polling-based waiting mechanism
- Test now automatically triggers the click after page load for more reliable testing

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Fix CA1307 build error: add StringComparison parameter to Contains calls

Added StringComparison.Ordinal parameter to both Contains() calls in WebView_Navigated method to satisfy code analyzer requirement CA1307.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Fix CS8602 null reference and CA1307 compilation errors in test

- Added null-conditional operators to GetText() calls to fix CS8602
- Added StringComparison.Ordinal parameter to Contains() call to fix CA1307
- Added null check before Contains() to ensure safe string comparison

Co-authored-by: jsuarezruiz <6755973+jsuarezruiz@users.noreply.github.com>

Fix test by using JavaScript onload to auto-trigger link click

- Moved link click trigger from C# Navigated event to HTML JavaScript onload
- Added window.onload handler with setTimeout to automatically click the target="_blank" link
- Removed WebView_Navigated event handler as it's no longer needed
- This ensures the link click happens reliably after the page loads, properly triggering the Navigating event

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Make test iOS-only since fix is iOS-specific

The fix for target="_blank" links is specifically for iOS. The test was
failing on Android because the JavaScript-triggered click behavior differs
between platforms. Making the test iOS-only ensures it only runs where the
fix applies and where it can be reliably tested.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Redesign test to use button-triggered approach for all platforms

- Remove iOS-only restriction (#if IOS)
- Add a button that triggers the target="_blank" link click via JavaScript
- Remove auto-click JavaScript from HTML that was unreliable across platforms
- Test now: waits for button → taps button → waits for result
- This approach is more reliable and works consistently across platforms

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Add x:Name attributes to Labels and use direct references

- Added x:Name attributes to NavigatingLabel, UrlLabel, and CancelLabel in XAML
- Updated code-behind to use direct references instead of FindByName calls
- Simplified the code by removing null checks since named elements are always available

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Simplify test to only assert Navigating event was triggered

Remove URL and cancel assertions as requested, keeping only the core assertion that the Navigating event was triggered.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
PureWeen pushed a commit that referenced this pull request Dec 3, 2025
Fix iOS WebView target blank navigation event handling

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Address review feedback: fix test implementation and formatting

- Fixed XAML text to properly display target="_blank" attribute
- Changed test approach to programmatically trigger target="_blank" link click using JavaScript
- Replaced Thread.Sleep with polling-based waiting mechanism
- Test now automatically triggers the click after page load for more reliable testing

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Fix CA1307 build error: add StringComparison parameter to Contains calls

Added StringComparison.Ordinal parameter to both Contains() calls in WebView_Navigated method to satisfy code analyzer requirement CA1307.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Fix CS8602 null reference and CA1307 compilation errors in test

- Added null-conditional operators to GetText() calls to fix CS8602
- Added StringComparison.Ordinal parameter to Contains() call to fix CA1307
- Added null check before Contains() to ensure safe string comparison

Co-authored-by: jsuarezruiz <6755973+jsuarezruiz@users.noreply.github.com>

Fix test by using JavaScript onload to auto-trigger link click

- Moved link click trigger from C# Navigated event to HTML JavaScript onload
- Added window.onload handler with setTimeout to automatically click the target="_blank" link
- Removed WebView_Navigated event handler as it's no longer needed
- This ensures the link click happens reliably after the page loads, properly triggering the Navigating event

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Make test iOS-only since fix is iOS-specific

The fix for target="_blank" links is specifically for iOS. The test was
failing on Android because the JavaScript-triggered click behavior differs
between platforms. Making the test iOS-only ensures it only runs where the
fix applies and where it can be reliably tested.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Redesign test to use button-triggered approach for all platforms

- Remove iOS-only restriction (#if IOS)
- Add a button that triggers the target="_blank" link click via JavaScript
- Remove auto-click JavaScript from HTML that was unreliable across platforms
- Test now: waits for button → taps button → waits for result
- This approach is more reliable and works consistently across platforms

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Add x:Name attributes to Labels and use direct references

- Added x:Name attributes to NavigatingLabel, UrlLabel, and CancelLabel in XAML
- Updated code-behind to use direct references instead of FindByName calls
- Simplified the code by removing null checks since named elements are always available

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Simplify test to only assert Navigating event was triggered

Remove URL and cancel assertions as requested, keeping only the core assertion that the Navigating event was triggered.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
PureWeen pushed a commit that referenced this pull request Dec 3, 2025
Fix iOS WebView target blank navigation event handling

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Address review feedback: fix test implementation and formatting

- Fixed XAML text to properly display target="_blank" attribute
- Changed test approach to programmatically trigger target="_blank" link click using JavaScript
- Replaced Thread.Sleep with polling-based waiting mechanism
- Test now automatically triggers the click after page load for more reliable testing

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Fix CA1307 build error: add StringComparison parameter to Contains calls

Added StringComparison.Ordinal parameter to both Contains() calls in WebView_Navigated method to satisfy code analyzer requirement CA1307.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Fix CS8602 null reference and CA1307 compilation errors in test

- Added null-conditional operators to GetText() calls to fix CS8602
- Added StringComparison.Ordinal parameter to Contains() call to fix CA1307
- Added null check before Contains() to ensure safe string comparison

Co-authored-by: jsuarezruiz <6755973+jsuarezruiz@users.noreply.github.com>

Fix test by using JavaScript onload to auto-trigger link click

- Moved link click trigger from C# Navigated event to HTML JavaScript onload
- Added window.onload handler with setTimeout to automatically click the target="_blank" link
- Removed WebView_Navigated event handler as it's no longer needed
- This ensures the link click happens reliably after the page loads, properly triggering the Navigating event

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Make test iOS-only since fix is iOS-specific

The fix for target="_blank" links is specifically for iOS. The test was
failing on Android because the JavaScript-triggered click behavior differs
between platforms. Making the test iOS-only ensures it only runs where the
fix applies and where it can be reliably tested.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Redesign test to use button-triggered approach for all platforms

- Remove iOS-only restriction (#if IOS)
- Add a button that triggers the target="_blank" link click via JavaScript
- Remove auto-click JavaScript from HTML that was unreliable across platforms
- Test now: waits for button → taps button → waits for result
- This approach is more reliable and works consistently across platforms

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Add x:Name attributes to Labels and use direct references

- Added x:Name attributes to NavigatingLabel, UrlLabel, and CancelLabel in XAML
- Updated code-behind to use direct references instead of FindByName calls
- Simplified the code by removing null checks since named elements are always available

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Simplify test to only assert Navigating event was triggered

Remove URL and cancel assertions as requested, keeping only the core assertion that the Navigating event was triggered.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
github-actions bot pushed a commit that referenced this pull request Dec 5, 2025
Fix iOS WebView target blank navigation event handling

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Address review feedback: fix test implementation and formatting

- Fixed XAML text to properly display target="_blank" attribute
- Changed test approach to programmatically trigger target="_blank" link click using JavaScript
- Replaced Thread.Sleep with polling-based waiting mechanism
- Test now automatically triggers the click after page load for more reliable testing

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Fix CA1307 build error: add StringComparison parameter to Contains calls

Added StringComparison.Ordinal parameter to both Contains() calls in WebView_Navigated method to satisfy code analyzer requirement CA1307.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Fix CS8602 null reference and CA1307 compilation errors in test

- Added null-conditional operators to GetText() calls to fix CS8602
- Added StringComparison.Ordinal parameter to Contains() call to fix CA1307
- Added null check before Contains() to ensure safe string comparison

Co-authored-by: jsuarezruiz <6755973+jsuarezruiz@users.noreply.github.com>

Fix test by using JavaScript onload to auto-trigger link click

- Moved link click trigger from C# Navigated event to HTML JavaScript onload
- Added window.onload handler with setTimeout to automatically click the target="_blank" link
- Removed WebView_Navigated event handler as it's no longer needed
- This ensures the link click happens reliably after the page loads, properly triggering the Navigating event

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Make test iOS-only since fix is iOS-specific

The fix for target="_blank" links is specifically for iOS. The test was
failing on Android because the JavaScript-triggered click behavior differs
between platforms. Making the test iOS-only ensures it only runs where the
fix applies and where it can be reliably tested.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Redesign test to use button-triggered approach for all platforms

- Remove iOS-only restriction (#if IOS)
- Add a button that triggers the target="_blank" link click via JavaScript
- Remove auto-click JavaScript from HTML that was unreliable across platforms
- Test now: waits for button → taps button → waits for result
- This approach is more reliable and works consistently across platforms

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Add x:Name attributes to Labels and use direct references

- Added x:Name attributes to NavigatingLabel, UrlLabel, and CancelLabel in XAML
- Updated code-behind to use direct references instead of FindByName calls
- Simplified the code by removing null checks since named elements are always available

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Simplify test to only assert Navigating event was triggered

Remove URL and cancel assertions as requested, keeping only the core assertion that the Navigating event was triggered.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
@github-actions github-actions bot locked and limited conversation to collaborators Dec 31, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clicking on a target="_blank" link in WebView on iOS does not do anything

4 participants