Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocalStorage doesn't work on PhoneGap

I create a example of LocalStorage. When I use Chrome web browser, It works fine. But when I test on iPhone Emulator, It doesn't. Please help. Here is my code to set the local storage value:

function onclick(){
    window.localStorage.setItem("data","Nguyen Minh Binh");
}

===========

    <!DOCTYPE html>
<html>
    <head>
        <title>Page Title</title>

        <meta name="viewport" content="width=device-width, initial-scale=1">
            <script src="cordova-2.5.0.js"></script>

            <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.css" />
            <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
            <script src="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.js"></script>
            <script src="js/index.js" type="text/javascript"    ></script>
    </head> 

    <body> 
        <a data-role="button" href="DemoWOrklightJQM/index.html"  data-prefetch onclick="onclick()">Click me</a>
    </body>
</html>

Here is the code where I tried to get the saved data:

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="UTF-8">
            <title>DemoSimpleControls</title>
            <meta name="viewport"
                content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
                <link rel="shortcut icon" href="images/favicon.png">
                    <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
                        <link href="jqueryMobile/jquery.mobile-1.3.0.css" rel="stylesheet">
                            <link rel="stylesheet" href="css/DemoSimpleControls.css">
                                <script>
                                    window.$ = window.jQuery = WLJQ;
                                    </script>
                                <script src="jqueryMobile/jquery.mobile-1.3.0.js"></script>
                                <script src="../js/jquery-1.9.1.min.js"></script>
                                <script>
                                    $(document).ready(function(){
                                                      $("#mysavedData").html("XYZ");
                                                      $("#mysavedData").html(window.localStorage.getItem("data"));
                                                      });

                                </script>
                                </head>

    <body id="content" >
        <div data-role="page" id="page">
            <div data-role="header" >
                <a data-rel="back" href="#" >Back</a>
                <h1>My page</h1>

            </div>
            <div data-role="content" style="padding: 15px" data-theme="e">
                <div id="mysavedData">My data</div>

                        <a href="#" onclick="getData(); return false;" data-role="button" id="button" data-theme="a" data-icon="home" data-iconpos="left" data-corners="true">Button</a>
            </div>

        </div>
    </body>
</html>

EDIT: At Whizkid747's suggest, I change the script source as below but It still doesn't work.

        <!DOCTYPE HTML>
    <html>
        <head>
            <meta charset="UTF-8">
                <title>DemoSimpleControls</title>
                <meta name="

viewport"
                content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
                <link rel="shortcut icon" href="images/favicon.png">
                    <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
                        <link href="jqueryMobile/jquery.mobile-1.3.0.css" rel="stylesheet">
                            <link rel="stylesheet" href="css/DemoSimpleControls.css">

                                <script src="jqueryMobile/jquery.mobile-1.3.0.js"></script>
                                <script src="../js/jquery-1.9.1.min.js"></script>
                                < script  type="text/javascript" charset="utf-8" src="../cordova-2.5.0.js"></script>
                                <script type="text/javascript" charset="utf-8">
                                    function onLoad(){
                                        document.addEventListener("deviceready", deviceready, false);
                                    }
                                    function deviceready(){
                                        $("#mysavedData").html("XYZ");
                                        $("#mysavedData").html(window.localStorage.getItem("data"));
                                    }

                                </script>
                                </head>

    <body id="content" onLoad="onLoad();" >
        <div data-role="page" id="page">
            <div data-role="header" >
                <a data-rel="back" href="#" >Back</a>
                <h1>My page</h1>

            </div>
            <div data-role="content" style="padding: 15px" data-theme="e">
                <div id="mysavedData">My data</div>

                        <a href="#" onclick="getData(); return false;" data-role="button" id="button" data-theme="a" data-icon="home" data-iconpos="left" data-corners="true">Button</a>
            </div>

        </div>
    </body>
</html>

EDIT2: Try to set localStorage at OnDeviceReady then get it next lines, but still doesn't work.

<!DOCTYPE html>
<html>
    <head>
        <title>Contact Example</title>
        <script src="js/jquery-1.9.1.min.js">
            </script>
        <script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
        <script src="https://raw.github.com/phonegap/phonegap/2.5.0rc1/lib/android/cordova-2.5.0rc1.js"></script>
        <script type="text/javascript" charset="utf-8">

            // Wait for PhoneGap to load
            //
            document.addEventListener("deviceready", onDeviceReady, false);

            // PhoneGap is ready
            //
            function onDeviceReady() {
                window.localStorage.setItem("key", "minhbinh");
                var keyname = window.localStorage.key(i);
                // keyname is now equal to "key"
                var value = window.localStorage.getItem("key");
                // value is now equal to "value"
                window.localStorage.removeItem("key");
                window.localStorage.clear();
                // localStorage is now empty
                $("p#p1").text(value);
            }


            </script>
    </head>
    <body>
        <h1>Example</h1>
        <p id="p1">localStorage</p>
    </body>
</html>
like image 545
Nguyen Minh Binh Avatar asked Mar 24 '13 15:03

Nguyen Minh Binh


People also ask

What are localStorage and sessionStorage objects in PhoneGap?

If you are already familiar with HTML5, you might have used localStorage and sessionStorage objects. As PhoneGap uses HTML5, you can use these same objects for your data storage purposes. You can store data permanently on your mobile device with localStorage. You can store data for a particular session on your mobile device with sessionStorage.

How do I store data in PhoneGap?

As PhoneGap uses HTML5, you can use these same objects for your data storage purposes. You can store data permanently on your mobile device with localStorage. You can store data for a particular session on your mobile device with sessionStorage. If both the above methods are not suitable for your data needs, you can store data on a server.

What is localStorage and how to use it?

If you want to store data permanently on your device without an expiry, then localStorage is the object you need to use. The data stored will be available even if you close your application and try to get it even a year after. We store data as key/value pairs in localStorage.

How do I access local storage data in PHP?

varsomeSetting= localStorage.getItem('someSetting'); This is the native way to set and retrieve local storage data, but some frameworks have their own inbuilt ways to access local storage. One important thing to keep in mind when using local storage is that it can only store strings.


2 Answers

Move your code in document.ready to onDeviceReady event of PhoneGap. onDeviceReady is when you need to start executing your custom code.

Edit: *Try the below code with cordova.js added locally and not from github*

<!DOCTYPE html>
<html>
    <head>
        <title>Contact Example</title>
        <script src="js/jquery-1.9.1.min.js">
            </script>
        <script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
        <script src="cordova-2.5.0rc1.js"></script>

    </head>
    <body>
        <h1>Example</h1>
        <p id="p1">localStorage</p>
        <script type="text/javascript">

            // Wait for PhoneGap to load

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

            // PhoneGap is ready

            function onDeviceReady() {
                window.localStorage.setItem("key", "minhbinh");
                var keyname = window.localStorage.key(i);
                // keyname is now equal to "key"
                var value = window.localStorage.getItem("key");
                // value is now equal to "value"
                //window.localStorage.removeItem("key");
                //window.localStorage.clear();
                // localStorage is now empty
                $("p#p1").text(value);
            }


         </script>
    </body>
</html>
like image 141
Nishanth Nair Avatar answered Nov 05 '22 18:11

Nishanth Nair


There was a problem when running Cordova 2.1 (fixed in 2.2) on iOS 6: initializing the localstorage the first time the app is run does not work.

Please check this for a solution: http://www.x-services.nl/cordova-localstorage-cleared-after-first-app-launch-ios6/369

like image 39
Sjoerd Huitema Avatar answered Nov 05 '22 20:11

Sjoerd Huitema