×
Namespaces

Variants
Actions
(Difference between revisions)

Using widget.preferences in Nokia Asha Web Apps

From Nokia Developer Wiki
Jump to: navigation, search
isalento (Talk | contribs)
(Isalento -)
isalento (Talk | contribs)
(Isalento - - Troubleshooting)
Line 101: Line 101:
 
* Preferences are lost after reloading the web app
 
* Preferences are lost after reloading the web app
 
** Make sure that you have declared preference in th <tt>config.xml</tt>
 
** Make sure that you have declared preference in th <tt>config.xml</tt>
 +
** Make sure that key and value are under 256 character limit.
  
 
*Preferences does not work in local simulator preview Series 40 Web Apps version 1.5
 
*Preferences does not work in local simulator preview Series 40 Web Apps version 1.5
 
** Use bracket access <tt>widget.preferences["key"]</tt> instead of <tt>getItem() </tt>/<tt>setItem()</tt> syntax.
 
** Use bracket access <tt>widget.preferences["key"]</tt> instead of <tt>getItem() </tt>/<tt>setItem()</tt> syntax.
 
*Preferences are lost after 30 minutes of incativity
 
** Make sure that key and value are under 256 character limit.
 

Revision as of 14:17, 10 April 2012

This article explains how to store data using widget.preferences in Series 40 Web Apps

Article Metadata
Tested with
Devices(s): Nokia X3-02
Compatibility
Platform(s): Series 40 Web Apps version 1.5
Series 40
Device(s): All Series 40 devices supporting Web Apps
Article
Keywords: widget.preferences
Created: isalento (12 Mar 2012)
Last edited: isalento (10 Apr 2012)

Contents

Introduction

Widget preference allows developer to store data in key-value pairs. Series 40 Web App environment preserves stored data, and makes it available when Web App is started. Data stored into widget storage area is unique for Web App instance, i.e. preferences cannot be used to share data between two Web Apps.


How to use

Declaring preference

Preference key must always be declared in config.xml file, in Series 40 Web Apps, prior to use. Failing to do so does not cause immediate error, but values stored are not preserved and lost when Web App is re-started.

Declaring preference in config.xml:

<preference name="apikey" 
value="f1934gdf4tg334"
readonly="true"/>

readonly is an optional argument, thus can be omitted. If preference has read-only flag, scripts cannot change its value nor delete it.

Accessing preferences

Once preferences are declared you can use setItem() and getItem() or bracket access to modify stored values. Please note that in current Web tools (v 1.5), only bracket access is supported in local simulator preview. There is a size limit for stored preference. Stored string should not be longer than 256 characters, same applies for the key.

//set value 
widget.preferences["key"] ="value";
 
//get value, returns “undefined” if there is no match for the key
var value = widget.preferences["key"];

If you don’t need to use local simulator preview, setItem() and getItem() can be used to access stored preferences, as defined in Storage interface [1]. Please note that NO_MODIFICATION_ALLOWED_ERR exception is not thrown in series 40 Web Apps, when trying to modify readonly preference.

//set value
widget.preferences.setItem("key","value");
 
//get value, returns “null” if there is no match for the key
widget.preferences.getItem("key");

Advanced usage

Among setItem and getItem, Storage interface offers following methods and attributes.

  • length
    • Total number of key/value pairs
  • key(n)
    • Returns name of the n:th key in the list
  • removeItem("key")
    • Removes key/value pair.
  • clear()
    • Removes all stored key/value pairs.

Note that in Series 40 Web Apps, removeItem(key) and clear() removes preferences event they would be defined as readonly.

//total number of stored items
var count = widget.preferences.length;
 
//key string for first item in the list.
//Note that order can change when new key/value pairs are added
var keyString = widget.preferences.key(0);
 
//Removes item, with “key” from preferences
widget.preferences.removeItem("key");
 
//Clears ALL stored preferences.
widget.preferences.clear();


Troubleshooting

  • Web App preferences are not stored correctly after deployment to device
    • In some cases short URL deployment causes problems in Series 40 Web Apps version 1.5. Please use Bluetooth deployment when possible.
  • Preferences are lost after reloading the web app
    • Make sure that you have declared preference in th config.xml
    • Make sure that key and value are under 256 character limit.
  • Preferences does not work in local simulator preview Series 40 Web Apps version 1.5
    • Use bracket access widget.preferences["key"] instead of getItem() /setItem() syntax.
175 page views in the last 30 days.
×