How to store data in phone's memory? - Forum - Mobile - Progress Community

How to store data in phone's memory?

 Forum

How to store data in phone's memory?

  • 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.

  • 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.

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

  • 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.

  • 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

  • LocalStorage issue has been resolved.

  • 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

  • Hi John,

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

    Cheers,

    Cameron

  • 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);

        }

    }

  • 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......

  • Thanks again John, this has been a great help!

    Cheers,

    Cameron

  • 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

  • Donations to Johns beer fund in Boston this October then?

  • Sure...