5

I want to open a document on my iphone app written in monotouch - i.e. launch a PDF file in the default PDF viewer.

I think I should be using UIDocumentInterationController?

Anyone have any Ideas on this..

I have put together the following on a viewcontroller ( with a toolbar)

but it doesnt work :-( It does nothing!!

string s = string.Format("{0}",strFilePath);

NSUrl ns = NSUrl.FromFilename (s);   

UIDocumentInteractionController PreviewController =
   UIDocumentInteractionController.FromUrl(ns);
PreviewController.Delegate =  new UIDocumentInteractionControllerDelegateClass();
PreviewController.PresentOpenInMenu(btnOpen,true);

  public class UIDocumentInteractionControllerDelegateClass : UIDocumentInteractionControllerDelegate
                {
                     public UIViewController FileViewController = new UIViewController();
                    public UIDocumentInteractionControllerDelegateClass ()
                    {
                    }

                    public override UIViewController ViewControllerForPreview (UIDocumentInteractionController controller)
                    {
                        return FileViewController;    
                    }

                    public override UIView ViewForPreview (UIDocumentInteractionController controller)
                    {
                        return FileViewController.View;
                    }
                }
Guillaume
  • 12,824
  • 3
  • 40
  • 48
jason clark
  • 829
  • 2
  • 11
  • 23

1 Answers1

6

First thing I would try, is ensuring when you present the options menu, that it is taking place on the main thread:

InvokeOnMainThread(delegate{
    PreviewController.PresentOpenInMenu(btnOpen,true);
});

If that alone doesn't work, another thing I noticed is that you're creating a new view controller in the delegate class. It doesn't appear to be added to the stack anywhere in your code, so maybe thats why it's not showing. Code I've used is as follows:

PreviewController.Delegate = new UIDocumentInteractionControllerDelegateClass(this);

...
...

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

This will then use the current viewcontroller to present the preview in. The only other change I can think of using is rather than presenting from a UIBarButtonItem try:

PreviewController.PresentOpenInMenu(new RectangleF(320,320,0,500), this.View, true);

I hope this helps!

Luke
  • 3,665
  • 1
  • 19
  • 39
  • 2
    Plus I also realised that the simulator wont load any applications during my testing as they dont exist in the emulator - bit of an oversite. – jason clark Dec 30 '10 at 07:31
  • In summary - invoke on main thread worked along with adding the delegate. – jason clark Dec 30 '10 at 07:32
  • 1
    @Luke we tried using your answer on an iPad using Xamarin however it doesn't seem to work. Could you take a look at question number 21913160? Any help would be greatly appreciated! – Robin Feb 20 '14 at 21:26