0
votes

I need to open pdf file from file system. I know how to get path to the file, but my IPhone does not open it.

I use the following code renderer:

using Xamarin.Forms;
using IdokladX.Services;
using UIKit;
using Foundation;
[assembly: Dependency(typeof(IdokladX.iOS.Services.PdfViewer))]
namespace IdokladX.iOS.Services {
public class PdfViewer:IPdfViewer {

    public void OpenPdf(string filename)
    {
        FileWorker fl = new FileWorker();

        if (fl.Exists(filename))
        {
            string strPfad = fl.GetFilePath(filename);

            var viewer = UIDocumentInteractionController.FromUrl(NSUrl.FromFilename(strPfad));
            var controller = GetVisibleViewController();
            viewer.PresentOpenInMenu(controller.View.Frame, controller.View, true);
        }
    }
    private UIViewController GetVisibleViewController(UIViewController controller = null)
    {
        controller = controller ?? UIApplication.SharedApplication.KeyWindow.RootViewController;

        if (controller.PresentedViewController == null)
            return controller;

        if (controller.PresentedViewController is UINavigationController)
        {
            return ((UINavigationController)controller.PresentedViewController).VisibleViewController;
        }

        if (controller.PresentedViewController is UITabBarController)
        {
            return ((UITabBarController)controller.PresentedViewController).SelectedViewController;
        }

        return GetVisibleViewController(controller.PresentedViewController);
    }
    }
}

Here FileWorker is a class which contains methods to save/delete/check file if it exists. What is wrong? I have also added the file type PDF in my Info.plist file.

enter image description here

What is wrong ? Don't advice me to use WebView, I need to open pdf file via another program.

1
Do you mean there's no popup when you trigger PresentOpenInMenu()? Do you have any other app can open the pdf file? - Kevin Li
You can test it on your device which has installed some other pdf viewer apps. - Kevin Li
@Kevin thanks for advice! Sure, I don't have any pdf viewer app on my simulator6 but I have safari... I guess it should open it, am I wrong? - ManOfWar
I test it on my side, safari can't do it. But iBooks and Notes can open pdf. So, use a device which has such apps to test it. - Kevin Li

1 Answers

1
votes

Reference:

See the documentation for presentOpenInMenu(from:in:animated:):

If there are no registered apps that support opening the document, the document interaction controller does not display a menu.

Solution:

Test the "Open in ..." on your device and make sure there's some app can open the pdf file (such as iBooks, Notes, Chrome and so on). Otherwise, the menu will not show.

Notice:

You don't need add the Document Type for PDF, if you just need other apps to open your file. Document Type is used to show your app in the "Open in ..." menu to open other apps' file when some other apps trigger presentOpenInMenu.