Thursday, April 30, 2026
HomeiOS Developmentjavascript - Cordova file plugin change storageLocation for iOs

javascript – Cordova file plugin change storageLocation for iOs

[ad_1]

I’m making an attempt to make use of cordova-plugin-file to avoid wasting a file generated within the app, into the cellphone storage, and contained in the Paperwork or Recordsdata folder if attainable.
The file is getting saved, I can see its path, however I can’t entry its location when I’m on iOs, as a result of it is outdoors the Recordsdata listing. That is my code, and it is working for Android.

I additionally added contained in the config.xml the next:

<desire identify="iosPersistentFileLocation" worth="Compatibility" />

………

const mimeType="software/json";
const filename="check.json";
const information = await this.http.get(`${surroundings.backUrl}/get-data`).toPromise();
const jsonString = JSON.stringify(information, null, 't');
const blob = new Blob([jsonString], { kind: mimeType });

    if (window.cordova && window.cordova?.platformId !== 'browser') {
  doc.addEventListener('deviceready', () => {
    let storageLocation = '';

    if (window.cordova?.platformId === 'android') {
        storageLocation = window.cordova.file.externalRootDirectory + 'Paperwork/';
    }
    if (window.cordova?.platformId === 'ios') {
        storageLocation = window.cordova.file.documentsDirectory;
    }

    (window as any).resolveLocalFileSystemURL(
      storageLocation, (dir) => {
        dir.getFile(
          filename, {create: true},
          (file) => {
            file.createWriter(
              (fileWriter) => {
                fileWriter.write(blob);

                fileWriter.onwriteend = () => {
                  let url = file.toURL(); 
                  alert('File Saved inside: ' + url);
                };

                fileWriter.onerror = (err) => {
                  alert('File not saved');
                  console.error(err);
                };
              }
            );
          }
        );
      }
    );
  });
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments