使用 UIActivityViewController 和 UIDocumentInteractionController 在 iBooks 中打开 PDF [英] Open PDF in iBooks using UIActivityViewController and UIDocumentInteractionController

查看:31
本文介绍了使用 UIActivityViewController 和 UIDocumentInteractionController 在 iBooks 中打开 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个应用程序来查看 PDF 并将其保存在 iBooks 中、邮寄并打印.现在我正在使用以下代码:

I am trying to make an app to view a PDF and save it in iBooks, mail it and print it. Right now I am using the following code:

            if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0))
            {
                AppDelegate appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
                 //url to local PDF
                var items = new NSObject[] { NSData.FromFile(_pdfUrl) };
                var activityController = new UIActivityViewController(items, null);
                activityController.ExcludedActivityTypes = new NSString[]
                { 
                    UIActivityType.AssignToContact, 
                    UIActivityType.Message,
                    UIActivityType.PostToFacebook,
                    UIActivityType.PostToTwitter,
                    UIActivityType.PostToWeibo,
                    UIActivityType.CopyToPasteboard,
                    UIActivityType.SaveToCameraRoll
                };

                PresentViewController(activityController, true, null);
            }

当我按下按钮来执行这段代码时,我看到了视图控制器,但只有邮件和打印(在带有 iBooks 的设备上测试).我的问题是:在 iBooks 中打开"是我必须自己添加的内容吗?还是我做错了什么?

When I press on the button to perform this code, I see the viewcontroller, but only with Mail and Print(Tested on a device with iBooks). My Question is: Is "Open in iBooks" something that I will have to add myself? Or am I doing something wrong?

另一个问题是在 iOS5 中做同样的事情时,我使用 UIDocumentInteractionController:

Another Question is when doing the same in iOS5, I use UIDocumentInteractionController:

                UIDocumentInteractionController doc = UIDocumentInteractionController.FromUrl(new NSUrl(_pdfUrl, false));
                doc.WeakDelegate = this;
                doc.PresentOptionsMenu(btn.Frame, this, true);

但什么也没发生(iPhone 模拟器,没有装有 iOS 5 的设备).在另一篇文章中,有人说如果设备上没有 iBooks,什么都不会发生.但是邮件和打印呢?

But nothing happened (iPhone Simulator, don't have a device with iOS 5). In another post someone said that if iBooks isn't on the device nothing will happen. But what about Mail and Print?

有人可以帮我解决这个问题吗?

Can someone help me out on this?

提前致谢

在 iOS 6 中使用此代码可以完美运行:

using this code in iOS 6 works perfectly:

            UIDocumentInteractionController doc = UIDocumentInteractionController.FromUrl(new NSUrl(_pdfUrl, false));
            doc.WeakDelegate = this;
            doc.PresentOptionsMenu(btn.Frame, this, true);

我正在寻找一种使用 UIDocumentInteractionController 在 iOS5 上显示邮件和打印的方法.任何人有什么想法吗?

I am looking for a way to show Mail and Print on iOS5 using UIDocumentInteractionController. Any ideas anyone?

推荐答案

那是因为 iBooks 没有内置的 UIActivityType 常量.

That's because there are no built-in UIActivityType constant for iBooks.

但是,创建自己的 UIActivity 提供程序非常容易.我在 C# 中为 AirPlay 制作了一个(可在 github) 您可以将其用作您的基础.

However it's quite easy to create your own UIActivity providers. I made one in C# for AirPlay (available on github) that you can use as the basis for yours.

首先要检测 iBook 是否可用(覆盖 CanPerform).似乎您可以使用ibooks://"前缀来检查这一点(因此应该更容易).接下来是打开文档,然后提供一个图标.

The first thing is to detect if iBook is available (to override CanPerform). It seems that you can use the `"ibooks://" prefix to check for this (so it should be easier). Next is to open the document and then provide an icon.

请注意,我建议您避免这样做:

Note that I suggest you to avoid doing this:

var items = new NSObject[] { NSData.FromFile(_pdfUrl) };

并且仅使用 URL,因为您不知道 PDF 可能有多大(它可能大于您设备的可用内存).

and only work with the URL since you do not know how big the PDF could be (it could be larger than the available memory of your device).

这篇关于使用 UIActivityViewController 和 UIDocumentInteractionController 在 iBooks 中打开 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆