重新安装应用程序后,Cordova 应用程序本地存储仍然存在
Cordova app local storage remains after app re-installed
我的 cordova 应用程序有一个登录名 (username/password),一旦通过身份验证,它就会在应用程序文件系统的根目录中创建一个名为 stk.db 的文件。
当我卸载应用程序并重新安装它时,我会自动使用我之前登录的帐户登录(由于正在读取 stk.db 文件并提取 'token'与用户帐户相关联)。
显然,我想让用户在重新安装应用程序时被迫输入新的用户帐户,根据下面的代码,解决此问题的最佳方法是什么?
define([
"jquery",
"backbone",
"config",
"stk/controllers/errorController",
"stk/controllers/accountController",
"stk/controllers/onboardController",
"stk/controllers/stkController"
],
function(
$,
Backbone,
Config,
ErrorController,
AccountController,
OnboardController,
stkController)
{
"use strict";
// --------------------------------------------------------------------------------------------
// Controller
// --------------------------------------------------------------------------------------------
var app = function(router) {
var self = this;
self.router = router;
self.views = {};
self.ctors = {};
self.stack = [
{ id: 0, name : "Account", selected: true, direction: null },
{ id: 1, name : "Achievements", selected: false, direction: null },
{ id: 2, name : "Prizes", selected: false, direction: null },
{ id: 3, name : "Shop", selected: false, direction: null },
{ id: 4, name : "Leaderboard", selected: false, direction: null },
{ id: 5, name : "Stats", selected: false, direction: null },
{ id: 6, name : "Contactus", selected: false, direction: null }
];
// --------------------------------------------------------------------------------------------
// register events
// --------------------------------------------------------------------------------------------
_.extend(this, Backbone.Events);
this.on("error", function (args) { this.error(args); }, this);
this.on("start", function (args) { this.db(args); }, this);
this.on("login:complete", function (args) { this.loginComplete(args); }, this);
this.on("onboard:complete", function (args) { this.onboardComplete(args); }, this);
// --------------------------------------------------------------------------------------------
// event handlers
// --------------------------------------------------------------------------------------------
this.db = function() {
var self = this;
var loadDB = function(callback) {
self.storage(callback);
}
loadDB(function(db) {
var check = {};
$.each(db.split("|"), function() {
var entry = this.split(":");
check[entry[0]] = entry[1];
})
self.start(check);
})
};
this.start = function(db) {
if (db.loggedin != "true") {
this.login();
} else {
this.token = db.token;
if (db.boarded === "true") {
this.launch();
} else {
this.onboard();
}
}
};
this.isCarUser = function (usercb) {
var self = this;
var check = {};
self.storage(function (db) {
$.each(db.split("|"), function () {
var entry = this.split(":");
check[entry[0]] = entry[1];
})
usercb(check.Car_user);
});
};
this.error = function(message) {
var handle = "errorCtor";
(this.ctors[handle] === undefined)
? new ErrorController().setup(handle).events().init(message)
: this.ctors[handle].init(message);
};
this.login = function(args) {
var handle = "accountCtor";
(this.ctors[handle] === undefined)
? new AccountController().setup(handle).events().init(args)
: this.ctors[handle].init(args);
};
this.loginComplete = function(args) {
var self = this;
self.token = args;
var store = function(callback) {
self.setLoggedIn(callback, self.token);
}
store(function(db) {
self.onboard();
});
};
this.onboard = function(args) {
var handle = "onboardCtor";
(this.ctors[handle] === undefined)
? new OnboardController().setup(handle).events().init(args)
: this.ctors[handle].init(args);
};
this.onboardComplete = function(args) {
var self = this;
var store = function(callback) {
self.setBoarded(callback, self.token);
}
store(function() {
self.launch();
});
};
this.launch = function(args) {
var handle = "stkCtor";
(this.ctors[handle] === undefined)
? new stkController().setup(handle).events().init(args)
: this.ctors[handle].init(args);
};
// --------------------------------------------------------------------------------------------
// FS
// --------------------------------------------------------------------------------------------
this.setBoarded = function(callback, token) {
var self = this;
var fc = 'opened:true|loggedin:true|boarded:true||Car_user:false|token:' + token;
var errorHandler = function(e) {
self.error(e);
};
var onInitFs = function(fs) {
fs.root.getFile('stk.db', {}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
callback(fc);
};
fileWriter.onerror = function(e) {
self.error(e.toString());
};
var boarded = new Blob([fc], {type: 'text/plain'});
fileWriter.write(boarded);
}, errorHandler);
});
};
if (window.isDevice) {
window.requestFileSystem(window.PERSISTENT, 512, onInitFs, errorHandler);
} else {
callback(fc);
}
};
this.setLoggedIn = function(callback, token) {
var self = this;
var fc = 'opened:true|loggedin:true|boarded:false|Car_user:false|token:' + token;
var errorHandler = function(e) {
self.error(e);
};
var onInitFs = function(fs) {
fs.root.getFile('stk.db', {}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
callback(fc);
};
fileWriter.onerror = function(e) {
self.error(e.toString());
};
var loggedIn = new Blob([fc], {type: 'text/plain'});
fileWriter.write(loggedIn);
}, errorHandler);
});
};
if (window.isDevice) {
window.requestFileSystem(window.PERSISTENT, 512, onInitFs, errorHandler);
} else {
callback(fc);
}
};
this.setCarUser = function(callback, CarUser){
var self = this;
var fc = 'opened:true|loggedin:true|boarded:true|token:' + this.token + "|Car_user:"+ CarUser;
var errorHandler = function(e) {
self.error(e);
};
var onInitFs = function(fs) {
fs.root.getFile('stk.db', {}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
callback(fc);
};
fileWriter.onerror = function(e) {
self.error(e.toString());
};
var loggedIn = new Blob([fc], {type: 'text/plain'});
fileWriter.write(loggedIn);
}, errorHandler);
});
};
if (window.isDevice) {
window.requestFileSystem(window.PERSISTENT, 512, onInitFs, errorHandler);
} else {
callback(fc);
}
}
this.storage = function(callback) {
var self = this;
var fc = 'opened:true|loggedin:false|boarded:false|token:null|Car_user:false';
var errorHandler = function(e) {
console.log(e);
};
var onInitFs = function(fs) {
fs.root.getFile('stk.db', {}, function(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
callback(this.result);
};
reader.readAsText(file);
}, errorHandler);
}, function() {
fs.root.getFile('stk.db', {create: true, exclusive: true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
callback(fc);
};
fileWriter.onerror = function(e) {
self.error(e.toString());
};
var opened = new Blob([fc], {type: 'text/plain'});
fileWriter.write(opened);
}, errorHandler);
}, errorHandler);
});
};
if (window.isDevice) {
window.requestFileSystem(window.PERSISTENT, 512, onInitFs, errorHandler);
} else {
callback(fc);
}
};
};
return app;
}
);
如果由于某些原因确实需要使用本地文件,可以在本地文件中存储一个版本号,并与index.html中存储的版本号进行比较,如果这两个版本号是不同,删除登录信息。在本地文件中更新版本号。在本地文件中。
顺便告诉您,本地文件 Api 并非在所有手机中都按预期工作。例如有时它会在 Samsung Note 2 中挂起,即既不会触发成功也不会触发失败,但相同的代码在其他手机上运行良好。
我的 cordova 应用程序有一个登录名 (username/password),一旦通过身份验证,它就会在应用程序文件系统的根目录中创建一个名为 stk.db 的文件。
当我卸载应用程序并重新安装它时,我会自动使用我之前登录的帐户登录(由于正在读取 stk.db 文件并提取 'token'与用户帐户相关联)。
显然,我想让用户在重新安装应用程序时被迫输入新的用户帐户,根据下面的代码,解决此问题的最佳方法是什么?
define([
"jquery",
"backbone",
"config",
"stk/controllers/errorController",
"stk/controllers/accountController",
"stk/controllers/onboardController",
"stk/controllers/stkController"
],
function(
$,
Backbone,
Config,
ErrorController,
AccountController,
OnboardController,
stkController)
{
"use strict";
// --------------------------------------------------------------------------------------------
// Controller
// --------------------------------------------------------------------------------------------
var app = function(router) {
var self = this;
self.router = router;
self.views = {};
self.ctors = {};
self.stack = [
{ id: 0, name : "Account", selected: true, direction: null },
{ id: 1, name : "Achievements", selected: false, direction: null },
{ id: 2, name : "Prizes", selected: false, direction: null },
{ id: 3, name : "Shop", selected: false, direction: null },
{ id: 4, name : "Leaderboard", selected: false, direction: null },
{ id: 5, name : "Stats", selected: false, direction: null },
{ id: 6, name : "Contactus", selected: false, direction: null }
];
// --------------------------------------------------------------------------------------------
// register events
// --------------------------------------------------------------------------------------------
_.extend(this, Backbone.Events);
this.on("error", function (args) { this.error(args); }, this);
this.on("start", function (args) { this.db(args); }, this);
this.on("login:complete", function (args) { this.loginComplete(args); }, this);
this.on("onboard:complete", function (args) { this.onboardComplete(args); }, this);
// --------------------------------------------------------------------------------------------
// event handlers
// --------------------------------------------------------------------------------------------
this.db = function() {
var self = this;
var loadDB = function(callback) {
self.storage(callback);
}
loadDB(function(db) {
var check = {};
$.each(db.split("|"), function() {
var entry = this.split(":");
check[entry[0]] = entry[1];
})
self.start(check);
})
};
this.start = function(db) {
if (db.loggedin != "true") {
this.login();
} else {
this.token = db.token;
if (db.boarded === "true") {
this.launch();
} else {
this.onboard();
}
}
};
this.isCarUser = function (usercb) {
var self = this;
var check = {};
self.storage(function (db) {
$.each(db.split("|"), function () {
var entry = this.split(":");
check[entry[0]] = entry[1];
})
usercb(check.Car_user);
});
};
this.error = function(message) {
var handle = "errorCtor";
(this.ctors[handle] === undefined)
? new ErrorController().setup(handle).events().init(message)
: this.ctors[handle].init(message);
};
this.login = function(args) {
var handle = "accountCtor";
(this.ctors[handle] === undefined)
? new AccountController().setup(handle).events().init(args)
: this.ctors[handle].init(args);
};
this.loginComplete = function(args) {
var self = this;
self.token = args;
var store = function(callback) {
self.setLoggedIn(callback, self.token);
}
store(function(db) {
self.onboard();
});
};
this.onboard = function(args) {
var handle = "onboardCtor";
(this.ctors[handle] === undefined)
? new OnboardController().setup(handle).events().init(args)
: this.ctors[handle].init(args);
};
this.onboardComplete = function(args) {
var self = this;
var store = function(callback) {
self.setBoarded(callback, self.token);
}
store(function() {
self.launch();
});
};
this.launch = function(args) {
var handle = "stkCtor";
(this.ctors[handle] === undefined)
? new stkController().setup(handle).events().init(args)
: this.ctors[handle].init(args);
};
// --------------------------------------------------------------------------------------------
// FS
// --------------------------------------------------------------------------------------------
this.setBoarded = function(callback, token) {
var self = this;
var fc = 'opened:true|loggedin:true|boarded:true||Car_user:false|token:' + token;
var errorHandler = function(e) {
self.error(e);
};
var onInitFs = function(fs) {
fs.root.getFile('stk.db', {}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
callback(fc);
};
fileWriter.onerror = function(e) {
self.error(e.toString());
};
var boarded = new Blob([fc], {type: 'text/plain'});
fileWriter.write(boarded);
}, errorHandler);
});
};
if (window.isDevice) {
window.requestFileSystem(window.PERSISTENT, 512, onInitFs, errorHandler);
} else {
callback(fc);
}
};
this.setLoggedIn = function(callback, token) {
var self = this;
var fc = 'opened:true|loggedin:true|boarded:false|Car_user:false|token:' + token;
var errorHandler = function(e) {
self.error(e);
};
var onInitFs = function(fs) {
fs.root.getFile('stk.db', {}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
callback(fc);
};
fileWriter.onerror = function(e) {
self.error(e.toString());
};
var loggedIn = new Blob([fc], {type: 'text/plain'});
fileWriter.write(loggedIn);
}, errorHandler);
});
};
if (window.isDevice) {
window.requestFileSystem(window.PERSISTENT, 512, onInitFs, errorHandler);
} else {
callback(fc);
}
};
this.setCarUser = function(callback, CarUser){
var self = this;
var fc = 'opened:true|loggedin:true|boarded:true|token:' + this.token + "|Car_user:"+ CarUser;
var errorHandler = function(e) {
self.error(e);
};
var onInitFs = function(fs) {
fs.root.getFile('stk.db', {}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
callback(fc);
};
fileWriter.onerror = function(e) {
self.error(e.toString());
};
var loggedIn = new Blob([fc], {type: 'text/plain'});
fileWriter.write(loggedIn);
}, errorHandler);
});
};
if (window.isDevice) {
window.requestFileSystem(window.PERSISTENT, 512, onInitFs, errorHandler);
} else {
callback(fc);
}
}
this.storage = function(callback) {
var self = this;
var fc = 'opened:true|loggedin:false|boarded:false|token:null|Car_user:false';
var errorHandler = function(e) {
console.log(e);
};
var onInitFs = function(fs) {
fs.root.getFile('stk.db', {}, function(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
callback(this.result);
};
reader.readAsText(file);
}, errorHandler);
}, function() {
fs.root.getFile('stk.db', {create: true, exclusive: true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
callback(fc);
};
fileWriter.onerror = function(e) {
self.error(e.toString());
};
var opened = new Blob([fc], {type: 'text/plain'});
fileWriter.write(opened);
}, errorHandler);
}, errorHandler);
});
};
if (window.isDevice) {
window.requestFileSystem(window.PERSISTENT, 512, onInitFs, errorHandler);
} else {
callback(fc);
}
};
};
return app;
}
);
如果由于某些原因确实需要使用本地文件,可以在本地文件中存储一个版本号,并与index.html中存储的版本号进行比较,如果这两个版本号是不同,删除登录信息。在本地文件中更新版本号。在本地文件中。
顺便告诉您,本地文件 Api 并非在所有手机中都按预期工作。例如有时它会在 Samsung Note 2 中挂起,即既不会触发成功也不会触发失败,但相同的代码在其他手机上运行良好。