xcode 快速将文档保存到“文件”应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50128462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:38:19  来源:igfitidea点击:

Save document to "Files" App in swift

swiftxcode

提问by ARUN KUMAR

I just created a pdf document in my app and I want to save the document into the device in a way that the "Files" App can access the saved document.

我刚刚在我的应用程序中创建了一个 pdf 文档,我想以“文件”应用程序可以访问保存的文档的方式将文档保存到设备中。

I saw this is in safari when we are downloading a pdf document, there is an option to save the document to the File App, I need just like that

当我们下载 pdf 文档时,我看到这是在 safari 中,有一个选项可以将文档保存到 File App,我需要这样

Googled for several hours but not found any useful methods

谷歌搜索了几个小时,但没有找到任何有用的方法

回答by rbaldwin

If you have access to your PDFdatathen you could present the user with a UIActivityViewControllerwhere they can then save it to Files, or share it with any of the other available options:

如果您有权访问您的,PDFdata那么您可以向用户显示UIActivityViewController他们可以将其保存到的位置Files,或与任何其他可用选项共享:

let activityViewController = UIActivityViewController(activityItems: ["Name To Present to User", pdfData], applicationActivities: nil)
present(activityViewController, animated: true, completion: nil)

enter image description here

在此处输入图片说明

回答by Devbot10

Minimum of iOS 11

最低 iOS 11

The Files app was introduced in iOS 11. You will not be presented with the option to save to Files when you are on a version less than iOS 11. With that being said, documents can be downloaded to a variety of different apps depending on the settings available on the user's phone and the options you allow.

文件应用程序是在 iOS 11 中引入的。当您使用的版本低于 iOS 11 时,您将不会看到保存到文件的选项。话虽如此,文档可以下载到各种不同的应用程序,具体取决于用户手机上可用的设置以及您允许的选项。

@available(iOS 11.0, *)
func savePDF() {
    guard let documentData = document.dataRepresentation() else { return }
    let activityController = UIActivityViewController(activityItems: [documentData], applicationActivities: nil)
    self.present(activityController, animated: true, completion: nil)
}

This is an example of a way to save a document. You can't force it to be downloaded and show up in their Files app. You have to present a UIActivityViewControllerthat shows options on what they can do with that document.

这是保存文档的方法的示例。你不能强制下载它并显示在他们的文件应用程序中。您必须提供一个UIActivityViewController显示他们可以使用该文档做什么的选项。

If you want to exclude any options shown on the UIActivityViewController, then implement excludedActivityTypeslike so.

如果您想排除 上显示的任何选项UIActivityViewController,请excludedActivityTypes像这样实现。

activityController.excludedActivityTypes = [.airDrop, .postToTwitter]

回答by Klein Mioke

Try UIDocumentInteractionController:

尝试UIDocumentInteractionController

let doc = UIDocumentInteractionController(url: url)
doc.presentOptionsMenu(from:in:animated:)

There's an option named save to Files.

有一个名为save to Files.