0

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);
     }
}

1 Answers1

0

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

ColeX
  • 14,062
  • 5
  • 43
  • 240
  • Thank you! Your answer helped me to solve the problem after implementing the missing stuff and deleting the following part from the code: viewer.WillBeginSendingToApplication += async (sender, args) => { DocumentOpened?.Invoke(this, EventArgs.Empty); }; Until this part was there I immediately got a popup that said "Could not open document" after selecting a document. – szviktor94 Oct 05 '21 at 14:57
  • Now that opening a document works I have a small problem. I select an application from the list to open a document, it opens up, I close it, and then I see that the options menu is still there and none of its buttons react when I tap on them. The only way to close it is to tap outside of the options menu. How can I achieve to close the options menu after selecting an option from the menu? – szviktor94 Oct 05 '21 at 14:58
  • You will need to override another method in the delegate , please Check https://stackoverflow.com/a/21684241/8187800 – ColeX Oct 06 '21 at 01:29