Jeroen Veldhuis
Jeroen Veldhuis

Reputation: 3

Cannot get the File API Working on IOS with build.phonegap.com

I’m having trouble to get the file API working with build.PhoneGap.com on iOS (iPhone). I have searched a lot, but I’m unable to find the reason... I hope someone can point out what I’m doing wrong or can confirm that it is a bug?

What I did:

  1. Took the code example from the phonap documentation

    document.addEventListener("deviceready", onDeviceReady, false);
    
    function onDeviceReady() {
        alert('onDeviceReady')
        try 
        {
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
        }
        catch (err) 
        {
            alert(err);
        }
    }
    
    function gotFS(fileSystem) {
        alert('gotFS')
        fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail);
    }
    
    function gotFileEntry(fileEntry) {
        alert('gotFileEntry')
        fileEntry.file(gotFile, fail);
    }
    
    function gotFile(file) {
        alert('gotFile')
        readDataUrl(file);
        readAsText(file);
    }
    
    function readDataUrl(file) {
        alert('readDataUrl')
        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) {
        alert('readAsText')
        var reader = new FileReader();
        reader.onloadend = function (evt) {
            console.log("Read as text");
            console.log(evt.target.result);
        };
        reader.readAsText(file);
    }
    
    function fail(evt) {
        alert(evt.target.error.code);
    }
    
  2. Added the file and filetransfer feature to the config.xml

    <feature name="File"> <param name="ios-package" value="CDVFile" /> </feature> <feature name="FileTransfer"> <param name="ios-package" value="CDVFileTransfer" /> </feature>

  3. Added the org.apache.cordova.file plugin to the config.xml

    <gap:plugin name="org.apache.cordova.file" version="1.0.1" />

  4. Added the iosPersistentFileLocation preference to the config.xml

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

  5. Zipped both files and uploaded it to my app in build.phonegap.com

  6. Download the app from my iphone en run it

  7. Only alert ‘onDeviceReady’ is shown

Plugin seems to be loaded fine, because the requestFileSystem method is defined. However the gofFS callback is never fired. I have tried both the 3.1.0 and 3.4.0 Cordova version.

Complete package can be downloaded here: https://www.dropbox.com/s/03228h72ygvf5jg/phonegap%20file%20api.zip

Upvotes: 0

Views: 312

Answers (1)

Mike K
Mike K

Reputation: 24

Do not use the "try" and "catch" in onDeviceReady. The fail callback handles errors. This code should run:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    alert('onDeviceReady')

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

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

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

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

function readDataUrl(file) {
    alert('readDataUrl')
    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) {
    alert('readAsText')
    var reader = new FileReader();
    reader.onloadend = function (evt) {
        console.log("Read as text");
        console.log(evt.target.result);
    };
    reader.readAsText(file);
}

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

Upvotes: 0

Related Questions