Checking first time app launch using Cordova 3.0
Asked Answered
G

3

7

Is there a way in Cordova 3.0 to check if that's the first time the application runs without using the DB for that purpose.

Gum answered 3/12, 2013 at 12:13 Comment(2)
you could use a localStorage variablePammie
write an answer and I will acceptGum
P
10

You could use localStorage to check for a variable. Try something like this:

in docummentready event:

if(window.localStorage.getItem('has_run') == '') {
    //do some stuff if has not loaded before
    window.localStorage.setItem('has_run', 'true');
}
Pammie answered 3/12, 2013 at 19:0 Comment(1)
What about the data expiration from user agent?Christmann
W
1

Dawson Loudon's solution didn't work for me but try this:

var count = window.localStorage.getItem('hasRun');

if(count){
   console.log("second time app launch");
}else{
  // set variable in localstore
  window.localStorage.setItem('hasRun',1);
  console.log("first time app launch");
}
Wendolyn answered 28/8, 2015 at 19:21 Comment(0)
M
-2

You must use sessionStorage instead of localStorage.

The correct code will be:

var count = window.sessionStorage.getItem('hasRun');

if (count) {
  console.log("second time app launch");
} else {
  // set variable in localstore
  window.sessionStorage.setItem('hasRun', 1);
  console.log("first time app launch");
}

It's because localStorage is persistent while sessionStorage ain't..

Midbrain answered 4/1, 2016 at 17:29 Comment(1)
That's why you want to use localStorage, it has to be persistent otherwise the data could be removed, and it looks like your app is launching for the first time.Zoniazoning

© 2022 - 2024 — McMap. All rights reserved.