question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebView on iOS using Navigating event fails with obj exception

See original GitHub issue

Description

I have an anchor tag which when clicked, is supposed to call the navigating event, where I inspect the url and do different things and set cancel to true.

Completion handler passed to -[Microsoft_Maui_Platform_MauiWebViewUIDelegate webView:contextMenuConfigurationForElement:completionHandler:] was not called.

<WebView x:Name="WebBrowser" Navigating="WebBrowser_Navigating">
  <WebView.Source>
    <HtmlWebViewSource>
      <HtmlWebViewSource.Html>
<!-- html snipped for brevity, include a tag with a url in the href and some text -->
      </HtmlWebViewSource.Html>
    </HtmlWebViewSource>
  <WebView.Source>
</WebView>

private void WebBrowser_Navigating(object? sender, WebNavigatingEventArgs e)
{
    e.Cancel = true;
    // business logic here, snipped for brevity
}

Steps to Reproduce

  1. Create new maui app
  2. Add webview component with an html anchor tag in the xaml that points to any url
  3. Hook into the navigating event - Navigating="WebBrowser_Navigating"
  4. Create callback in the xaml.cs file and in the navigating event callback, set e.Cancel to true
  5. Deploy to iOS device
  6. Click the anchor tag in the web view
  7. Crash

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

iOS 13, 14, 15, 16

Did you find any workaround?

No

Relevant log output

Completion handler passed to -[Microsoft_Maui_Platform_MauiWebViewUIDelegate webView:contextMenuConfigurationForElement:completionHandler:] was not called.

Works fine on Windows and Android, no issue.

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
anton-yashincommented, Feb 14, 2023

This is because MauiWebViewUIDelegate.SetContextMenuConfiguration may not call completionHandler when no webView.Interactions is available. For workaround you should install WKView.UIDelegate where you going to call completion handler. Something like that:

sealed class MauiWebViewUIDelegate2 : MauiWebViewUIDelegate
    {
        public MauiWebViewUIDelegate2(IWebViewHandler handler) : base(handler)
        {
        }

        [SupportedOSPlatform("maccatalyst13.1")]
        [SupportedOSPlatform("ios13.0")]
        [UnsupportedOSPlatform("macos")]
        [UnsupportedOSPlatform("tvos")]
        public override void SetContextMenuConfiguration(WKWebView webView, WKContextMenuElementInfo elementInfo, Action<UIContextMenuConfiguration> completionHandler)
        {
            bool completionHandlerIsInvoked = false;

            base.SetContextMenuConfiguration(webView, elementInfo, cfg =>
            {
                completionHandlerIsInvoked = true;
                completionHandler(cfg);
            });

            if (completionHandlerIsInvoked == false)
                completionHandler(null!);
        }
    }

    sealed class WebViewHandler2 : WebViewHandler
    {
        WKUIDelegate? _delegate;

        public static void MapWKUIDelegate2(IWebViewHandler handler, IWebView webView)
        {
            if (handler is WebViewHandler2 platformHandler)
                handler.PlatformView.UIDelegate = platformHandler._delegate ??= new MauiWebViewUIDelegate2(handler);
        }
    }

    // at your application startup
   WebViewHandler.Mapper.Add(nameof(WKUIDelegate), WebViewHandler2.MapWKUIDelegate2);
   // when maui app is created
  app.ConfigureMauiHandlers(h => h.AddHandler<WebView, WebViewHandler2>());
0reactions
msftbot[bot]commented, Mar 2, 2023

We’ve moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebView Navigated event not firing consistently on iOS
I have a page that has a WebView and an ActivityIndicator. I've wired up event handlers to to the WebView Navigating and Navigated...
Read more >
webView(_:didFail:withError:)
This object corresponds to a WKNavigation object that WebKit returned when the load operation began. You use it to track the progress of...
Read more >
Issue with observing WKWebView URL changes via ...
I have a WKWebView that presents a Laravel web application. I currently have my ViewController setup to catch URL changes – and it...
Read more >
JavaScript Manipulation on iOS Using WebKit
WebKit offers a suite of tools for iOS developers to manipulate JavaScript directly inside a webview in a native app without focusing on...
Read more >
WebView and Android back button navigation
Let's explore some solutions to common problems that Android developers often encounter when using WebView, such as back button navigation.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found