Using Local Storage in MyEclipse in a PhoneGap App
Local storage allows a mobile app to save and recall data without connecting to a web server or entering data each use. Local storage has many purposes such as saving personal information, settings, and arrays.
*PhoneGap projects require MyEclipse 2015.
In this tutorial, you’ll learn how to:
- Save and recall data
- View local storage data
- Add JQuery Mobile elements into an application
Note: Mobile development was removed in MyEclipse 2016. This tutorial only applies to MyEclipse 2015.
1. Add an Input Element
To use local storage, your app must have an input element in which the user enters information.
- Create a PhoneGap Mobile application project, and open the index.html file found in the www folder.
- In the code section, add a blank line in your source code in the position you want the input element.
- Drag the textinput widget from the widget Palette to the blank line. This opens the Insert Tag window for editing properties.
- Type Your Name: in the Label field.
- Type myNametxt in the ID field, and click Finish.
The code is inserted into the index.html file and reflects the new element.
2. Add a Document Ready Function
Now that you have an input field, create a function that will run when the file is loaded. You can test the function before going further using an alert.
- Add a document ready function inside the head tag.
- Inside the Document Ready function, add a keyup function to bind the text box. This example also contains an alert function for testing.
$( "#myNametxt" ).keyup(function() {alert( "Added letter." );});
The complete code:
When you run the app, an alert pops up verifying the function was called.
3. Test the Project in the Simulator
- Right click the project folder and select PhoneGap>Preview App in Mobile Web Simulator.
- Select a platform for launching the app. The Mobile Web Simulator opens with your project running.
- Enter text into the text box to test the application. The alert box appears indicating the function is working.
- Close the simulator.
4. Add Code to Store Data in Local Storage
The local storage call has two arguments, Key and Value. The Key is the name of the local storage item. Value is the string contained in local storage.
localStorage.setItem(Key, Value);
Set the Key as ‘myName’ and the value with the value of the text box into the keyup function.
localStorage.setItem('myName', $("#myNametxt").val());
5. View Local Storage Data with Developer Tools
- Run the app in the web simulator again, and enter text into the text box.
- Access the Chrome Developer tools by right-clicking the simulator and selecting Inspect Element.
- Select Resources, expand Local Storage, and select the http: option.
- The local storage for the html file appears in the Key and Value columns.
6. Recall the local storage
To recall local storage, use the getItem function. It accepts only a single argument, the Key of the local storage item.
localStorage.getItem('myName')
Assign the local storage key to the text box’s value in the document ready function.
Test the app. On startup, the local storage item is placed into the text box.