Cordova: Share created file (cordova-plugin-file to cordova-plugin-x-socialsharing)


#1

This problem is iOS specific, the same exact code works on Android. So I wonder what might be the problem: it appears the file has been created and is shared successfully from the logs, however I cant see the file anywhere.

There are two plugins in play here:
-cordova-plugin-file to create the file
-cordova-plugin-x-share to share the file

//Create .gpx file
//this is inside an event handler...
let blob = new Blob([result], { type: "text/plain;charset=utf-8" });
requestFileSystem(filename + ".gpx", blob)
//end event...

function requestFileSystem(filename, blob) {
  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
    fs.root.getFile(filename, { create: true, exclusive: false }, function(fileEntry) {
      console.log("write file");
      writeFile(fileEntry, blob);
    }, function onErrorCreateFile(error) {});
  }, function onErrorLoadFs(error) {});
}

function writeFile(fileEntry, blob) {
  // Create a FileWriter object for our FileEntry
  fileEntry.createWriter(function(fileWriter) {
    fileWriter.onwriteend = function() {
      readFile(fileEntry);
    };

    fileWriter.onerror = function(error) {};

    // If data object is not passed in,
    // create a new Blob instead.
    fileWriter.write(blob);
  });
}

//In order to share the file, we will read 
//it directly after it has been written
function readFile(fileEntry) {
  fileEntry.file(function(file) {
    var reader = new FileReader();

    reader.onloadend = function() {
      shareFileCordova(fileEntry.nativeURL);
    };

    reader.readAsText(file);

  }, function(error) {
    toastr.error(error);
  });
}

function shareFileCordova(fullPath) {
  console.log(fullPath);
  var options = {
    files: [fullPath],
    chooserTitle: 'Select an app'
  }
  window.plugins.socialsharing.shareWithOptions(options, (result) => {
    console.log(result);
  }, (msg) => {
    toastr.error(msg, "Something went wrong!");
  });
}

Output:

[Log] share file cordova (console-via-logger.js, line 173)
[Log] file:///var/mobile/Containers/Data/Application/ADC2A5F7-920D-46B5-AC18-CD8900B50F85/Library/files/HMO%205%20km%20Oldenzaal.gpx (console-via-logger.js, line 173)
[Log] file:///var/mobile/Containers/Data/Application/ADC2A5F7-920D-46B5-AC18-CD8900B50F85/Library/files/HMO%205%20km%20Oldenzaal.gpx (console-via-logger.js, line 173)
[Log] /local-filesystem/var/mobile/Containers/Data/Application/ADC2A5F7-920D-46B5-AC18-CD8900B50F85/Library/files/HMO%205%20km%20Oldenzaal.gpx (console-via-logger.js, line 173)
[Log] share (console-via-logger.js, line 173)
[Log] {completed: true, app: “com.apple.UIKit.activity.Open.Copy.ph.telegra.Telegraph”} (console-via-logger.js, line 173)

So at the end it says “completed: true”, however nothing is actually send over the Telegram app or any other app if you try.


#2

Bump. Anyone who can help? If I manage to get the solution myself, I’ll post the answer here. However Im not familiar with the iOS filesystem.


#3

Okay, I made some stupid mistake, but the above code actually works for creating a file.

Just one question: how do you import files in iOS with a cordova webview (wkwebview)?


#4

I’m still stuck on this matter. Really hard to bugtest and find a solution with these cordova plugins.


#5

Apparently it has to do with filename. For some idiotic reason this filename is not allowed:

“MyRoutes 23-08-2017 (0.22km)”

Actually anything with a number is not allowed. How come?