如何创建文件并将其保存到文件应用程序(iOS 平台)

How to create and save file to Files application (iOS platform)

我需要创建 PDF 文件(已完成)并将其保存到“文件”应用程序,以便用户可以在我的应用程序之外随时访问它。我尝试了 rn-fetch-blobreact-native-fs 包,它们在 Android 上运行良好,但对于 iOS 它们只能创建文件到内部应用程序存储(因此文件不会在外部存储中创建,即不在 iOS 文件应用程序中)。 我有哪些选项可以将我创建的文件保存到文件应用程序?我知道这是可能的,i.g。 Slack 应用程序允许将文件保存到文件应用程序。

您必须在 Info.plist 文件中添加 UISupportsDocumentBrowser 键并设置为 true,或者同时添加 UIFileSharingEnabledLSSupportsOpeningDocumentsInPlace

当用户通过“文件”应用打开您应用的文档目录中的文档时,他们将在原地编辑文档。更改将保存到您应用程序的文档目录中。

参考this了解更多。

我已将新功能添加到 react-native-share 模块以仅在 Files 应用程序中存储文件。它尚未合并,但您可以使用我的分叉版本。 https://github.com/gevorg94/react-native-share/tree/share-files-app.

"react-native-share": "X.X.X"替换为"react-native-share": "git+https://github.com/gevorg94/react-native-share.git#share-files-app",

您可以在 PR 中找到用法 https://github.com/react-native-community/react-native-share/pull/681

你可以尝试用react-native-fs在本地目录下保存和下载

var url = path let fileName = path.split("/").pop() // console.log('fileName', fileName) var ext = this.extention(url); ext = "." + ext[0]; const localFile = ${RNFS.LibraryDirectoryPath}/${fileName}; // console.log('localFile', localFile)

        const options = {
            fromUrl: path,
            toFile: localFile,
            fileCache: true
        };

        if (await RNFS.exists(localFile)) {
            FileViewer.open(localFile, { displayName: this.props.route.params.conversationSubject });
        } else {
            await RNFS.downloadFile(options).promise
                .then((res) => {
                    // console.log('res', res)
                    FileViewer.open(localFile);
                })
                .then((res) => {
                    // success
                    // console.log("success", res);
                })
                .catch(error => {
                    // error
                    console.log("Attachment open error: ", error);
                });
        }