maui: [regression/8.0.3] [iOS] [SDK 17] Touch events are not working on WebView when a PDF is displayed.

Description

With iOS Sdk 17 / XCode 15, the WebView on iOS doesn’t respond to any touch events (no zoom or scroll is possible) with PDF file. Works fine with any other html content.

Steps to Reproduce

  1. Create a new Maui App
  2. Add a web view control under a Grid Element to the Xaml
        <Grid>
            <WebView Grid.Row="0" x:Name="WebView"
                Source="https://www.africau.edu/images/default/sample.pdf">
             </WebView>
       </Grid>
  1. Launch the App on iOS (16 or 17)

Actual: PDF file is rendered but no touch events are working on WebView (No scroll, no Zoom).

Expected: Should be able to zoom or scroll.

Notes:

  • Rendering a webpage works fine without any issue (e.g. www.bing.com)
  • This worked fine before updating to Microsoft.iOS SDK / XCode 15 for SDK 17
  • Tested on .Net Maui Workload (8.0.0-rc.2.9511, 8.0.0-rc.2.9530) and both are impacted.

Link to public reproduction project repository

No response

Version with bug

8.0.0-rc.2.9511

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.0-rc.2.9373

Affected platforms

iOS

Affected platform versions

iOS (All versions)

Did you find any workaround?

No 😦

Relevant log output

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (6 by maintainers)

Most upvoted comments

I can confirm that it is fixed in 8.060 nightly build.

A slightly more involved solution, but I ended up resolving this by creating a custom control that implements PDFView from PDFKit instead of using WkWebView for iOS.

This solution provides an equal user experience and allows you to leverage the other features of PDFKit if needed.

This repo by @vitalii-vov has a great example on how you can implement this: https://github.com/vitalii-vov/Maui.PDFView/blob/master/Maui.PDFView/Platforms/iOS/PdfViewHandler.cs


For the Android implementation of this control, I used a WebView with pdf.js by Mozilla.

This blog outlines how you can implement pdf.js in Xamarin.Forms: https://medium.com/medialesson/show-pdf-files-in-your-xamarin-android-app-3718148de1c0

These steps can be adapted to use .NET MAUI handlers fairly simply.


I didn’t need to implement this control for Mac or Windows but imagine PDFKit would work the same for Mac and a similar WebView solution would work for Windows.

For more info on creating custom controls from scratch see Create a custom control using handlers.

Hello @last-Programmer ,

i created a ViewHandler for iOS: Here is the Code:

`using CoreGraphics; using Foundation; using Microsoft.Maui.Handlers; using PlentycoMaui.Logger; using UIKit; using WebKit;

namespace PlentycoMaui.iOS;

public class PdfViewHandler : ViewHandler<IPdfView, WKWebView> { private static readonly PropertyMapper<IPdfView, PdfViewHandler> PfgViewMapper = new(ViewMapper); private WKWebView _wkWebView; private UIButton _closeButton; private const int ButtonSize = 50; public PdfViewHandler() : base(PfgViewMapper) { }

protected override WKWebView CreatePlatformView()
{
    try
    {
        if (_wkWebView == null)
        {
            var wkWebViewConfiguration = new WKWebViewConfiguration();
            _wkWebView = new WKWebView(CGRect.Empty, wkWebViewConfiguration)
            {
                AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
            };

            _wkWebView.Configuration.Preferences.SetValueForKey(Foundation.NSObject.FromObject(true), (Foundation.NSString)"allowFileAccessFromFileURLs");
        }

        // We implemented this solution on the 10.01.2024 because if we put the close button and the pdf view inside any grid or layout, the touch events are gone in the pdf view. 
        // We can revert this solution when the ticket is fixed https://github.com/dotnet/maui/issues/18716
        CreateCloseButton();

        var url = new NSUrl(VirtualView.PdfUri, false);
        _wkWebView.LoadFileUrl(url, url);
    }
    catch (Exception e)
    {
        Log.Error(e.Message, e.StackTrace, e);
    }


    return _wkWebView;
}

private void CreateCloseButton()
{
    if (_closeButton != null)
    {
        return;
    }

    _closeButton = new UIButton(UIButtonType.Custom);
    _closeButton.Frame = new CGRect(UIScreen.MainScreen.Bounds.Width - ButtonSize - 4, 4, ButtonSize, ButtonSize);
    _closeButton.BackgroundColor = FromHex("#D71065");

    _closeButton.Layer.CornerRadius = ButtonSize / 2;

    var label = new UILabel(new CGRect(0, 0, _closeButton.Frame.Width, _closeButton.Frame.Height));
    label.Text = "X";
    label.TextColor = UIColor.White;
    label.Font = UIFont.SystemFontOfSize(24);
    label.TextAlignment = UITextAlignment.Center;
    label.Center = new CGPoint(_closeButton.Frame.Width / 2, _closeButton.Frame.Height / 2);

    _closeButton.AddSubview(label);
    _wkWebView.Add(_closeButton);
    _closeButton.TouchDown += CloseButtonTouchDown;
    _closeButton.TouchUpOutside += CloseButtonTouchUpOutside;
    _closeButton.TouchUpInside += CloseButtonTouchUpInside;
}


private void CloseButtonTouchDown(object sender, EventArgs ev)
{
    if (_closeButton == null)
    {
        return;
    }

    try
    {
        _closeButton.Alpha = 0.7f;
    }
    catch (Exception e)
    {
        Log.Error(e.Message, e.StackTrace, e);
    }
}

private void CloseButtonTouchUpOutside(object sender, EventArgs e)
{
    if (_closeButton == null)
    {
        return;
    }

    try
    {
        _closeButton.Alpha = 1.0f;
    }
    catch (Exception ex)
    {
        Log.Error(ex.Message, ex.StackTrace, ex);
    }
}

private void CloseButtonTouchUpInside(object sender, EventArgs ev)
{
    try
    {
        if (VirtualView.Close != null)
        {
            VirtualView.Close();
        }

        if (_closeButton == null)
        {
            return;
        }

        _closeButton.Alpha = 1.0f;
    }
    catch (Exception e)
    {
        Log.Error(e.Message, e.StackTrace, e);
    }
}

public override Size GetDesiredSize(double widthConstraint, double heightConstraint)
{
    try
    {
        if (_closeButton != null)
        {
            _closeButton.Frame = new CGRect(widthConstraint / 2 - ButtonSize / 2, heightConstraint - ButtonSize - 4, ButtonSize, ButtonSize);
        }
    }
    catch (Exception e)
    {
        Log.Error(e.Message, e.StackTrace, e);
    }

    return base.GetDesiredSize(widthConstraint, heightConstraint);
}

protected override void DisconnectHandler(WKWebView platformView)
{
    base.DisconnectHandler(platformView);

    try
    {
        if (_closeButton != null)
        {
            _closeButton.TouchDown -= CloseButtonTouchDown;
            _closeButton.TouchUpInside -= CloseButtonTouchUpInside;
            _closeButton.TouchUpOutside -= CloseButtonTouchUpOutside;
        }

        _wkWebView?.Dispose();
    }
    catch (Exception e)
    {
        Log.Error(e.Message, e.StackTrace, e);
    }
}

private UIColor FromHex(string hex)
{
    hex = hex.TrimStart('#');

    if (hex.Length == 6)
    {
        int r = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
        int g = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
        int b = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);

        return UIColor.FromRGB(r, g, b);
    }

    return UIColor.Red;
}

}`