ioscordovacordova-ioscordova-plugin-file

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


I'm using the cordova-plugin-file to save some data image to the user's 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)
}

for the folderpath, I tried all of these 5 options:

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;

After running the function saveBlobAsImageFile, the log shows the successful writing, but when I go to Files or Library in my simulator/iPhone, I cannot found any saved file.

Update: When I run in simulator, after clicking save, I found the file saved in these folders:

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/

However, when I go to the simulator files and library, these images are not found.

How to make dynamically generated files by the iOS Cordova APP accessible to the end user through the library?


Solution

  • If you add UIFileSharingEnabled, LSSupportsOpeningDocumentsInPlace, UISupportsDocumentBrowser all as YES on plist file, your app files will be visible to iOS Explorer. The main source is here.

    Thus you can edit your config.xml adding these entries inside <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>