sudam
sudam

Reputation: 211

Access files using using Phonegap 3.3.0 for ios

I'm trying to work with files on IOS, using Phonegap[cordova 3.3.0]. I read how to access files and read them on the API Documentation of phone gap. also added plugin like this

  $ cordova plugin add org.apache.cordova.file
    $ cordova plugin ls
    [ 'org.apache.cordova.file' ]
    $ cordova plugin rm org.apache.cordova.file

 $ cordova plugin add org.apache.cordova.file-transfer
    $ cordova plugin ls
    [ 'org.apache.cordova.file',
      'org.apache.cordova.file-transfer' ]
    $ cordova plugin rm org.apache.cordova.file-transfer

function gotFS(fileSystem) is not calling after onDeviceReady() function.

Here's the code I'm using:

       function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
    fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
    fileEntry.file(gotFile, fail);
}

function gotFile(file){
    readDataUrl(file);
    readAsText(file);
}

function readDataUrl(file) {
    var reader = new FileReader();
    reader.onloadend = function(evt) {
        console.log("Read as data URL");
        console.log(evt.target.result);
    };
    reader.readAsDataURL(file);
}

function readAsText(file) {
    var reader = new FileReader();
    reader.onloadend = function(evt) {
        console.log("Read as text");
        console.log(evt.target.result);
    };
    reader.readAsText(file);
}

function fail(evt) {
    console.log(evt.target.error.code);
}

This code is working for android. But for Ios, I am getting ReferenceError: Can't find variable: LocalFileSystem in this line -

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

Upvotes: 3

Views: 8415

Answers (3)

eshcol
eshcol

Reputation: 569

Phonegap 3.3.0's Filesystem has a new approach. If you have been using fullpath for entry, you need to replace that with toURL().

Also in your config.xml file you got to add

<preference name="iosPersistentFileLocation" value="Compatibility" />

Your best bet would be to go over this link https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md

Making these changes worked for me. Hope it works for you too.

Upvotes: 2

sudam
sudam

Reputation: 211

Beside cordova. I also install phonegap like bellow command. and make new application and install all plugin. Now file read write program is working. Thank You for help .

$ sudo npm install -g phonegap 
$ phonegap create my-app 
$ cd my-app 

Upvotes: 0

Ian Clelland
Ian Clelland

Reputation: 44192

If LocalFileSystem isn't defined, it almost certainly means that the plugin's JavaScript code isn't being loaded.

Are you using any other Cordova APIs? Can you tell if cordova.js is being loaded from your HTML page and whether it runs correctly?

On iOS, one of the best debugging techniques for a problem like this is to connect to the iPad (or simulator) with Safari, and run

location.reload()

from the JavaSCript console. If cordova.js encounters an error, then it may stop running before it gets to loading the File plugin.

(FWIW, LocalFileSystem was never supposed to be a real object; it's actually an interface, which window is supposed to implement. I would switch to using window.PERSISTENT for compatibility with the File API spec. That said, Cordova (for backwards compatibility) should be setting the PERSISTENT and TEMPORARY symbols on both window and LocalFileSystem.)

Upvotes: 2

Related Questions