如何通过 Cordova iOS APP 动态地使文件 generated/saved 最终用户可以访问?

How to make files dynamically generated/saved by Cordova iOS APP accessible to the end user?

我正在使用 cordova-plugin-file 将一些数据图像保存到用户的 iOS phone。

saveBlobAsImageFile(folderpath, filename, blob) {
  const onError = function(msg) {
    // handles error
  }

  window.resolveLocalFileSystemURL(folderpath, function(dir) {
    console.log("Access to the directory granted");
    dir.getFile(filename, { create: true }, function(file) {
      console.log("File created")
      file.createWriter(function(fileWriter) {
        fileWriter.write(blob);
        console.log("Written file")
      }, onError)
    }, onError)
  }, onError)
}

对于 folderpath,我尝试了所有这 5 个选项:

let folderpath = cordova.file.syncedDataDirectory;
let folderpath1 = cordova.file.documentsDirectory; 
let folderpath2 = cordova.file.dataDirectory;
let folderpath3 = cordova.file.cacheDirectory;
let folderpath4 = cordova.file.tempDirectory;

运行启用函数saveBlobAsImageFile后,日志显示写入成功,但是当我转到simulator/iPhone中的文件或库时,找不到任何保存的文件。

更新:当我在模拟器中运行,点击保存后,我发现文件保存在这些文件夹中:

file:///Users/ngoctuan001/Library/Developer/CoreSimulator/Devices/2964FE69-A5FE-4516-B8F1-C45488DBE2B5/data/Containers/Data/Application/1EF9FF0C-903E-481F-87A0-9A1CEDA4DE5D/Library/Cloud/
file:///Users/ngoctuan001/Library/Developer/CoreSimulator/Devices/2964FE69-A5FE-4516-B8F1-C45488DBE2B5/data/Containers/Data/Application/1EF9FF0C-903E-481F-87A0-9A1CEDA4DE5D/Documents/
file:///Users/ngoctuan001/Library/Developer/CoreSimulator/Devices/2964FE69-A5FE-4516-B8F1-C45488DBE2B5/data/Containers/Data/Application/1EF9FF0C-903E-481F-87A0-9A1CEDA4DE5D/Library/NoCloud/
file:///Users/ngoctuan001/Library/Developer/CoreSimulator/Devices/2964FE69-A5FE-4516-B8F1-C45488DBE2B5/data/Containers/Data/Application/1EF9FF0C-903E-481F-87A0-9A1CEDA4DE5D/Library/Caches/

但是,当我转到模拟器文件和库时,找不到这些图像。

如何使 iOS Cordova APP 动态生成的文件可供最终用户通过库访问?

我改为 cordova-plugin-x-social-sharing 以保存到照片。看来要像android那样去接触iOS的文件系统并不容易。如果有帮助。

如果您在 plist 文件中将 UIFileSharingEnabledLSSupportsOpeningDocumentsInPlaceUISupportsDocumentBrowser 全部添加为 YES,您的应用程序文件将对 iOS Explorer 可见。主要来源是here.

因此您可以编辑 config.xml<platform name="ios">

中添加这些条目
<edit-config target="UIFileSharingEnabled" file="*-Info.plist" mode="merge">
  <true/>
</edit-config>
<edit-config target="LSSupportsOpeningDocumentsInPlace" file="*-Info.plist" mode="merge">
  <true/>
</edit-config>
<edit-config target="UISupportsDocumentBrowser" file="*-Info.plist" mode="merge">
  <true/>
</edit-config>