Wednesday, March 13, 2013

UIDocumentInteractionController Delegate MonoTouch

Sometimes you need to be able to show Documents within your application.  This is the code in Monotouch to Provide a native "Document Preview" via QuickLook referenced here:

Opening and Previewing Files


This is the code you can place in your ViewController.

                    UIDocumentInteractionController docpreview = UIDocumentInteractionController.FromUrl (NSUrl.FromFilename(path));

                    InvokeOnMainThread (delegate {
                        this.DismissViewController(true,delegate {
                        docpreview.Delegate = new Test(this);

                        docpreview.PresentPreview (true);
                    });
                    });


The Delegate made for your specific UIViewController to control behavior after the Preview Ends :

In this particular instance my ViewController I was calling the DocumentPreview from was called "FirstViewController".

public class Test : UIDocumentInteractionControllerDelegate
    {

            UIViewController viewC;
            
            public Test(UIViewController controller)
            {
                viewC = controller;
            }
            
            public override UIViewController ViewControllerForPreview (UIDocumentInteractionController controller)
            {
                return viewC;
            }
            
            public override UIView ViewForPreview (UIDocumentInteractionController controller)
            {
                return viewC.View;
            }
            
            public override RectangleF RectangleForPreview (UIDocumentInteractionController controller)
            {
                return viewC.View.Frame;
            }
            public override void DidEndPreview (UIDocumentInteractionController controller)
            {
                ((FirstViewController)viewC).PresentPrintDialog (controller.Url.Path);
            }
    }

Below is the screenshot of opening the file in the QuickLook Document Preview :





No comments:

Post a Comment