UIDocumentInteractionController

UIDocumentInteractionController

本文介绍了使用 UIActivityViewController 共享所有类型的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 UIActivityViewController 从我的 iOS 应用程序共享文件.我的主要问题是如何处理不同的文件类型.

到目前为止我得到了什么:

图片

public void OpenInExternalApp(string filepath){如果 (!File.Exists(filepath))返回;UIImage uiImage = UIImage.FromFile(filepath);//定义要分享的内容var activityItems = new NSObject[] { uiImage };UIActivity[] applicationActivity = null;var activityController = new UIActivityViewController(activityItems, applicationActivities);if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone){//电话UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(activityController, true, null);}别的{//药片var popup = new UIPopoverController(activityController);UIView 视图 = UIApplication.SharedApplication.KeyWindow.RootViewController.View;CGRect rect = new CGRect(view.Frame.Width/2, view.Frame.Height, 50, 50);popup.PresentFromRect(rect, view, UIPopoverArrowDirection.Any, true);}}

不知道从内存管理方面来说,一次性加载图片是否是个好主意.如果图像太大而无法将其完全保存在 RAM 中,会发生什么情况?例如,请参见此处.

字符串

var activityItems = new NSObject[] { UIActivity.FromObject(new NSString(text)) };

只有文字.

NSUrl

NSUrl url = NSUrl.CreateFileUrl(filepath, false, null);

在大多数情况下,这里会出现相同的应用程序.但例如,PDF 阅读器不会出现在 PDF 文件中.另一边的邮件预览显示的是 Adob​​e Acrobat.

一切

var activityItems = new NSObject[] { NSData.FromFile(filepath) };

最后一种方法的缺点是并非所有应用程序都会显示,例如可以打开 PDF 文件.也适用.

我想使用所有类型的文件.我认为 UIActivity 的子类在这里没有帮助.也许是 UIActivityItemProvider 的子类?

解决方案

我尝试实现 UIActivityItemProvider,但这里再次不是所有应用程序都针对相应的文件类型显示.例如.对于 docx 文档 Word 未显示.

现在我切换到 UIDocumentInteractionController 并且现在有很多可用的应用程序.

UIDocumentInteractionController documentController = new UIDocumentInteractionController();documentController.Url = new NSUrl(filepath, false);string fileExtension = Path.GetExtension(filepath).Substring(1);string uti = UTType.CreatePreferredIdentifier(UTType.TagClassFilenameExtension.ToString(), fileExtension, null);documentController.Uti = uti;UIViewpresentingView = UIApplication.SharedApplication.KeyWindow.RootViewController.View;documentController.PresentOpenInMenu(CGRect.Empty,presentingView,true);

恕我直言,应用程序太多了,因为文件类型 xml 不应该被 PDF 阅读器真正支持,但它确实支持.尽管如此,由于这篇文章,它现在似乎可以工作了::>

通常,如果您要共享图像或网址,则可能需要使用 UIActivityViewController.如果您要共享文档,则可能需要使用 UIDocumentInteractionController.

I want to use UIActivityViewController to share files from my iOS app. The main question for me is how do I handle different file types.

What I'v got so far:

Images

public void OpenInExternalApp(string filepath)
{
    if (!File.Exists(filepath))
        return;

    UIImage uiImage = UIImage.FromFile(filepath);

    // Define the content to share
    var activityItems = new NSObject[] { uiImage };
    UIActivity[] applicationActivities = null;

    var activityController = new UIActivityViewController(activityItems, applicationActivities);

    if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone)
    {
        // Phone
        UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(activityController, true, null);
    }
    else
    {
        // Tablet
        var popup = new UIPopoverController(activityController);
        UIView view = UIApplication.SharedApplication.KeyWindow.RootViewController.View;
        CGRect rect = new CGRect(view.Frame.Width/2, view.Frame.Height, 50, 50);
        popup.PresentFromRect(rect, view, UIPopoverArrowDirection.Any, true);
    }
}

Don't know if from the memory management aspect it is a good idea to load the image at once. What will happen if the image is too big for holding it completely in RAM? See here for example.

Strings

var activityItems = new NSObject[] { UIActivity.FromObject(new NSString(text)) };

Only text.

NSUrl

NSUrl url = NSUrl.CreateFileUrl(filepath, false, null);

Here in most cases the same app appear. But for example the PDF reader doesn't appear for a PDF file. The preview in mail on the other side shows Adobe Acrobat.

Everything

var activityItems = new NSObject[] { NSData.FromFile(filepath) };

The last approach has the disadvantage that not all apps are displayed, which for example could open a PDF file. Also this applies.

I want to use all types of files. I don't think a subclass of UIActivity would help here. Perhaps a sublcass of UIActivityItemProvider?

解决方案

I tried to implement UIActivityItemProvider, but here again not all apps where shown for the corresponding filetype. E.g. for a docx-document Word was not shown.

Now I switched to UIDocumentInteractionController and now there are many apps available.

UIDocumentInteractionController documentController = new UIDocumentInteractionController();
documentController.Url = new NSUrl(filepath, false);
string fileExtension = Path.GetExtension(filepath).Substring(1);
string uti = UTType.CreatePreferredIdentifier(UTType.TagClassFilenameExtension.ToString(), fileExtension, null);
documentController.Uti = uti;

UIView presentingView = UIApplication.SharedApplication.KeyWindow.RootViewController.View;
documentController.PresentOpenInMenu(CGRect.Empty, presentingView, true);

Imho there are too many apps, because the file type xml should not be really be supported by a PDF reader, but it is. Nevertheless, it seems to work now thanks to this post:

这篇关于使用 UIActivityViewController 共享所有类型的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 11:24