File creation.

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Kaubs
Posts: 124
Joined: Wed Jun 29, 2011 3:55 am

File creation.

Post by Kaubs » Tue Jul 12, 2011 5:48 pm

I am looking for a suggestion...a starting place more or less. When my app first starts it asks for a username and password....which are sent off to a server to authenticate...now when the application authenticates for the first time a file will be made containing the username and password essentially cached so that it can be used at a later time to authenticate with a different server when submitting a picture. The UN and PW will be the same on the second server but I don't want the user to have to input the info again. What I am looking for is what would be the best way to store this file so that it can be called and information pulled from it at a later time in the specialfolder or elsewhere? Could I have the iphone make something as simple as a text file or do I have to go with a mysql db? Just looking for a suggestion or other users experience with something similar so I can research and start writing the script in :)

Thanks,
Kaubs

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: File creation.

Post by townsend » Tue Jul 12, 2011 6:01 pm

You certainly could put it in a text file, but there may be a better way. Chances are, once you get going you'll need to store other values besides the user name and password.

A while back I wrote a example app to demonstrate how to use the built in SQLite DB manager in LiveCode. There's a secondary Stack there, with this single function for saving and retrieving any values in a SQLite DB. It might be ideal for your needs. It's just one function, you can copy and paste into your app.
CRUD SQLite Example
108 CRUD SQLite example 4.3.JPG

Klaus
Posts: 14182
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: File creation.

Post by Klaus » Tue Jul 12, 2011 6:43 pm

Hi Kaubs,
Kaubs wrote:What I am looking for is what would be the best way to store this file so that it can be called and information pulled from it at a later time in the specialfolder or elsewhere?
I'm afraid you are thinking far too complicated! :D
Yes, you can simply store a simple textfile with the UN and PW in a "specialfolderpath"!
Kaubs wrote:Could I have the iphone make something as simple as a text file...
What makes you think you can't?

Out of my head:
Call this on openstack or whenever you need the Username and Password.
If the function returns EMPTY you need to lask for the username and password.

Code: Select all

function retrieveUNandPW
   put specialfolderpath("documents") & "/your_file_name_here" into tFile
   if there is a file tFile then

    ## File has been save here before:
    return url("file:" & tFile)
  else

   ## Not saved yet...
   return empty
  end if
end retrieveUNandPW
Save the data once the user entered them correctly:

Code: Select all

## 2 Parameter Username and Password
command saveUNandPW tUN, tPW
  
  ## Just save the stuff in a text file:
  ## First line = Username
  ## Second line = Password

  put specialfolderpath("documents") & "/your_file_name_here" into tFile
  put tUN & CR & tPW into url("file:" & tFile)
end saveUNandPW
You get the picture. A SQLite database seem a bit overkill in this case :)

Hope that helps!


Best

Klaus

Kaubs
Posts: 124
Joined: Wed Jun 29, 2011 3:55 am

Re: File creation.

Post by Kaubs » Tue Jul 12, 2011 6:57 pm

Thank you both! The SQL DB may come in handy at a later time...for the app I am creating right now the text file should be sufficient...I think I will start with that method and see what I can do.

Kaubs
Posts: 124
Joined: Wed Jun 29, 2011 3:55 am

Re: File creation.

Post by Kaubs » Tue Jul 12, 2011 7:11 pm

next question....lol. I have a simple login field, the user specifies the username and password. Would I use a function like rawkeyup to pull the data from those fields to my text file? Trying to put something together as if you literally had a field named username and a field named password with a button underneath called login.

Thanks again guys and Klaus you never sleep man!

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: File creation.

Post by BvG » Tue Jul 12, 2011 7:22 pm

I would probably put the code into the "login" button. so when the user presses it, the app compares his entry with the stored data, and if it's a match, only then it would go to the next step.
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

Kaubs
Posts: 124
Joined: Wed Jun 29, 2011 3:55 am

Re: File creation.

Post by Kaubs » Tue Jul 12, 2011 10:44 pm

Ok, was trying to test the scripting and make sure it was working properly but something seems wrong....

Code: Select all

on mouseUp
   send "saveUNandPW" to me in 0 milliseconds
end mouseUp


command saveUNandPW tUN, tPW
     put specialfolderpath("documents") & "/test" into tFile
     put tUN & CR & tPW into url("file:test.txt" & tFile)
     if there is a file specialfolderpath("documents") & "/test" then
        go to card 6
    end if
      
end saveUNandPW
anything wrong there?

Kaubs
Posts: 124
Joined: Wed Jun 29, 2011 3:55 am

Re: File creation.

Post by Kaubs » Tue Jul 12, 2011 11:21 pm

Ok....after some testing this seemed to work. Is it correctly verifying that there is a file tho and if so its just an empty file right?

Code: Select all

on mouseUp
   send "saveUNandPW" to me in 0 milliseconds
end mouseUp



command saveUNandPW tUN, tPW
     put specialfolderpath("documents") & "/test" into tFile
     put tUN & CR & tPW into url("file:" & tFile)
     if there is a file tFile then
        go to card 3
    end if
      
end saveUNandPW

Klaus
Posts: 14182
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: File creation.

Post by Klaus » Wed Jul 13, 2011 1:00 pm

Hi Kaubs,

of course you need to supply the Username and the Password as the two parameters to my function!

Presumed this "mosueup" script is in the button "Login" or "OK" that the user clicks after he entered the username and pasword, right?

Code: Select all

on mouseUp
   put fld "username" intotUN
   put fld "password" int tPW

  ## The function need to knwo these two parameters!
  ## Otherwise you will find the strings tUN and tPW in that fiel!
  saveUNandPW tUN, tPW
end mouseUp

command saveUNandPW tUN, tPW

  ## YEP! tFile IS already the correct filename!
  put specialfolderpath("documents") & "/test" into tFile
  put tUN & CR & tPW into url("file:" & tFile)

   ## NO idea what this is good for?????
   if there is a file tFile then
      go to card 3
  end if     
end saveUNandPW
Is it correctly verifying that there is a file tho and if so its just an empty file right?
Your "if there is a file..." does indeed check for the presence of that file, but I have no idea what its good for AFTER we wrote the file, so this check will always be positive!?
The script abvove will only write the username and password to that file!
If the file is already there, it will be overwritten!
This is not an empty file since we wrote the users name a pasword to it!


Best

Klaus

Kaubs
Posts: 124
Joined: Wed Jun 29, 2011 3:55 am

Re: File creation.

Post by Kaubs » Wed Jul 13, 2011 5:40 pm

Presumed this "mosueup" script is in the button "Login" or "OK" that the user clicks after he entered the username and pasword, right?
Correct!

Code: Select all

 if there is a file tFile then
I had used for testing to continue to the next card after the file was created. I couldn't find another way to verify that the file was in fact there.

The code

Code: Select all

on mouseUp
   put fld "username" intotUN
   put fld "password" int tPW
  saveUNandPW tUN, tPW
end mouseUp

command saveUNandPW tUN, tPW

  put specialfolderpath("documents") & "/test" into tFile
  put tUN & CR & tPW into url("file:" & tFile)
      go to card 3
  end if     
end saveUNandPW
Resides on my second card on the login button.

The stack itself will contain

Code: Select all

on openstack
   put specialfolderpath("documents") & "/test" into tFile
   if there is a file tFile then
   go to card 3
   end if
end openstack

Does this clear up what I am aiming for? If the user has specified correct credentials...which will be worked in with a server for authentication the user will not have to authenticate on subsequent visits to the app and it will just skip to card 3...Leaving the text file behind is so that I can collect the log in info later in the app to authenticate with another server without the users input.

Is the line "put specialfolderpath("documents") & "/test" into tFile" just creating a new file? If so...how on the stack would I verify the file exists and not replace it? How would changing directories for verification be handled?

Kaubs
Posts: 124
Joined: Wed Jun 29, 2011 3:55 am

Re: File creation.

Post by Kaubs » Wed Jul 13, 2011 7:06 pm

Ok...so this works. I verified the file contents using iPhone Explorer.

Card 2 contains 2 fields, one for username and one for password.
The login button contains this script

Code: Select all

on mouseUp
   put fld "username" into tUN
   put fld "password" into tPW
 saveUNandPW tUN, tPW
end mouseUp

command saveUNandPW tUN, tPW
   put specialfolderpath("documents") & "/text.txt" into tFile
   put tUN & CR & tPW into url("file:" & tFile)
      go to card 3 
end saveUNandPW
The stack itself contains this script.

Code: Select all

on openstack
   put specialfolderpath("documents") & "/text.txt" into tFile
   if there is a file tFile then
   go to card 3
   end if
end openstack

Klaus
Posts: 14182
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: File creation.

Post by Klaus » Thu Jul 14, 2011 12:31 am

Hi Kaubs,

yes :D


Best

Klaus

Post Reply