ManOfWar
ManOfWar

Reputation: 121

Xamarin Forms IOS open PDF

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.

Upvotes: 0

Views: 1474

Answers (1)

Kevin Li
Kevin Li

Reputation: 2258

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.

Upvotes: 1

Related Questions