Shawn Roser Shawn Roser - 2 months ago 23x
iOS Question

How to implement UIDocumentInteractionController on iPad using Xamarin

I'm using Xamarin to develop an iPad iOS 7.0 app and I want to use the UIDocumentInteractionController to display a PDF. I used various examples on Stack Overflow to get the proper implementation but nothing has worked.

Here is a link on Stack Overflow which I used for reference.

Monotouch open document - UIDocumentInterationController

I also used this objective C tutorial but have been unable to properly translate this code into C#.

Here is my code. I currently have a toolbar button which, when clicked, should trigger the PDF preview. Nothing happens when I click the button.

What is it that I am missing for this to work?

public override void ViewDidLoad ()
base.ViewDidLoad ();

documentInteractionController = new UIDocumentInteractionController ();

string fileName = "Content/test.pdf";
string localURL = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);
NSUrl URL = new NSUrl(localURL, false);

documentInteractionController = UIDocumentInteractionController.FromUrl (URL);
documentInteractionController.Delegate = new UIDocumentInteractionControllerDelegateClass(this);

toolbarButton.Clicked += delegate(object sender, EventArgs e){
documentInteractionController.PresentOpenInMenu(View.Frame, View, true);

public class UIDocumentInteractionControllerDelegateClass : UIDocumentInteractionControllerDelegate
UIViewController viewC;

public UIDocumentInteractionControllerDelegateClass(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;


After researching this issue, asking the same question on another forum, and using different code examples, I am unable to get this code working using a UIDocumentInteractionController in C#. I can, however, get it working using the objective-c tutorial linked in the above question. This is not useful since I need the code in C# instead of objective-c.

During my research I found this post on Stack Overflow which instead used a QLPreviewController.

Monotouch - Issue with QLPreviewController

This code provides exactly what I need for my application. I made one change to the original location of the PDF and it works as expected.

Here is the code, taken and modified from the above post, I am using to open a PDF in a Quick Preview control which has the "Open In" functionality.

Here is the code for the controller:

QLPreviewController previewController= new QLPreviewController();             

previewController.DataSource=new MyQLPreviewControllerDataSource();     

this.PresentViewController(previewController,true, null);

Here is the code for the Data Source:

public class MyQLPreviewControllerDataSource : QLPreviewControllerDataSource { 

    public override int PreviewItemCount (QLPreviewController controller) {
        return 1;

    public override QLPreviewItem GetPreviewItem (QLPreviewController controller, int index)
        string fileName = @"example.pdf";
        var documents = NSBundle.MainBundle.BundlePath;
        var library = Path.Combine (documents,fileName);
        NSUrl url = NSUrl.FromFilename (library);
        return new QlItem ("Title", url);

Here is the code for the QlItem:

public class QlItem : QLPreviewItem 
        string title; 
        NSUrl uri; 

        public QlItem(string title, NSUrl uri) 
            this.title = title; 
            this.uri = uri; 

        public override string ItemTitle { 
            get { return title; } 

        public override NSUrl ItemUrl { 
            get { return uri; }