0

我一直在尝试在 iOS 上保存文件。现在我一直在手机上运行 Xcode。

当我运行该应用程序时,它说已成功保存。但是当我使用 FileApp、FileManager、iCloud 和 Apple crappy Files 应用程序等文件管理器应用程序时,我看不到任何文件。

我的问题是,我从网络搜索中听说,为了保存文件,iOS 为应用程序创建了一个沙盒文件夹。

如果我将它保存到 this.file.documentDirectory,用户如何在 Pages 或 Numbers 应用程序中打开它?(你知道 Apple 的 Word 和 Excel 替代了初学者。)

这是代码。

 writeToIOS(opts : {
    fileName: string,
    text: any
  }) : Promise < IResponse < any >> {
    let response: IResponse < boolean > = {
      success: false,
      error: '',
      data: false
    };
    const options: IWriteOptions = {
      replace: true
    };
    return new Promise((resolve, reject) => {
      const path = this.file.documentsDirectory;
      const directory = 'Attendance Log';
      this
        .file
        .checkDir(path, directory)
        .then(res => {
          this
            .file
            .writeFile(path + directory, opts.fileName, opts.text, options)
            .then(res => {
              response = {
                ...response,
                success: true,
                error: res,
                data: res
              };
              resolve(response);
            })
            .catch(error => reject(error));
        })
        .catch(() => {
          this
            .file
            .createDir(path, directory, true)
            .then(directory => {
              this
                .file
                .writeFile(path + directory.name, opts.fileName, opts.text, options)
                .then(res => {
                  response = {
                    ...response,
                    success: true,
                    error: res,
                    data: res
                  };
                  resolve(response);
                })
                .catch(error => reject(error));
            })
            .catch(error => reject(error));
        });
    });
  }

编辑:

客户/用户应该做的是能够选择文件并在他/她最喜欢的应用程序中打开它。那是一个文件管理器应用程序或一个编写器或电子表格应用程序。

4

2 回答 2

1

我对 iOS 没有太多经验,但我认为你选择了好的目录。

cordova.file.documentsDirectory - 应用程序专用的文件,但对其他应用程序有意义(例如 Office 文件)。请注意,对于 OSX,这是用户的 ~/Documents 目录。(iOS、OSX)

在 iOS 我会搜索/var/mobile/Applications/<UUID>/Documents

于 2018-03-29T21:18:06.427 回答
0

这是将cordova-plugin-fileopener2与离子本机文件打开器结合使用:

const options = {
  replace: true
};
const path = this.file.documentsDirectory;
const directory = 'Attendance Log';
this.file
   .checkDir(path, directory)
      .then(res => {
   this.file
      .writeFile(path + directory, opts.fileName, opts.text, options)
         .then(res => {
   this.fileOpener
      .open(`${path}${directory}/${opts.fileName}` , 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
      .then(() => ...)
      .catch(error => ...);
})
.catch(error => ...);
于 2018-04-12T03:13:14.170 回答