【发布时间】:2017-11-09 13:48:45
【问题描述】:
我需要从文件系统打开 pdf 文件。 我知道如何获取文件的路径,但我的 iPhone 打不开它。
我使用以下代码渲染器:
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);
}
}
}
这里的FileWorker 是一个包含保存/删除/检查文件(如果存在)的方法的类。
怎么了?我还在 Info.plist 文件中添加了文件类型 PDF。
怎么了?不要建议我使用 WebView,我需要通过其他程序打开 pdf 文件。
【问题讨论】:
-
你的意思是触发
PresentOpenInMenu()时没有弹窗?有没有其他app可以打开pdf文件? -
您可以在安装了其他一些pdf查看器应用程序的设备上进行测试。
-
@Kevin 感谢您的建议!当然,我的模拟器 6 上没有任何 pdf 查看器应用程序,但我有 safari……我想它应该打开它,我错了吗?
-
我自己测试了一下,safari 做不到。但 iBooks 和 Notes 可以打开 pdf。因此,请使用具有此类应用程序的设备进行测试。
标签: c# ios pdf xamarin xamarin.forms