Phonegap - create a .txt file on first boot

I am creating a phonegap application and you need to create a new .txt file on first boot. After that, I need to check if the file exists, and then ignore the creation, if so, below is the general stream following:

1 - onDeviceReady - downloading the phoengap application 2 - Check if the readme.txt file exists (if so, download the home page) 3 - Create the readme.txt file and add it to the www folder 4 - Continue loading the home page

EDIT. Instead of the correct answer mentioned below, I decided to use HTML5 local storage, as it was just one line of code.

localStorage.setItem("name", "Email Supplied!"); 

and can be checked with this simple if statement

  if (localStorage.getItem("name") == "Email Supplied!") { // What you want to happen here } 
+4
source share
2 answers

You can see the full example here:

http://docs.phonegap.com/en/1.4.1/phonegap_file_file.md.html#FileWriter

This line creates a file if it does not exist:

 fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail); 

Supported Platforms

Android BlackBerry WebWorks (OS 5.0 and higher) iOS Windows Phone 7 (Mango)

I do not know about others, but in iOS, the document is created in / var / mobile / Application / YOU _APP / Documents

[THE CODE]

  <script type="text/javascript" charset="utf-8"> // Wait for PhoneGap to load // document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap is ready // function onDeviceReady() { window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); } function gotFS(fileSystem) { fileSystem.root.getFile("readme.txt", {create: true}, gotFileEntry, fail); } function gotFileEntry(fileEntry) { fileEntry.createWriter(gotFileWriter, fail); } function gotFileWriter(writer) { writer.onwrite = function(evt) { console.log("write success"); }; writer.write("some sample text"); writer.abort(); // contents of file now 'some different text' } function fail(error) { console.log("error : "+error.code); } </script> 

Hope this helps

+1
source

Depending on the devices you are developing, you may see how to use their own file creation APIs. For example, iOS uses plists. Android uses TXT files, look at for more information.

0
source

Source: https://habr.com/ru/post/1398569/


All Articles