Page 1 of 1
iOS Text Files and Fields
Posted: Sat Mar 03, 2012 6:55 pm
by ncmac
I have been experimenting with saving and loading a text file. It's pretty straightforward:
To Save:
on mouseUp
set the defaultFolder to specialFolderPath("Documents")
put the text of field "NameField" into tName
put the date into tDateĀ
put field "Field" into URL ("file:tName && tDate.txt")
end mouseUp
To Load:
on mouseUp
set the defaultFolder to specialFolderPath("Documents")
put URL ("file:tName && tDate.txt") into field "Field"
end mouseUp
How can a user save multiple unique files, and then select one to load from the existing set of files they have created?
I am using the LiveCode fields rather than creating a UITextField. This is also confusing. I need to be able to use chunk expressions (put x in item 1 of line 1) and couldn't get it to work with the UITextField. So, if it's OK to use Livecode fields to catch user date, is there a rule for when a UITextField should be used? I don't need the keyboard and will actually be setting the focus to false.
Thanks as always!
Re: iOS Text Files and Fields
Posted: Sun Mar 04, 2012 12:11 am
by Jellicle
ncmac wrote:
How can a user save multiple unique files, and then select one to load from the existing set of files they have created?
Use "the files". So:
Code: Select all
set the defaultFolder to specialFolderPath("Documents")
put the files into fld "ListOfFiles"
Once you have the list of files you can present them in a number of different ways for the user to select - a scrolling list field, a data grid or a html/css list shown in a browser object (styled to look like a real iOS lost) are among the options.
I need to be able to use chunk expressions (put x in item 1 of line 1) and couldn't get it to work with the UITextField.
You can get and set the contents of native text controls. So you can do this:
Code: Select all
put iPhoneControlGet (controlID,"text") into theText
put "fred" into item 3 of line 6 of theText
iphoneControlSet controlID,"text",theText
Gerry
Re: iOS Text Files and Fields
Posted: Sun Mar 04, 2012 8:05 pm
by ncmac
Thanks for your help. I did look in to using "the files", but still am not understanding how to associate a particular user with their own data file. I am assuming it would be a variable that points to the file. But, how would you present those user choices to the end user if the engine can't write back to itself? In other words, how are new users created and then referenced among? An example stack would be greatly appreciated.
Also, is there any problems with using the Livecode field object to store data? Is there any reason it shouldn't be used on iOS?
Re: iOS Text Files and Fields
Posted: Sun Mar 04, 2012 9:46 pm
by Jellicle
iOS doesn't really understand the idea of multiple users - it's usually one user per device. But if your app will be used on a single device by more than one user use a log in system and create a folder for each user inside the app's document folder where you store their documents - you can write files there with no restrictions. When they log in, set the default folder to "documents"&"/"&username, or something like that.
Apps can't be saved so text you put in a field at run time won't be there when you re-open the app - store the text in a file when the app shuts down.
Gerry
Re: iOS Text Files and Fields
Posted: Mon Mar 05, 2012 5:36 am
by FireWorx
ncmac,
Here is an example of using a text file to keep track of user preferences.
Global tBlend, tCcolor,tPrefs
on preopenstack
if the environment is not "mobile" then exit preopenstack
put specialFolderPath("library") & "/settings.txt" into tPath ##Now take care of the settings
if there is a file tPath then
open file tPath
read from file tPath until EOF
close file tPath
put it into tPrefs
setprefs tPrefs ## the handler down below that loads the global variables that contain the settings
else ## Else its the first time ever launched so lets enter some standard settings
put "61" into line 1 of tPrefs ##the blend level
put "255,255,255" into line 2 of tPrefs ##The color Red
put specialFolderPath("library") & "/settings.txt"into tPath
open file tPath for write
write tPrefs to file tPath ## write the same info to the text file for the first and only time
close file tPath
setprefs tPrefs ## populate the global vars
if there is not a file tPath then
answer "There was a problem loading the file" ##program will not operate without data from prefs
end if
exit preopenstack ## Then wen can leave this handler right now and use the stack as built!
end if
end preopenstack
on setprefs tPrefs ## This function will populate the global vars from the setting.txt file
put line 1 of tPrefs into tBlend
put line 2 of tPrefs into tCcolor
end setprefs
---------------------------
THEN here is the code to write the users preferences to the text file for storage activated in the settings control panel group via a button script but could also be executed in the close stack script.
global tPrefs,tBlend,tCcolor
on mouseUp
if the environment is not "mobile" then exit mouseUp
put tBlend into line 1 of tPrefs
put tCcolor into line 2 of tPrefs
put specialFolderPath("library") & "/settings.txt"into tPath
open file tPath for write
write tPrefs to file tPath ## write the same info to the text file for the first and only time
close file tPath
if there is not a file tPath then
answer "There was a problem loading the file" ##program will not operate without data from prefs
end if
end mouseUp
Hope this helps. Funny that I am now just now realizing I am using this script in the same stack I also am using an SQLite database in. Guess I could have just had a db table with some fields installed to store the preferences data in. =]
Dave