SessionA142(2015)
DATA PERSISTENCE
LOCALSTORAGE
ahmadsuki@gmail.com
STIJ6044MobileApplicationProgramming
SessionA142(2015)
COOKIES
Invented early in Webs history as a way to store
persistent data (magic cookies)
Small pieces of information about a user stored by
Web server as text files on users computer
Can be temporary or persistent
ahmadsuki@gmail.com
STIJ6044MobileApplicationProgramming
SessionA142(2015)
COOKIES
Included with every HTTP request slows down
application by transmitting same information
repeatedly
Sends unencrypted data over internet with every
HTTP request
Limited to 4KB data
Example: filling out a text form field
ahmadsuki@gmail.com
STIJ6044MobileApplicationProgramming
SessionA142(2015)
HISTORY
IE: DHTML Behaviors
userData behavior allowed 64K per domain
Hierarchical XML-based structure
Adobe Flash (2002)
Flash cookies or Local Shared Objects
Allows Flash objects to store 100K data per
domain temporarily
HISTORY
AMASS (AJAX Massive Storage System)
Brad Neuberg
Flash-to-JavaScript bridge
Limited by Flash design quirks
AMASS rewritten
Integrated into Dojo Toolkit: dojox.storage
100KB storage
Prompts user for exponentially increased storage
ahmadsuki@gmail.com
STIJ6044MobileApplicationProgramming
SessionA142(2015)
HISTORY
Google: Gears (2007)
Open source browser plug-in
Provides additional capability in browsers (geolocation API
in IE)
API to embedded SQL database
Unlimited data per domain in SQL database tables
ahmadsuki@gmail.com
STIJ6044MobileApplicationProgramming
SessionA142(2015)
HTML5 STORAGE
IE
8+
ahmadsuki@gmail.com
Firefox
3.5+
Safari
4.0+
Chrome
4.0+
Opera IPhone
10.5+ 2.0+
Android
2.0+
STIJ6044MobileApplicationProgramming
SessionA142(2015)
OR USE MODERNIZR
if (Modernizr.localstorage) {
// window.localStorage is available!
} else {
// no native support for HTML5 storage :(
// maybe try dojox.storage or a third-party
solution
}
ahmadsuki@gmail.com
STIJ6044MobileApplicationProgramming
SessionA142(2015)
localstorage object
.setItem( )
.getItem( )
.removeItem( )
.clear( )
ahmadsuki@gmail.com
STIJ6044MobileApplicationProgramming
SessionA142(2015)
TYPE
key
string
DESCRIPTION
the named key that was added, removed, or modified
oldValue
any
newValue
any
url*
ahmadsuki@gmail.com
string
STIJ6044MobileApplicationProgramming
SessionA142(2015)
HTML5 IN ACTION
ahmadsuki@gmail.com
10
STIJ6044MobileApplicationProgramming
SessionA142(2015)
HTML5 IN ACTION
function saveGameState() {
if (!supportsLocalStorage()) { return false; }
localStorage["halma.game.in.progress"] = gGameInProgress;
for (var i = 0; i < kNumPieces; i++) {
localStorage["halma.piece." + i + ".row"] = gPieces[i].row;
localStorage["halma.piece." + i + ".column"] = gPieces[i].column;
}
localStorage["halma.selectedpiece"] = gSelectedPieceIndex;
localStorage["halma.selectedpiecehasmoved"] =
gSelectedPieceHasMoved;
localStorage["halma.movecount"] = gMoveCount;
return true;
}
function resumeGame() {
if (!supportsLocalStorage()) { return false; }
gGameInProgress = (localStorage["halma.game.in.progress"] == "true");
if (!gGameInProgress) { return false; }
gPieces = new Array(kNumPieces);
for (var i = 0; i < kNumPieces; i++) {
var row = parseInt(localStorage["halma.piece." + i + ".row"]);
var column = parseInt(localStorage["halma.piece." + i + ".column"]);
gPieces[i] = new Cell(row, column);
}
gNumPieces = kNumPieces;
gSelectedPieceIndex = parseInt(localStorage["halma.selectedpiece"]);
gSelectedPieceHasMoved = localStorage["halma.selectedpiecehasmoved"]
== "true";
gMoveCount = parseInt(localStorage["halma.movecount"]);
drawBoard();
return true;
}
ahmadsuki@gmail.com
11
STIJ6044MobileApplicationProgramming
SessionA142(2015)
HTML5 IN ACTION
In the saveGameState() function, we did not worry
about the data type:
localStorage["halma.game.in.progress"] =
gGameInProgress;
HTML5 IN ACTION
But in the resumeGame() function, we need to treat
the value we got from the local storage area as a
string and manually construct the proper Boolean
value ourselves:
gGameInProgress =
(localStorage["halma.game.in.progress"] == "true");
ahmadsuki@gmail.com
12
STIJ6044MobileApplicationProgramming
SessionA142(2015)
HTML5 IN ACTION
Similarly, the number of moves is stored in
gMoveCount as an integer. In the saveGameState()
function, we just stored it:
gMoveCount =
parseInt(localStorage["halma.movecount"]);
HTML5 IN ACTION
But in the resumeGame() function, we need to
coerce the value to an integer, using the parseInt()
function built into JavaScript:
gMoveCount =
parseInt(localStorage["halma.movecount"]);
ahmadsuki@gmail.com
13
STIJ6044MobileApplicationProgramming
SessionA142(2015)
ahmadsuki@gmail.com
14
STIJ6044MobileApplicationProgramming
SessionA142(2015)
Chrome Opera
4.0+
10.5+
Mobile
Safari
3.0+
Android
2.0+
SQL-92
Oracle SQL
Microsoft
SQL
MySQL
PostgreSQL
SQLite SQL
ahmadsuki@gmail.com
15
STIJ6044MobileApplicationProgramming
SessionA142(2015)
ahmadsuki@gmail.com
16
STIJ6044MobileApplicationProgramming
SessionA142(2015)
LOCALSTORAGE
IN ACTION
ahmadsuki@gmail.com
17
STIJ6044MobileApplicationProgramming
SessionA142(2015)
SETTING ITEMS
By using setItem() method,
Store data in key/ value format (or name/ value)
pair as an argument.
Example:
localStorage.setItem(name, Ahmad Suki);
localStorage.setItem(umur, 30);
localStorage.setItem(point, 100);
ahmadsuki@gmail.com
18
STIJ6044MobileApplicationProgramming
SessionA142(2015)
ahmadsuki@gmail.com
19
STIJ6044MobileApplicationProgramming
SessionA142(2015)
GETTING ITEMS
By using getItem() method,
Store data in key/ value format (or name/ value)
pair as an argument.
Example:
localStorage.getItem(name);
@
localStorage.name;
TRY THIS
ahmadsuki@gmail.com
20
STIJ6044MobileApplicationProgramming
SessionA142(2015)
Amount (withdrawal)
Note RM50 (how many)
Note RM20
Note RM10
ahmadsuki@gmail.com
Withdrawal Amount
Note RM100
Note RM20
Note RM5
21
STIJ6044MobileApplicationProgramming
SessionA142(2015)
ahmadsuki@gmail.com
22
STIJ6044MobileApplicationProgramming
SessionA142(2015)
Retrieve the
stored values
Test the
value
Display the
username &
password form
**probably through
Intern
Store the
items in the
device
ahmadsuki@gmail.com
23
STIJ6044MobileApplicationProgramming
SessionA142(2015)
TRY THIS:
**Request
again
**not match
**match
Start the real
app
TRY THIS:
**consider to apply
Intent
Get username
& password
Compare users
username&pass
Wd with
sharePrefs
**match
Start your
BMI/ATM/etc app
ahmadsuki@gmail.com
**Request
again
**not match
**Youre advised to plan
your screens movement
**sketch yr screen & draft its
flow.
24
STIJ6044MobileApplicationProgramming
SessionA142(2015)
TRY THIS:
CHILDREN MATH APP - I
The questions are randomly
generated.
TRY THIS:
CHILDREN MATH APP - I
Start
Generate
random numbers
User answer
the questions
User press
button
Button Next
Button Check
Implement
evaluate user
answer
ahmadsuki@gmail.com
Implement
Generate
random
numbers
25