4

My Cordova app saves settings to a local text file. I'm using the Cordova file plugin to save the file.

When the user re-installs the app over an existing install, I'd like the original settings file to be deleted. Is it possible to do this?

Or is there a way to get a unique key or timestamp from the new installation to create a unique filename for the new settings file?

Fergal
  • 5,213
  • 6
  • 35
  • 44
  • Why don't you just change the name of the file with each install then!! Suppose in first install your file name is file1.txt and you worked with it to save preference, in the next version you can name it to file2.txt and change your code to use file2.txt. Then it'll not use previous preferences. OR you can save a version no with the file. And check after app opens that the version number matched or not. If matched then do nothing, if doesn't match then delete the file, and create new one with new version number. – AtanuCSE Jun 19 '14 at 17:23
  • Thanks, I've considered those options but the problem is 1) what if the user is reinstalling the same version of the app and 2) how can the app know it's running for the first time and that a new settings file should be created? If it were possible to determine that, then I could simply overwrite the existing file on the first time launching. – Fergal Jun 20 '14 at 03:01

1 Answers1

2

So far I've learnt that phonegap doesn't have that way to get the installation date/time. You'll need to write platform dependent code and find a way to call them. You can also write a plugin to communicate between native and cordova.

In android you can get the installation time. LINK

PackageManager pm = context.getPackageManager();
ApplicationInfo appInfo = pm.getApplicationInfo("app.package.name", 0);
String appFile = appInfo.sourceDir;
long installed = new File(appFile).lastModified(); //Epoch Time

In ios you can get the FIRST LAUNCH date

In application didFinishLaunchingWithOptions

NSDate *date =[[NSUserDefaults standardUserDefaults]objectForKey:@"FirstLaunchTime"];
if (date == nil) {
     // nil means your application running for the first time
    [[NSUserDefaults standardUserDefaults]setObject:[NSDate date] forKey:@"FirstLaunchTime"]; // set the current time
}

And you can use them to uniquely identify the preference file.

Community
  • 1
  • 1
AtanuCSE
  • 8,832
  • 14
  • 74
  • 112
  • Thought I'd have to go down this path. Thanks for code snippet; great help. – Fergal Jun 21 '14 at 06:38
  • @Fergal I know this is antique, but would you share your solution? I need to know if my app has been reinstalled as well… finding that Android doesn't always delete localStorage when the app is uninstalled and I need to do that manually if I'm launching a 'fresh' install. Thx – Tawpie Jun 29 '18 at 15:38