Xamarin.Forms Target="_blank" links not working WkWebViewRenderer

George Foulds 0 Reputation points
2023-07-20T09:11:19.09+00:00

We are trying to open a certain link that is set to open in a new window. We have set the UIDelegate to 'this' in OnElementChanged in our WkWebViewRenderer and implemented CreateWebView, telling it to open in the current webview if the action.Request.URL is null, This works, but only the second time the link is tapped. The first time produces an error saying that popups are blocked by the browser. Our WkWebViewRenderer CreateWebView looks like this:

[Export("webView:createWebViewWithConfiguration:forNavigationAction:windowFeatures:")]
public WKWebView CreateWebView(WKWebView webView, WKWebViewConfiguration configuration, WKNavigationAction action, WKWindowFeatures features)
        {
            if (action.Request.Url != null)
            {
                webView.LoadRequest(action.Request);
            }

            return null;
        }


And we set the WeakUIDelegate in OnElementChanged like this:

protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            try
            {
                base.OnElementChanged(e);

                if (NativeView != null)
                {
                    WeakUIDelegate = this;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }

Steps to Reproduce

  1. Navigate to https://app.senecalearning.com and click login (top right of the page)
  2. Click continue with Microsoft or Google
  3. Observe that this does not work. No navigation happens. A popup blocked message appears.
  4. Tap the same link again and CreateWebView is hit this time and the link is followed in the same webview.

Expected Behavior

The link to sign in via Google or Microsoft is followed the first time the link is tapped.

Actual Behavior

The link to sign in via Google or Microsoft is followed the second time the link is tapped.

Basic Information

  • Version with issue: Xamarin.Forms 5.0.0.2545
  • Platform Target Frameworks: 
  • iOS: 16.4
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,376 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,092 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.