How to implement UIDocumentInteractionController on iPad using Xamarin

I am using Xamarin to develop the iPad iOS 7.0, and I want to use the UIDocumentInteractionController to display the PDF. I used various examples in Stack Overflow to get the correct implementation, but nothing worked.

Here is the stack overflow link that I used for reference.

Open Monotouch Document - UIDocumentInterationController

I also used this objective C tutorial, but could not correctly translate this code into C #. http://code.tutsplus.com/tutorials/previewing-and-opening-documents-with-uidocumentinteractioncontroller--mobile-15130

Here is my code. I currently have a toolbar button that, when clicked, should launch a PDF preview. Nothing happens when I press a button.

What am I missing for this?

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){
        InvokeOnMainThread(delegate{
            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;
    }
}
+4
source share
2 answers

After exploring this problem, asking the same question in another forum and using different code examples, I cannot get this code to work using the UIDocumentInteractionController in C #. However, I can get it to work with the objective-c tutorial related in the above question. This is not useful since I need code in C # instead of objective-c.

Stack Overflow, QLPreviewController.

Monotouch - QLPreviewController

, . PDF, , .

, , PDF , "".

:

QLPreviewController previewController= new QLPreviewController();             

previewController.DataSource=new MyQLPreviewControllerDataSource();     

this.PresentViewController(previewController,true, null);

:

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

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; } 
        } 
    }
+3

-, .

iOS-, .

Uti UIDocumentInteractionController :

documentInteractionController.Uti = @"com.adobe.pdf"

xamarin, .

+1

Source: https://habr.com/ru/post/1527942/


All Articles