set/get preference file

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
maxs
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 421
Joined: Sat Jun 24, 2006 12:02 am
Contact:

set/get preference file

Post by maxs » Sun Jul 21, 2013 3:09 am

I looked but could not find.

How can I make and get a preference file for IOS?

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

Re: set/get preference file

Post by Klaus » Sun Jul 21, 2013 4:10 pm

Hi Max,

the same way as on the desktop, maybe in another folder :-D
Read from and write to -> specialfolderpath("documents") & "/your_pref_file_here"


Best

Klaus

maxs
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 421
Joined: Sat Jun 24, 2006 12:02 am
Contact:

Re: set/get preference file

Post by maxs » Sun Jul 21, 2013 5:11 pm

Thanks, Klaus,

So simple, but not so obvious for me.

Max

maxs
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 421
Joined: Sat Jun 24, 2006 12:02 am
Contact:

Re: set/get preference file not working

Post by maxs » Sun Jul 21, 2013 5:55 pm

It works for the Mac, but not ios.
I copied the special folder path folder with the file into the standalone prefs, but this code does nothing. Where did I go wrong?

on closecard
if the environment = mobile
then
open file specialfolderpath("myprefs") & "/PrefsMax"
write ( the number of this card ) to file "PrefsMax"
close file specialfolderpath("myprefs") & "/PrefsMax"
end if
end closecard

on opencard
global NewCardLoc
if the environment = mobile
then
if there is a file specialfolderpath("myprefs") & "/PrefsMax"
then
open file specialfolderpath("myprefs") & "/PrefsMax"
read from file "PrefsMax" at 1 until eof
put it into NewCardLoc
put it into fld "testit" of cd 2
close file specialfolderpath("myprefs") & "/PrefsMax"
end if
end if
end opencard

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

Re: set/get preference file

Post by Klaus » Sun Jul 21, 2013 5:59 pm

"myprefs" is NOT a valid "specialfolderpath", please check the dictionary if in doubt!
What's wrong with my proposal -> specialfolderpath("documents") ? 8-)

P.S.
You might want to use the shorter URL syntax:
...
put specialfolderpath("documents") & "/MinPrefs) into tPrefsFile
## Read:
put url("file:" & tPrefsFile) into fld "Wherever you want to display the prefs..."
...
## Write:
put the num of this cd into url("file:" & tPrefsFile)
...

maxs
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 421
Joined: Sat Jun 24, 2006 12:02 am
Contact:

Re: set/get preference file

Post by maxs » Sun Jul 21, 2013 9:31 pm

Thank you Klaus

I really like
put the num of this cd into url("file:" & tPrefsFile)

Does that save the number into a permanent file on the ipad? I can not be sure when using the simulator.


Max

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

Re: set/get preference file

Post by Klaus » Sun Jul 21, 2013 9:45 pm

Hi Max,

the simulator is also still a mistery to me :-D

Yes, this will save to a "permanent" file on the device itself!


Best

Klaus

Jellicle
Posts: 453
Joined: Thu Feb 24, 2011 11:07 am

Re: set/get preference file

Post by Jellicle » Sun Jul 21, 2013 9:52 pm

I use these three scripts to manage preferences. For example, if I want to write the value "1" to line 3 of the preferences file I call:

setPref 3, "1"

To get the value in line 3 of the preferences file I call:

getPref (3)

The writefile script simply manages the writing of the preferences file to the device.

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

on setPref whichPref, whatValue
open file specialFolderPath("Documents") & "/prefs.txt"
read from file specialFolderPath("Documents") & "/prefs.txt" until eof
close file specialFolderPath("Documents") & "/prefs.txt"
put whatValue into line whichPref of it
writeFile "prefs", it
end setPref

function getPref lineNumber
open file specialFolderPath("Documents") & "/prefs.txt"
read from file specialFolderPath("Documents") & "/prefs.txt" until eof
close file specialFolderPath("Documents") & "/prefs.txt"
return line lineNumber of it
end getPref

on writefile fileTowrite,dataTowrite
set the defaultFolder to specialFolderPath("Documents")
put dataTowrite into URL (("file:"&fileTowrite&".txt"))
end writefile



Cheers

Gerry
14" MacBook Pro
Former LiveCode developer.
Now recovering.

Post Reply