【问题标题】:Xamarin iOS: How can I open a pdf file using a standard readerXamarin iOS:如何使用标准阅读器打开 pdf 文件
【发布时间】:2019-01-17 23:10:22
【问题描述】:

我需要使用默认阅读器打开一个 pdf 文件,android 可以,但对于 iOS,我不能。而且我没有扎实的C#经验只有2个月

 public void SaveOpen(string filename, MemoryStream stream)
        {

            string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string filePath = Path.Combine(path, filename);

            //Create a file and write the stream into it.
            FileStream fileStream = File.Open(filePath, FileMode.Create);
            stream.Position = 0;
            stream.CopyTo(fileStream);
            fileStream.Flush();
            fileStream.Close();


            UIViewController currentController = UIApplication.SharedApplication.KeyWindow.RootViewController;
            while (currentController.PresentedViewController != null)
                currentController = currentController.PresentedViewController;
            UIView currentView = currentController.View;

        }

【问题讨论】:

  • 你不能在 webview 上加载它吗?必须是默认应用?
  • 是的,应该是@BrunoCaceiro
  • @BrunoCaceiro já vi que és Portugues e acho que podemos falar portugues。 Sim eu preciso abrir com um leitor padrão, e no android eu consigo baixar um arquivo pdf e abrir logo e já no iOS nao abri。
  • 我们需要说英语。你遇到了什么错误?
  • 我看不到pdf,它没有显示任何错误@Bruno

标签: xamarin.forms xamarin.ios


【解决方案1】:

从 filePath 打开一个 pdf 文件:

  public void OpenPDF(string filePath)
    {
        FileInfo fi = new FileInfo(filePath);

        QLPreviewController previewController = new QLPreviewController();
        previewController.DataSource = new PreviewControllerDataSource(fi.FullName, fi.Name);

        UINavigationController controller = FindNavigationController();
        if (controller != null)
            controller.PresentViewController(previewController, true, null);
    }


    private UINavigationController FindNavigationController()
    {
        foreach (var window in UIApplication.SharedApplication.Windows)
        {
            if (window.RootViewController.NavigationController != null)
            {
                return window.RootViewController.NavigationController;
            }

            var value = CheckSubs(window.RootViewController.ChildViewControllers);
            if (value != null)
                return value;
        }

        return null;
    }


    private UINavigationController CheckSubs(UIViewController[] controllers)
    {
        foreach (var controller in controllers)
        {
            if (controller.NavigationController != null)
            {
                return controller.NavigationController;
            }

            var value = CheckSubs(controller.ChildViewControllers);

            return value;
        }

        return null;
    }

因此,在您的代码中,保存后,只需使用正确的路径调用 OpenPDF。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多