Search code examples
c#xamarin.formsxamarin.iosquicklook

After tapping on the Quick Look icon the document is not opened


My task is to implement the Quick Look functionality in a way to present options for opening the chosen document. I'm using the UIDocumentInteractionController's PresentOptionsMenu method to display the options. The Quick Look icon is displayed but when I click on it then nothing happens. The popup with the options closes and the document is not opened.

What do I do wrong? Why is to document not opened after clicking on the Quick Look icon?

The popup displayed after selecting a document

My code is:

public class DataViewer
{
     public event EventHandler DocumentOpened;

     public async Task OpenDocument(string filePath)
     {
         var viewer = UIDocumentInteractionController.FromUrl(new NSUrl(filePath, false));
         viewer.WillBeginSendingToApplication += async (sender, args) =>
         {
             DocumentOpened?.Invoke(this, EventArgs.Empty);
         };
         var controller = UIApplication.SharedApplication.KeyWindow.RootViewController;
         viewer.PresentOptionsMenu(controller.View.Frame, controller.View, true);
     }
}

Solution

  • It needs to indicate a ViewController for presenting a document preview .

    Try to implement method ViewControllerForPreview in delegate UIDocumentInteractionControllerDelegate.

    var viewer = UIDocumentInteractionController.FromUrl(new NSUrl(filePath, false));
    viewer.WillBeginSendingToApplication += async (sender, args) =>
    {
        DocumentOpened?.Invoke(this, EventArgs.Empty);
    };
    var controller = UIApplication.SharedApplication.KeyWindow.RootViewController;
    
    viewer.Delegate = new MyClass(controller);   //add this line
    viewer.PresentOptionsMenu(controller.View.Frame, controller.View, true);
    
    
     public class MyClass : UIDocumentInteractionControllerDelegate
        {
            readonly UIViewController _controller;
            public MyClass(UIViewController controller)
            {
                _controller = controller;
            }
    
            public override UIViewController ViewControllerForPreview(UIDocumentInteractionController controller)
            {
                return _controller;
            }
    
            public override UIView ViewForPreview(UIDocumentInteractionController controller)
            {
                return _controller.View;
            }
        }
    

    Refer to

    https://stackoverflow.com/a/34326921/8187800

    https://stackoverflow.com/a/58269780/8187800