Open PDF in iBooks using UIActivityViewController

2019-08-09 16:37发布

问题:

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);
            }

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?

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);

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?

Thanks in advance

EDIT:

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);

EDIT:

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

回答1:

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

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.

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) };

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).