7 Replies Latest reply on Aug 8, 2011 3:38 PM by GarethAW

    Save text when application closes/re-opens?

    wolf14 Level 1

      Hello, on my air project I've got some input textfields where a user types a name and a code and then when he hits a button the flash communicates with a PHP file and if the name/code exists it displays some data on different dynamic textfields.


      Is it possible to create a "save" button for the input textfield info? for example type a word: "test" and hit save, then if you close the application and re-open it, the textfield must remember that "test" word so the user doesn't have to re-type it.

        • 1. Re: Save text when application closes/re-opens?
          GarethAW

          Hi, It is possible to create a "save" button for the input.

           

          I would personally recommend using the Local Store feature in Adobe AIR.

           

          Below is a GREAT Demonstration video of the local store feature;

          http://tv.adobe.com/watch/flash-downunder/encryped-local-store/

           

          You can use the concepts behind this video to create, encrypt and store information within the application securely.

          And ofcourse you can also call it back when the application re-opens again.

           

          I hope this information was useful/helpful,
          Enjoy your day,

          Gareth W.

          1 person found this helpful
          • 2. Re: Save text when application closes/re-opens?
            wolf14 Level 1

            Hi, that sounds like it, but I can't seem to make it work.

            Can you build a simple as3 example?

             

            Here's what I am trying to do:

             

            Type inside an input textfield -> hit save button -> close application -> open application -> input textfield displays the word typed before(without hitting any "load" buttons).

            • 3. Re: Save text when application closes/re-opens?
              GarethAW Level 1

              I don't want to give you too much, because that would hinder your learning...
              BUT here are some code snippets to help you on your way,
              Just watch the video carefully and try out the code, tinker around a bit and I bet it'll become second nature in no time

               

              - - - - - - - - - - - - - - - - - - - - - - - - -

              * Make sure you have these at the top of your code.

               

              import flash.utils.ByteArray;

              import flash.data.EncryptedLocalStore;

               

              - - - - - - - - - - - - - - - - - - - - - - - - -

              * When hitting the Save Button

               

              var bytes:ByteArray = new ByteArray();

              bytes.writeUTFBytes(input.text);

              EncryptedLocalStore.setItem("inputStore", bytes); // Save the Stored Input.

               

              - - - - - - - - - - - - - - - - - - - - - - - - -

              * Restore content on Application Load (without any "load" button")

               

              if (EncryptedLocalStore.getItem("inputStore")) // Check if the Stored Input is Saved.

              {

              input.text = bytes.readUTFBytes(bytes.length); // Read the Stored Input

              } else {

              input.text = "No Saved Content"; // This is some default text if there is no saved data.

              }

               

              - - - - - - - - - - - - - - - - - - - - - - - - -

              ** The Instance Name of the textfield you mentioned is "input" in this example.

              + N.B. Remember when loading content this way ALWAYS embed fonts.

               

              I hope this information was useful/helpful,
              Enjoy your day,

              Gareth W.

              1 person found this helpful
              • 4. Re: Save text when application closes/re-opens?
                wolf14 Level 1

                Yes, I can now understand how it works a little bit better by reading your example. I got a little confused by the video.

                 

                Ok so, there is an issue. Since I do not want a Load button, I suppose I need to put the if statement outside of the save button function.

                When I do that, I suppose this line doesn't work: EncryptedLocalStore.getItem("inputStore") since inputStore is stored inside the save button function.

                 

                Is this correct? And if so, how can I fix it?

                 

                 

                // EDIT:

                 

                I also tried removing

                EncryptedLocalStore.setItem("inputStore", bytes);

                from the function and pasting below the if statement.


                Although this works, when you type and save the word, then close and re open the application the word that appears is the one typed on the else statement.

                • 5. Re: Save text when application closes/re-opens?
                  GarethAW Level 1

                  I'm pleased this is helping you understand. The video is a lot more complex but once you get the grip of it - it's really clear

                   

                  I'm not sure why that lines not working... Here is how I tested it.
                  - - - - - - - - - - - - - - - - - - - - - - - - -

                  // Imports

                  import flash.utils.ByteArray;

                  import flash.data.EncryptedLocalStore;

                  // Variables

                  var bytes:ByteArray = new ByteArray();

                  // Save Function

                  save_btn.addEventListener(MouseEvent.CLICK, clickSave);

                  function clickSave(event:MouseEvent):void {

                     bytes.writeUTFBytes(input.text);

                     EncryptedLocalStore.setItem("inputStore", bytes);

                     input.text = "Saved Successfully";

                  }

                  // Loading saved content or default content

                  if (EncryptedLocalStore.getItem("inputStore"))

                  {

                     input.text = EncryptedLocalStore.getItem("inputStore").toString();

                  } else {

                     input.text = "No Saved Content";

                  }

                  - - - - - - - - - - - - - - - - - - - - - - - - -

                   

                  I've changed the code slightly to make it easier to manage and manipulate.


                  P.S. If you want to clear your Local Store use this code;

                  - - - - - - - - - - - - - - - - - - - - - - - - -

                  bytes.clear();

                  EncryptedLocalStore.removeItem("inputStore");

                  - - - - - - - - - - - - - - - - - - - - - - - - -

                  I normally place this code just after the variables at the top,
                  REMEMBER to comment them out before running the application afterwards or you will keep clearning the saved data on load
                  It might be a good idea to put both lines in a button/function to "Clear Saved Data" - if you wanted

                  I hope this information was useful/helpful,
                  Enjoy your day,

                  Gareth W.

                  • 6. Re: Save text when application closes/re-opens?
                    wolf14 Level 1

                    Thanks, it worked! It's an awesome feature by the way.

                    Going to practice now

                    • 7. Re: Save text when application closes/re-opens?
                      GarethAW Level 1

                      My Pleasure ^_^ I'm pleased I could help,

                      I hope your application works out well

                      It is a GREAT feature and comes in handy when you develop applications that let users change preferences/settings.

                      Hope you enjoy the rest of your day,
                      Good Luck,
                      Gareth W.