How to store data in phone's memory?

Posted by cjohnston on 10-Jun-2013 17:21

Hello,

I'm working on an app that requires a username and password for users to login. I want these credentials to be saved on users' phones, if they want to, so that they don't have to enter them every time they use the app. Any idea how I can do this? I've been searching OE mobile tutorials and docs but I don't seem to have found anything. Any help or guidance would be great, thanks.

All Replies

Posted by John Goodland on 11-Jun-2013 08:35

Hi Cameron,

I'm saving this sort of thing locally in a file. Take a look at this API http://docs.phonegap.com/en/1.7.0/cordova_file_file.md.html.

Cheers

John.

Posted by cjohnston on 11-Jun-2013 12:38

Thanks John! Can you offer any tips on how I would implement these functions in the OE mobile app builder?

Posted by egarcia on 11-Jun-2013 13:39

Hello,

have you tried using a localStorage variable?

There used to be an issue with localStorage and PhoneGap but it should have been fixed.

in the Mobile AppBuilder you can map to a localStorage variable.

Also, you can use ocalStorage from JavaScript.

i hope this helps.

Posted by John Goodland on 11-Jun-2013 13:56

I use localStorage to store transit data where as IP address, user/password, client-principle etc etc goes into a local file. This allows the standard iPhone backup to pick this file up - not so sure on whether localStorage gets backed up. Also, as its my local file I can add encryption where as localStorage is character strings.

Will drop you some examples of both methods in the morning (uk).

cheers

john

Posted by John Goodland on 11-Jun-2013 13:57

LocalStorage issue has been resolved.

Posted by cjohnston on 12-Jun-2013 11:05

Hi,

I am able to use local storage variables or localStorage using JavaScript. What I'm looking to do is save text in a file for the reasons John mentioned. I'll want to encrypt the password and be sure that the data is backed up.

Cheers,

Cameron

Posted by cjohnston on 12-Jun-2013 16:17

Hi John,

Thanks for the tips, any examples you can post would be very helpful.

Cheers,

Cameron

Posted by John Goodland on 13-Jun-2013 02:39

Hi Cameron,

Sorry I didnt get back to you yesterday..... anyhow hope this helps

Cheers

John.

Write file -

function writeFile() {

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

    // Cordova is ready

    //

    function onDeviceReady() {

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

    }

    function gotFS(fileSystem) {

        fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);

    }

    function gotFileEntry(fileEntry) {

        fileEntry.createWriter(gotFileWriter, fail);

    }

    function gotFileWriter(writer) {

        writer.onwriteend = function(evt) {

            alert("contents of file now 'some sample text'");

            writer.truncate(11); 

            writer.onwriteend = function(evt) {

                alert("contents of file now 'some sample'");

                writer.seek(4);

                writer.write(" different text");

                writer.onwriteend = function(evt){

                    alert("contents of file now 'some different text'");

                }

            };

        };

        writer.write("some sample text");

    }

    function fail(error) {

        console.log(error.code);

    }

}

Read file

function readFile() {

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

    // Cordova is ready

    //

    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) {

            alert("Read as text");

            alert(evt.target.result);

        };

        reader.readAsText(file);

    }

    function fail(evt) {

        console.log(evt.target.error.code);

    }

}

Posted by John Goodland on 13-Jun-2013 02:41

One gotcha - it only works when running as an app installed on the iPhone. I've spent many a happy hour debugging this from a webbrowser .

I havent tried Andriod......

Posted by cjohnston on 13-Jun-2013 12:40

Thanks again John, this has been a great help!

Cheers,

Cameron

Posted by Jean Richert on 11-Jul-2013 07:31

Hi all,

Just wanted to let you know we created a KB article in our Support knowledge base so others can benefit of this solution too.

KR article 000041783

Posted by John Goodland on 11-Jul-2013 07:50

Donations to Johns beer fund in Boston this October then?

Posted by Jean Richert on 11-Jul-2013 08:06

Sure...

This thread is closed