使用Cordova 3.0检查首次启动应用程序

时间:2013-12-03 12:13:02

标签: javascript cordova cordova-3

在Cordova 3.0中是否有办法检查这是第一次运行应用程序而不使用数据库进行此目的。

3 个答案:

答案 0 :(得分:10)

您可以使用localStorage检查变量。尝试这样的事情:

在docummentready事件中:

if(window.localStorage.getItem('has_run') == '') {
    //do some stuff if has not loaded before
    window.localStorage.setItem('has_run', 'true');
}

答案 1 :(得分:1)

道森Loudon的解决方案对我没有用,但试试这个:

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");
}

答案 2 :(得分:-1)

您必须使用sessionStorage而不是localStorage。

正确的代码是:

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");
}

这是因为localStorage是持久的,而sessionStorage是因为..